I have a Android Service which scans for Bluetooth Low Energy devices. This works but if the app is killed the scan dont work. The service is running but I see in the logcat that the binder for bluetooth is dead.
D/BtGatt.GattService(3088): Binder is dead - unregistering client (5)!
D/BtGatt.GattService(3088): stopScan() - queue=1
D/BtGatt.GattService(3088): stopScan() - queue empty; stopping scan
D/BtGatt.btif(3088): btif_gattc_scan
D/BtGatt.btif(3088): btgattc_handle_event: Event 1003
D/BtGatt.GattService(3088): unregisterClient() - clientIf=5
D/BtGatt.btif(3088): btif_gattc_unregister_app
D/BtGatt.btif(3088): btgattc_handle_event: Event 1001
D/BtGatt.btif(3088): btif_gattc_upstreams_evt: Event 1
How can i scan for bluetooth devices with a service? Thank you
Unlike previous versions, in 4.4 swiping app out of recent tasks permanently kills app along with its service (like force-stop) even though it's running background services. Here you can find a "quick and dirty workaround":
In android 4.4, swiping app out of recent tasks permanently kills application with its service . Any idea why?
Related
I am trying to perform BLE scans in a foreground service (Android 12) after some start scan end stop scan I receive the error SCAN_FAILED_APPLICATION_REGISTRATION_FAILED, I cannot figure out what is the reason of the error.
I do startScan and stopScan in sequence (the scan is a long-running scan)
The app target SDK 32 so I have the new Bluetooth permissions.
This is the complete scenario:
I open the app and I start the foreground service
I close the application (the foreground service is still running)
The foreground service scan and connect to my device and then I stop scanning
I turn off my BLE device and the foreground service start to scan again
I turn on my ble device and the foreground service detect it and connect to it.
Repeat step 4 and suddenly the error SCAN_FAILED_APPLICATION_REGISTRATION_FAILED show.
I also notice this error in the logcat App 'com.sample.xxx' is scanning too much clientif (I look into AOSP source code and seems that this string is specific of Xiaomi ROM)
https://github.com/appersiano/TestingBackgroundConnection
P.S. Tested on Xiaomi Mi11 Lite 5g
How can I solve?
you should not scan more than 5 times in 30s.
GattService:
This constant defines the time window an app can scan multiple times.
Any single app can scan up to |NUM_SCAN_DURATIONS_KEPT| times during
this window.
Once they reach this limit, they must wait until their earliest recorded scan exits this window.
static final int NUM_SCAN_DURATIONS_KEPT = 5;
static final long EXCESSIVE_SCANNING_PERIOD_MS = 30 * 1000;
I am runnning an app or service with a active Bluetooth LE scanner and showing scan results on Log console. If I lock the phone in a table and not touching anymore. After a time it stops, and it doesn't give me more scan results.
If I press power button and the screen wake up it gives me more scan results. If i lock again the screen or wait to lock automatically it stops and not give me more scan results.
I test with service and an app that give me more results by Log and see the app is running but scanner LE stops and no give me more results while the screen is lock.
I have the app in "no optimized battery" for doze mode. I test forcing by command introducing the phone en doze mode and work fine it give me scan results.
In my Nexus 5 with Android 7.1.1 pass when running APP and lock the screen and not touch anymore the phone. The time is 30 minutes. The phone is in a table alone, only connected with microusb to see the log in android studio.
In other Moto G2 with android 7.1 pass exactly but the time is between 20 minutes and 40 minutes, it is more aleatory. The phone is in a table alone, only connected with microusb to see the log in android studio.
For have running well again, I need to force close the app manually and restart, otherwise only works when screen is active and no give me more results when screen is locked.
This is used for beacon results, first I use Android Beacon Library for this purpose and the result was the same.
I think it is a problem of android bluetooth component, because I have the same result with the Android Beacon Library or if I implement my own BLE Scanner, but I don't know how to solve this.
Are any form to use Bluetooth Scanner LE always running in Android when the phone is much time in lock state??
Thanks in advance.
Best regards.
Android 7.0 introduced a BLE scan timeout, where any scan running for 30 minutes or more is effectively stopped automatically and only resumed "opportunistically" which essentially means that if another process does a scan, it can get the results as well.
You can see this by setting up code to start a Bluetooth LE scan and leave it running indefinitely. After exactly 30 minutes, the scan will stop, and you will see entries like this in LogCat:
06-11 19:00:22.848 5123 5147 D BtGatt.ScanManager: clientIf set to scan opportunisticly: 6
06-11 19:00:22.848 5123 5147 D BtGatt.ScanManager: configureRegularScanParams() - queue=1
06-11 19:00:22.848 5123 5147 D BtGatt.ScanManager: configureRegularScanParams() - ScanSetting Scan mode=-1 mLastConfiguredScanSetting=2
06-11 19:00:22.848 5123 5147 D BtGatt.ScanManager: configureRegularScanParams() - queue emtpy, scan stopped
06-11 19:00:22.849 5123 5147 D BtGatt.ScanManager: stop scan
You can see the code that does this in the AOSP source here:
https://android.googlesource.com/platform/packages/apps/Bluetooth/+/android-7.0.0_r1/src/com/android/bluetooth/gatt/ScanManager.java#72
A workaround for this is to not keep scans going that long. You can simply stop them and restart them periodically.
I am developing an Android App that communicates with a BLE Peripheral. I am able to enable the notifications and receive data from the BLE Peripheral. However, a few seconds later, I receive the following message on my Android Terminal in Android Studio and the Notifications stop.
I noticed that Android phone is still connected to the BLE Peripheral as Status = 0, but after the BLE Connection Interval Change = 156, the Notifications stop.
Has anyone encountered a similar problem? I am using the BluetoothLEGatt example from Android Studio.
Thanks
Hani Abidi
D/BluetoothGatt: onClientConnParamsChanged() - Device=80:EA:CA:00:A8:77 interval=156 status=0enter code here
So I ran into this issue as well and although I don't 100% know the exact cause, I can at least relay how I "fixed" the problem and my experiences.
So I'm using a Samsung Galaxy Note 4 running 6.0.1 and using RxAndroidBle to ease the pain of having to deal with BLE on Android. My application connects to a PSoC4 BLE microprocessor and immediately does a back and forth data transfer with said microprocessor. What I was seeing was similar to the OP: more often than not the back and forth transmission would just stop dead in its tracks with the following messages:
03-15 16:06:39.989 27793-28784/application D/BluetoothGatt: onClientConnParamsChanged() - Device=00:A0:50:65:24:91 interval=6 status=0
03-15 16:06:40.289 6292-6754/? D/BtGatt.GattService: onClientConnParamsChanged() - clientIf=7 address=00:A0:50:65:24:91, interval=39status=0
03-15 16:06:40.289 27793-28561/application D/BluetoothGatt: onClientConnParamsChanged() - Device=00:A0:50:65:24:91 interval=39 status=0
03-15 16:06:54.989 6292-6754/? D/BtGatt.GattService: onClientConnParamsChanged() - clientIf=7 address=00:A0:50:65:24:91, interval=6status=0
03-15 16:06:54.989 27793-27823/application D/BluetoothGatt: onClientConnParamsChanged() - Device=00:A0:50:65:24:91 interval=6 status=0
03-15 16:06:55.259 6292-6754/? D/BtGatt.GattService: onClientConnParamsChanged() - clientIf=7 address=00:A0:50:65:24:91, interval=39status=0
03-15 16:06:55.259 27793-28784/application D/BluetoothGatt: onClientConnParamsChanged() - Device=00:A0:50:65:24:91 interval=39 status=0
It appeared to me at first that for some reason the phone was still negotiating the connection interval in the middle of the data transfer and, for some reason, would just completely get screwed up as a result.
Maybe this is what is happening, maybe it isn't, but what I found was the root cause of this was that I am (apparently) connecting and interfacing with my microprocessor over BLE before the BLE device scanner is completely done shutting down.
When the user taps on the table view cell to connect to the device, I was originally calling to stop subscribing to the RxAndroidBle observable that is scanning for devices, which should end the BLE scanning and then immediately launched into showing a new Activity for communicating with the PSoC4 which in turn immediately connects and starts interfacing with the microprocessor:
scanSubscription.unsubscribe();
Intent myIntent = new Intent(MainActivity.this, ConnectedActivity.class);
myIntent.putExtra("address", testdevice.getAddress());
myIntent.putExtra("name", testdevice.getName());
MainActivity.this.startActivityForResult(myIntent, CONNECTED_ACTIVITY);
It appears, however, that simply unsubscribing from the scan subscription does not immediately end the BLE scanning so my current strategy is to wait 500 ms before starting any communications with the other device. This is obviously not ideal - ideally you'd have a method callback when scanning actually was ceased, but I haven't figured out how to do that just yet.
Hopefully this helps someone.
Connection parameter update is unrelated to notification subscription status. Are you saying your onCharacteristicChanged are not being called although you send notifications from the peripheral? You should check your code again. If you think you've done everything correct you should check android's hci snoop log or use a BLE sniffer to see what really happens.
I am developing an app that needs to connect to a ble peripheral automatically.
I have a sticky service that does the following:
looks for the required device in bonded devices
if it doesn't fine the device (first time), scans for it and bonds to it by using device.createBond(), waits for the bond to finish by listening to the ACTION_BOND_STATE_CHANGED broadcast
connects to it using device.connectGatt(ctx,true,callback)
waits for onConnectionStateChange callback with connected state
starts service discovery by using gatt.discoverServices()
waits for onServicesDiscoverd callback
enables notifications on a characteristic by writing a descriptor using gatt.writeDescriptor
waits for onDescriptorWrite callback with success status BluetoothGatt.GATT_SUCCESS (0)
does stuff with the notifications it receives
This all works fine for the first time. When the device disconnects (becomes out of range for instance, or turned off) the sticky service callbacks gatt.disconnect() and gatt.close(), restarts and does all this again, this time it uses the bonded device to connect.
Everything works fine until step 7, meaning I get a callback to onDescriptorWrite with status 133 sometimes followed by a connection state change callback with state 0 and status 22.
I could not find any info online for what status 133 or 22 mean.
Any idea why this is happening?
I am sort of working around it now by reacting to the bad onDescriptorWrite callback by removing the bond (reflection) an doing everything again with the freshly scanned device.
So basically I am using the bond just to wait for the device connection and then restart the whole thing.
This means a gatt connection for a bonded device is useless for writing the descriptor I need.
It feels like I am missing something, would love to know what.
EDIT:
some relavant logcat output
08-18 16:06:31.363 12765-12835/? W/bt-att﹕ gatt_rsp_timeout disconnecting...
08-18 16:06:31.363 12765-12835/? W/bt-btif﹕ bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0016
08-18 16:06:31.363 12765-12835/? W/bt-btif﹕ bta_gattc_conn_cback() - cif=4 connected=0 conn_id=4 reason=0x0016
08-18 16:06:31.363 12765-12835/? W/bt-btif﹕ bta_gattc_conn_cback() - cif=5 connected=0 conn_id=5 reason=0x0016
08-18 16:06:31.366 12765-12807/? D/BtGatt.GattService﹕ onDisconnected() - clientIf=5, connId=5, address=C1:D1:22:BA:F5:13
here im getting onDescriptorWrite with status 133
D/BluetoothGatt﹕ onClientConnectionState() - status=22 clientIf=5 device=C1:D1:22:BA:F5:13
by the looks of this:
https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/idea133/bta/include/bta_gatt_api.h#169
16 means BTA_GATT_CONN_TERMINATE_LOCAL_HOST
22 means BTA_GATT_CONN_LMP_TIMEOUT
asked this on nordics github:https://github.com/NordicSemiconductor/Android-nRF-Toolbox/issues/9#issuecomment-132191406
asked this on nordics github:
https://github.com/NordicSemiconductor/Android-nRF-Toolbox/issues/9#issuecomment-132191406
following their recommendation added a 2 second delay after calling connectGatt and now it works.
I'm trying to connect a BLE Device (Sigma chest strap) to my Sony Smartwatch 3 and the writeDescriptor() method fails to enable Notifications on the Device. The onDescriptorWrite() method displays the status code "128" which apparently stands for "GATT_NO_RESOURCES". Does anyone know what this error means or how to fix it?
Another BLE Device (Alpha Mio 2) works without any problems. descriptor is written and data received via onCharacteristicChanged().
Descriptor UUID: 00002902-0000-1000-8000-00805f9b34fb
Service UUID: 0000180d-0000-1000-8000-00805f9b34fb