I am using a BLE reactive Flutter library to communicate between STM32WB and my Android phone app (Android 12). Multiple devices (STMs) can be connected to the app at the same time. If one of the devices timeouts, it goes over my disconnection procedure. That works well, but every now and then there is a bug, that some other STM also disconnects without even going over my disconnect functions. What could be the cause of that?
Library github:
https://github.com/PhilipsHue/flutter_reactive_ble#readme
Test case
In this test 3 STM devices were connected to the phone. I purposely timeouted 00:80:E1:26:D2:6D. After that 00:80:E1:26:E6:CC also disconnected itself, but not over my functions. The third device stayed connected and working as it should.
D/BluetoothGatt(27591): onPhyUpdate() - status=0 address=00:80:E1:26:D2:6D txPhy=2 rxPhy=2
// *** I turn off the power to one STM so it timeouts for testing (status 8 - Connection timeout)
D/BluetoothGatt(27591): onClientConnectionState() - status=8 clientIf=9 device=00:80:E1:26:D2:6D
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000aa02-8e22-4541-9d4c-21edae82ed19 enable: false
D/BluetoothGatt(27591): close()
D/BluetoothGatt(27591): unregisterApp() - mClientIf=9
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000ac02-8e22-4541-9d4c-21edae82ed19 enable: false
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000ad02-8e22-4541-9d4c-21edae82ed19 enable: false
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000ae02-8e22-4541-9d4c-21edae82ed19 enable: false
// *** My disconnection handling functions get called.
[log] Connecting widget: bt disconnected
[log] Battery provider: removed battery 00:80:E1:26:D2:6D
[log] Handling connection lost from: 00:80:E1:26:D2:6D
[log] Disconnecting device: 00:80:E1:26:D2:6D
// *** Procedure to disconnect the timeouted device completed.
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000ad03-8e22-4541-9d4c-21edae82ed19 enable: false
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000af02-8e22-4541-9d4c-21edae82ed19 enable: false
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000ad04-8e22-4541-9d4c-21edae82ed19 enable: false
// *** All 7 streams of disconnected STM get canceled ok
// *** Randomly another device disconnects, but not over my disconnect procedure.
D/BluetoothGatt(27591): cancelOpen() - device: 00:80:E1:26:E6:CC
D/BluetoothGatt(27591): onClientConnectionState() - status=0 clientIf=11 device=00:80:E1:26:E6:CC
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000aa02-8e22-4541-9d4c-21edae82ed19 enable: false
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000ac02-8e22-4541-9d4c-21edae82ed19 enable: false
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000ad02-8e22-4541-9d4c-21edae82ed19 enable: false
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000ae02-8e22-4541-9d4c-21edae82ed19 enable: false
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000ad03-8e22-4541-9d4c-21edae82ed19 enable: false
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000af02-8e22-4541-9d4c-21edae82ed19 enable: false
D/BluetoothGatt(27591): setCharacteristicNotification() - uuid: 0000ad04-8e22-4541-9d4c-21edae82ed19 enable: false
// *** All its 7 streams get canceled.
D/BluetoothGatt(27591): onClientConnectionState() - status=0 clientIf=11 device=00:80:E1:26:E6:CC
// *** My third STM is still connected and working ok tho
Main BLE Reactive function that handles connection changes:
Future<void> handleBluetoothConnectionChanges(String deviceUuid) async {
QueuedDevices device = connectedDevices.where((element) => element.device.id == deviceUuid).first;
device.subscription = Provider.of<BleDeviceConnector>(context, listen: false).state.listen((event) async {
switch(event.connectionState){
case DeviceConnectionState.disconnected:
if(device.connectionState == DeviceConnectionState.disconnected || device.device.id != event.deviceId)
return;
Globals.gLogMessage("Connecting widget: bt disconnected", MessageType.DEBUG);
// Handles all UI changes etc. Nothing directly related to BLE connection.
await Provider.of<BatteriesModel>(context, listen: false).removeBattery(context, deviceUuid);
// Cancels connection and removes device
await handleConnectionLostOrTimeout(device);
break;
}
device.connectionState = event.connectionState;
})..onError((error){
Globals.gLogMessage("EXCEPTION - device connection error!: " + error.toString(), MessageType.ERROR_FATAL);
});
}
handleConnectionLostOrTimeout()
Future<void> handleConnectionLostOrTimeout(QueuedDevices device) async {
try {
Globals.gLogMessage("Handling connection lost from: " + device.device.id, MessageType.DEBUG);
await device.subscription?.cancel();
await Provider.of<BleDeviceConnector>(context, listen: false).disconnect(device.device.id);
connectedDevices.removeWhere((element) => element.device.id == device.device.id);
}
catch(exception) {
Globals.gLogMessage(exception.toString(), MessageType.ERROR_FATAL);
}
}
Related
I'm working on an application connected to a BLE device, and I'm focused on the firmware update of the device (DFU). For that, I'm using the android lib created by Nordic and I'm trying to make it autonomous, so a user doesn't have to browse his files and pick the zip of the firmware update.
However, the DFU always stops at 30%, the app loses the bluetooth connection and I have to finish the update manually with an external app.
Is there anyone that has already made this kind of thing with DFU and can help me?
Here is a link to the lib's github: https://github.com/NordicSemiconductor/Android-DFU-Library
Here is the code I'm currently using, it's a standard DFU procedure with Nordic lib for Android.
val starter = DfuServiceInitiator(deviceMacAddress!!)
.setDeviceName(deviceName)
.setKeepBond(true)
starter.setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(true)
starter.setZip(uri)
val controller = starter.start(context!!, DfuService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
DfuServiceInitiator.createDfuNotificationChannel(context!!)
}
Edit: Here are some logs that show what happens on runtime
2019-01-08 10:54:35.481 3394-3394/com.thingsaremoving.enovap E/DfuListener: onDeviceConnected
2019-01-08 10:54:35.481 3394-3394/com.thingsaremoving.enovap E/DfuListener: onDfuProcessStarting
2019-01-08 10:54:36.492 3394-3698/com.thingsaremoving.enovap D/BluetoothGatt: setCharacteristicNotification() - uuid: 8ec90003-f315-4f60-9fb8-838830daea50 enable: true
2019-01-08 10:54:41.741 3394-3406/com.thingsaremoving.enovap D/BluetoothGatt: onClientConnectionState() - status=8 clientIf=11 device=FE:E6:FD:B8:13:DE
2019-01-08 10:54:41.741 3394-3592/com.thingsaremoving.enovap D/BluetoothGatt: onClientConnectionState() - status=8 clientIf=12 device=FE:E6:FD:B8:13:DE
2019-01-08 10:54:41.743 3394-3698/com.thingsaremoving.enovap D/BluetoothGatt: close()
2019-01-08 10:54:41.743 3394-3698/com.thingsaremoving.enovap D/BluetoothGatt: unregisterApp() - mClientIf=12
2019-01-08 10:54:41.755 3394-3406/com.thingsaremoving.enovap D/BluetoothGatt: setCharacteristicNotification() - uuid: 23058a49-636d-82ab-d641-badd3648cc9a enable: false
2019-01-08 10:54:41.755 3394-3598/com.thingsaremoving.enovap D/BluetoothManager: getConnectionState()
2019-01-08 10:54:41.755 3394-3598/com.thingsaremoving.enovap D/BluetoothManager: getConnectedDevices
2019-01-08 10:54:41.756 3394-3698/com.thingsaremoving.enovap D/BluetoothAdapter: isLeEnabled(): ON
2019-01-08 10:54:41.757 3394-3406/com.thingsaremoving.enovap E/Companion: Charac change error Disconnected from FE:E6:FD:B8:13:DE with status 8 (GATT_INSUF_AUTHORIZATION)
2019-01-08 10:54:41.757 3394-3406/com.thingsaremoving.enovap E/BleManager: release()
2019-01-08 10:54:41.758 3394-3406/com.thingsaremoving.enovap I/Companion: onConnectionStateChanged : RxBleConnectionState{DISCONNECTED}
2019-01-08 10:54:41.758 3394-3406/com.thingsaremoving.enovap E/BleManager: release()
2019-01-08 10:54:41.759 3394-3406/com.thingsaremoving.enovap E/bleManager: error characteristic change : Disconnected from FE:E6:FD:B8:13:DE with status 8 (GATT_INSUF_AUTHORIZATION)
2019-01-08 10:54:41.761 3394-3598/com.thingsaremoving.enovap D/BluetoothGatt: close()
2019-01-08 10:54:41.761 3394-3598/com.thingsaremoving.enovap D/BluetoothGatt: unregisterApp() - mClientIf=11
2019-01-08 10:54:41.764 3394-3406/com.thingsaremoving.enovap D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=12 mScannerId=0
2019-01-08 10:54:41.787 3394-3394/com.thingsaremoving.enovap E/ConnectionState: disconnected
2019-01-08 10:54:41.872 3394-3698/com.thingsaremoving.enovap D/BluetoothAdapter: isLeEnabled(): ON
2019-01-08 10:54:43.905 3394-3698/com.thingsaremoving.enovap D/BluetoothGatt: connect() - device: FE:E6:FD:B8:13:DF, auto: false
2019-01-08 10:54:43.906 3394-3698/com.thingsaremoving.enovap D/BluetoothGatt: registerApp()
2019-01-08 10:54:43.906 3394-3698/com.thingsaremoving.enovap D/BluetoothGatt: registerApp() - UUID=766e5e19-e1f9-48d3-a0a0-9c5eeba2b66a
2019-01-08 10:54:43.910 3394-3592/com.thingsaremoving.enovap D/BluetoothGatt: onClientRegistered() - status=0 clientIf=11
2019-01-08 10:54:44.059 3394-3406/com.thingsaremoving.enovap D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=11 device=FE:E6:FD:B8:13:DF
2019-01-08 10:54:44.060 3394-3406/com.thingsaremoving.enovap D/BluetoothGatt: discoverServices() - device: FE:E6:FD:B8:13:DF
2019-01-08 10:54:44.746 3394-3406/com.thingsaremoving.enovap D/BluetoothGatt: onConnectionUpdated() - Device=FE:E6:FD:B8:13:DF interval=24 latency=0 timeout=400 status=0
2019-01-08 10:54:45.045 3394-3406/com.thingsaremoving.enovap D/BluetoothGatt: onConnectionUpdated() - Device=FE:E6:FD:B8:13:DF interval=6 latency=0 timeout=500 status=0
2019-01-08 10:54:45.135 3394-3406/com.thingsaremoving.enovap D/BluetoothGatt: onSearchComplete() = Device=FE:E6:FD:B8:13:DF Status=0
2019-01-08 10:54:45.224 3394-3406/com.thingsaremoving.enovap D/BluetoothGatt: onConnectionUpdated() - Device=FE:E6:FD:B8:13:DF interval=24 latency=0 timeout=400 status=0
2019-01-08 10:54:45.825 3394-3406/com.thingsaremoving.enovap D/BluetoothGatt: onConnectionUpdated() - Device=FE:E6:FD:B8:13:DF interval=24 latency=0 timeout=400 status=0
2019-01-08 10:54:46.149 3394-3698/com.thingsaremoving.enovap D/BluetoothGatt: configureMTU() - device: FE:E6:FD:B8:13:DF mtu: 517
2019-01-08 10:54:46.244 3394-3406/com.thingsaremoving.enovap D/BluetoothGatt: onConfigureMTU() - Device=FE:E6:FD:B8:13:DF mtu=23 status=0
2019-01-08 10:54:46.246 3394-3698/com.thingsaremoving.enovap D/BluetoothGatt: setCharacteristicNotification() - uuid: 8ec90001-f315-4f60-9fb8-838830daea50 enable: true
2019-01-08 10:54:49.738 3394-3394/com.thingsaremoving.enovap E/timezone: time : 1.0
2019-01-08 10:54:49.738 3394-3394/com.thingsaremoving.enovap I/LocationUpdate: 2.2889202 : 48.8515689
2019-01-08 10:55:09.687 3394-3394/com.thingsaremoving.enovap E/timezone: time : 1.0
2019-01-08 10:55:09.687 3394-3394/com.thingsaremoving.enovap I/LocationUpdate: 2.2889202 : 48.8515689
2019-01-08 10:55:16.871 3394-3406/com.thingsaremoving.enovap E/DfuImpl: Characteristic write error: 133
2019-01-08 10:55:16.871 3394-3406/com.thingsaremoving.enovap D/BluetoothGatt: onClientConnectionState() - status=8 clientIf=11 device=FE:E6:FD:B8:13:DF
2019-01-08 10:55:16.873 3394-3698/com.thingsaremoving.enovap E/DfuBaseService: Unable to write Op Code 3 (error 133)
2019-01-08 10:55:16.874 3394-3698/com.thingsaremoving.enovap W/removing.enova: Accessing hidden method Landroid/bluetooth/BluetoothGatt;->refresh()Z (light greylist, reflection)
2019-01-08 10:55:16.875 3394-3698/com.thingsaremoving.enovap D/BluetoothGatt: refresh() - device: FE:E6:FD:B8:13:DF
2019-01-08 10:55:16.876 3394-3698/com.thingsaremoving.enovap D/BluetoothGatt: close()
2019-01-08 10:55:16.876 3394-3698/com.thingsaremoving.enovap D/BluetoothGatt: unregisterApp() - mClientIf=11
We are working on Android BLE & implemented successfully and it’s working fine with all OS version and devices.
We are able to connect successfully using below code:
mBluetoothDevice.connectGatt(this, true, mGattCallback);
connectGatt
But recently Samsung released Android Oreo(8.0.0) for Samsung Galaxy S9, S9+,S8, S8+ etc devices. Now we are not able to connect with BLE in Samsung devices with Android Oreo using above method. We didn’t get any callback in mGattCallback.
When the autoconnect flag true, the log is:
06-16 14:03:39.666 13724-13724/com.ble D/BluetoothGatt: cancelOpen() - device: DA:32:19:58:63:69
06-16 14:03:39.667 13724-13724/com.ble D/BluetoothGatt: close()
06-16 14:03:39.668 13724-13724/com.ble D/BluetoothGatt: unregisterApp() - mClientIf=0
06-16 14:03:39.792 13724-13724/com.ble D/BluetoothGatt: connect() - device: DA:32:19:58:63:69, auto: true
06-16 14:03:39.793 13724-13724/com.ble D/BluetoothGatt: registerApp()
06-16 14:03:39.793 13724-13724/com.ble D/BluetoothGatt: registerApp() - UUID=f672f50e-d321-4785-9af7-0668e5222970
06-16 14:03:39.797 13724-15182/com.ble D/BluetoothGatt: onClientRegistered() - status=0 clientIf=5
But if change the autoconnect flag to false then it’s working.
06-16 14:05:29.486 15681-15681/com.ble D/BluetoothGatt: connect() - device: DA:32:19:58:63:69, auto: false
06-16 14:05:29.487 15681-15681/com.ble D/BluetoothGatt: registerApp()
06-16 14:05:29.487 15681-15681/com.ble D/BluetoothGatt: registerApp() - UUID=a7c4d089-4ba2-422e-b2d1-355d56f6cc8b
06-16 14:05:29.489 15681-16134/com.ble D/BluetoothGatt: onClientRegistered() - status=0 clientIf=5
06-16 14:05:30.198 15681-16134/com.ble D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=5 device=DA:32:19:58:63:69
06-16 14:05:30.202 15681-16134/com.ble D/BluetoothGatt: discoverServices() - device: DA:32:19:58:63:69
06-16 14:05:30.503 15681-16134/com.ble D/BluetoothGatt: onConnectionUpdated() - Device=DA:32:19:58:63:69 interval=6 latency=0 timeout=500 status=0
06-16 14:05:31.033 15681-16134/com.ble D/BluetoothGatt: onSearchComplete() = Device=DA:32:19:58:63:69 Status=0
06-16 14:05:31.055 15681-16134/com.ble D/BluetoothGatt: setCharacteristicNotification() - uuid: 00001734-2927-efef-cdab-eba0fe583711 enable: true
06-16 14:05:31.108 15681-16134/com.ble D/BluetoothGatt: onConnectionUpdated() - Device=DA:32:19:58:63:69 interval=39 latency=0 timeout=500 status=0
When we apply this change it does not connect when the user goes out of range and come again in the range in all devices.
If anyone knows the solutions, please post here.
I have a problem when I try to use cheaper Android phones to communicate with our NordicSemi nRF52832 chip.
I can discover the peripheral and then connect, but when I device.discoverServices() the software pauses for about 10 seconds, and then disconnects with the output:
D/BluetoothGatt: connect() - device: CA:E7:8F:03:82:CE, auto: false
D/BluetoothGatt: registerApp() D/BluetoothGatt: registerApp() -
UUID=b05b54e9-bd20-423f-a6db-461775f954f0 D/BluetoothGatt:
onClientRegistered() - status=0 clientIf=7 D/BluetoothGatt:
onClientConnectionState() - status=0 clientIf=7
device=CA:E7:8F:03:82:CE D/BluetoothGatt: discoverServices() - device:
CA:E7:8F:03:82:CE D/BluetoothGatt: onSearchComplete() =
Device=CA:E7:8F:03:82:CE Status=129 D/BluetoothGatt:
onClientConnectionState() - status=22 clientIf=7
device=CA:E7:8F:03:82:CE D/BluetoothGatt: cancelOpen() - device:
CA:E7:8F:03:82:CE D/BluetoothGatt: close() D/BluetoothGatt:
unregisterApp() - mClientIf=7 D/BluetoothGatt: cancelOpen() - device:
CA:E7:8F:03:82:CE D/BluetoothGatt: close() D/BluetoothGatt:
unregisterApp() - mClientIf=0
With onServicesDiscovered status = 129
The phones are in this case: Samsung Galaxy J3 (J320FN) & Huawei P8lite (ALE-L21)
I'm using RxAndroidBle library for communicating with bluetooth device,
however recently i bumped into strange issue.
When reading one of the device characteristics i receive such error:
BleGattCharacteristicException{macAddress=60:CE:32:BA:9E:70,
status=6 (0x06 -> https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.1.0_r1/stack/include/gatt_api.h),
bleGattOperationType=BleGattOperation{description='CHARACTERISTIC_READ'}}
I see that the description states that something is wrong with the characteristic read but I was able to read it on the same device using some commercial scanners or vanilla bluetooth code from google samples.
I checked the characteristic properties - it has PROPERTY_READ.
I bump into same error when using Polidea sample of RxAndroidBle.
Couldn't find similar case anywhere in the internet, maybe someone bumped into such error.
Additional info might be that the device I'm connecting to is a ble device simulator running on an iPhone.
I'm not providing any snippets as I'm using the same code which is in the core examples.
Full verbose log:
06-12 11:00:20.105 22459-22459/? D/RxBle#Radio: QUEUED RxBleRadioOperationConnect(121589482)
06-12 11:00:20.106 22459-22504/? D/RxBle#Radio: STARTED RxBleRadioOperationConnect(121589482)
06-12 11:00:20.131 22459-22459/? D/BluetoothGatt: connect() - device: 41:0A:AE:0E:C2:50, auto: false
06-12 11:00:20.131 22459-22459/? D/BluetoothGatt: registerApp()
06-12 11:00:20.131 22459-22459/? D/BluetoothGatt: registerApp() - UUID=b6c1ef8f-408b-4afb-8e27-97a6ff2c20fb
06-12 11:00:20.137 22459-22473/? D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
06-12 11:00:20.330 22459-22473/? D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=41:0A:AE:0E:C2:50
06-12 11:00:20.333 22459-22473/? D/RxBle#BluetoothGatt: onConnectionStateChange newState=2 status=0
06-12 11:00:20.395 22459-22854/? D/CharacteristicOperationExampleActivity: Hey, connection has been established!
06-12 11:00:20.400 22459-22504/? D/RxBle#Radio: FINISHED RxBleRadioOperationConnect(121589482)
06-12 11:00:25.608 22459-22459/? I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
06-12 11:00:25.663 22459-22459/? I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
06-12 11:00:25.675 22459-22459/? D/RxBle#Radio: QUEUED RxBleRadioOperationServicesDiscover(3295555)
06-12 11:00:25.676 22459-22504/? D/RxBle#Radio: STARTED RxBleRadioOperationServicesDiscover(3295555)
06-12 11:00:25.683 22459-22459/? D/BluetoothGatt: discoverServices() - device: 41:0A:AE:0E:C2:50
06-12 11:00:25.726 22459-22473/? D/BluetoothGatt: onSearchComplete() = Device=41:0A:AE:0E:C2:50 Status=0
06-12 11:00:25.727 22459-22473/? D/RxBle#BluetoothGatt: onServicesDiscovered status=0
06-12 11:00:25.732 22459-22854/? D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicRead(177013014)
06-12 11:00:25.733 22459-22504/? D/RxBle#Radio: FINISHED RxBleRadioOperationServicesDiscover(3295555)
06-12 11:00:25.735 22459-22504/? D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicRead(177013014)
06-12 11:00:25.827 22459-22471/? W/BluetoothGatt: onCharacteristicRead() - Device=41:0A:AE:0E:C2:50 handle=102 Status=6
06-12 11:00:25.830 22459-22471/? D/RxBle#BluetoothGatt: onCharacteristicRead characteristic=1f26b1a4-2b50-11e7-99a8-fcaa142b0bea status=6
06-12 11:00:25.844 22459-22471/? D/RxBle#Radio: QUEUED RxBleRadioOperationDisconnect(84936079)
06-12 11:00:25.849 22459-22471/? D/elo: error: BleGattCharacteristicException{macAddress=41:0A:AE:0E:C2:50, status=6 (0x06 -> https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.1.0_r1/stack/include/gatt_api.h), bleGattOperationType=BleGattOperation{description='CHARACTERISTIC_READ'}}
06-12 11:00:25.879 22459-22504/? D/RxBle#Radio: FINISHED RxBleRadioOperationCharacteristicRead(177013014)
06-12 11:00:25.880 22459-22504/? D/RxBle#Radio: STARTED RxBleRadioOperationDisconnect(84936079)
06-12 11:00:25.915 22459-22459/? D/elo: error: BleGattCharacteristicException{macAddress=41:0A:AE:0E:C2:50, status=6 (0x06 -> https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.1.0_r1/stack/include/gatt_api.h), bleGattOperationType=BleGattOperation{description='CHARACTERISTIC_READ'}}
06-12 11:00:25.925 22459-22459/? D/BluetoothManager: getConnectionState()
06-12 11:00:25.925 22459-22459/? D/BluetoothManager: getConnectedDevices
06-12 11:00:25.951 22459-22459/? D/BluetoothGatt: cancelOpen() - device: 41:0A:AE:0E:C2:50
06-12 11:00:25.962 22459-22471/? D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=41:0A:AE:0E:C2:50
06-12 11:00:25.966 22459-22471/? D/RxBle#BluetoothGatt: onConnectionStateChange newState=0 status=0
06-12 11:00:25.971 22459-22459/? D/BluetoothGatt: close()
06-12 11:00:25.971 22459-22459/? D/BluetoothGatt: unregisterApp() - mClientIf=6
06-12 11:00:25.974 22459-22504/? D/RxBle#Radio: FINISHED RxBleRadioOperationDisconnect(84936079)
Verbose log from the Google Bluetooth Sample app in which I can read that property
06-12 14:43:36.540 10869-10869/com.example.android.bluetoothlegatt I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
06-12 14:43:36.598 10869-10869/com.example.android.bluetoothlegatt I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
06-12 14:43:36.599 10869-10869/com.example.android.bluetoothlegatt D/BluetoothLeService: Trying to use an existing mBluetoothGatt for connection.
06-12 14:43:41.209 10869-10869/com.example.android.bluetoothlegatt D/BluetoothAdapter: stopLeScan()
06-12 14:43:41.215 10869-10869/com.example.android.bluetoothlegatt D/BluetoothAdapter: scan not started yet
06-12 14:43:43.781 10869-10882/com.example.android.bluetoothlegatt D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=5F:56:F7:E9:8A:38
06-12 14:43:43.789 10869-10882/com.example.android.bluetoothlegatt I/BluetoothLeService: Connected to GATT server.
06-12 14:43:43.789 10869-10882/com.example.android.bluetoothlegatt D/BluetoothGatt: discoverServices() - device: 5F:56:F7:E9:8A:38
06-12 14:43:43.792 10869-10882/com.example.android.bluetoothlegatt I/BluetoothLeService: Attempting to start service discovery:true
06-12 14:43:45.513 10869-10882/com.example.android.bluetoothlegatt D/BluetoothGatt: onSearchComplete() = Device=5F:56:F7:E9:8A:38 Status=0
06-12 14:43:54.958 10869-10869/com.example.android.bluetoothlegatt D/BluetoothGatt: setCharacteristicNotification() - uuid: 1f26b1a4-2b50-11e7-99a8-fcaa142b0bea enable: true
06-12 14:43:55.063 10869-10882/com.example.android.bluetoothlegatt W/BluetoothGatt: onCharacteristicRead() - Device=5F:56:F7:E9:8A:38 handle=110 Status=0
There is a difference indeed, as this app is setting a notification before reading a value. The code in app responsible for that looks like this:
// If a given GATT characteristic is selected, check for supported features. This sample
// demonstrates 'Read' and 'Notify' features. See
// http://d.android.com/reference/android/bluetooth/BluetoothGatt.html for the complete
// list of supported characteristic features.
private final ExpandableListView.OnChildClickListener servicesListClickListner =
new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic =
mGattCharacteristics.get(groupPosition).get(childPosition);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(characteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(
characteristic, true);
}
return true;
}
return false;
}
};
As for RxAndroidBle version, I tested it on 1.2.0, 1.2.4 and 1.3.0.
Android versions 7.1, 6.0.1 and 5.1.
Will try to implement this notification before reading characteristic as it's the only difference I can see.
I have a TI Sensor Tag that I'm trying to interface with Android using BLE. I'm following the instructions here: http://processors.wiki.ti.com/index.php/SensorTag_User_Guide#SensorTag_Android_Development and things work up until the last step, I am not receiving onCharacteristicChanged callbacks. Note: I do receive ONE onCharacteristicWrite callback, but nothing after that. Please help!
At first I thought this might be due to the synchronous callbacks & queue issue explained in the instructions, but I implemented that and it didn't solve the problem.
Applicable classes of my code: https://github.com/bhadley/ble_testing/tree/master/src/com/example/helloworld
D/BtGatt.GattService( 771): onGetDescriptor() - address=BC:6A:29:AC:72:D9, stat
us=0, descUuid=00002901-0000-1000-8000-00805f9b34fb
D/BluetoothGatt( 3500): onGetDescriptor() - Device=BC:6A:29:AC:72:D9 UUID=000029
01-0000-1000-8000-00805f9b34fb
D/BtGatt.btif( 771): btif_gattc_get_descriptor
D/BtGatt.btif( 771): btgattc_handle_event: Event 1010
D/BtGatt.GattService( 771): onGetDescriptor() - address=BC:6A:29:AC:72:D9, stat
us=133, descUuid=00002901-0000-1000-8000-00805f9b34fb
D/BluetoothGatt( 3500): onSearchComplete() = Device=BC:6A:29:AC:72:D9 Status=0
I/BLE ( 3500): onServicesDiscovered
D/BluetoothGatt( 3500): writeCharacteristic() - uuid: f000aa32-0451-4000-b000-00
0000000000
D/BtGatt.GattService( 771): writeCharacteristic() - address=BC:6A:29:AC:72:D9
D/BtGatt.btif( 771): btif_gattc_write_char
D/BtGatt.btif( 771): btgattc_handle_event: Event 1015
D/BluetoothGatt( 3500): setCharacteristicNotification() - uuid: f000aa31-0451-40
00-b000-000000000000 enable: true
D/BtGatt.GattService( 771): registerForNotification() - address=BC:6A:29:AC:72:
D9 enable: true
D/BtGatt.btif( 771): btif_gattc_reg_for_notification
D/BtGatt.btif( 771): btgattc_handle_event: Event 1018
D/BluetoothGatt( 3500): writeDescriptor() - uuid: 00002902-0000-1000-8000-00805f
9b34fb
D/BtGatt.GattService( 771): onRegisterForNotifications() - address=null, status
=0, registered=1, charUuid=f000aa31-0451-4000-b000-000000000000
D/BtGatt.GattService( 771): writeDescriptor() - address=BC:6A:29:AC:72:D9
D/BtGatt.btif( 771): btif_gattc_write_char_descr
D/BtGatt.btif( 771): btgattc_handle_event: Event 1016
E/bt-btif ( 771): already has a pending command!!
I/BLE ( 3500): status of enabling magnetometer notifications: true
D/BtGatt.btif( 771): btif_gattc_upstreams_evt: Event 4
D/BtGatt.GattService( 771): onWriteCharacteristic() - address=BC:6A:29:AC:72:D9
, status=0
D/BluetoothGatt( 3500): onCharacteristicWrite() - Device=BC:6A:29:AC:72:D9 UUID=
f000aa32-0451-4000-b000-000000000000 Status=0
I/BLE ( 3500): onCharacteristicWrite
Thanks to some great help I received, I found the problems that fixed my issues:
1) needed to wait for the sensor to turn on THEN enable notifications (originally I called them sequentially without a pause) and this caused the error: already has a pending command!!
2) The correct flag for notifications for TI SensorTag is BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE (I had an incorrect tag in previously)
3) I had to implement a queue to schedule callbacks to prevent the pending command callback. Thanks to Steven Rudenko for some amazing help with that!
Data is coming in through the onCharacteristicChanged callback now!