Device used for testing: Nexus 4, Android 4.3
Connection is working fine but the onCharacteristicChanged
Method of my callback is never called. However I am registering for notifications using setCharacteristicNotification(char, true)
inside onServicesDiscovered
and that function even returns true.
Device log (there are actually no messages at all when notifications should appear / are sent via the Bluetooth device):
07-28 18:15:06.936 16777-16809/de.ffuf.leica.sketch D/BluetoothGatt: setCharacteristicNotification() - uuid: 3ab10101-f831-4395-b29d-570977d5bf94 enable: true
07-28 18:15:06.936 4372-7645/com.android.bluetooth D/BtGatt.GattService: registerForNotification() - address=C9:79:25:34:19:6C enable: true
07-28 18:15:06.936 4372-7645/com.android.bluetooth D/BtGatt.btif: btif_gattc_reg_for_notification
07-28 18:15:06.946 4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1018
07-28 18:15:06.946 4372-7645/com.android.bluetooth D/BtGatt.GattService: onRegisterForNotifications() - address=null, status=0, registered=1, charUuid=3ab10101-f831-4395-b29d-570977d5bf94
07-28 18:15:06.946 4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1016
07-28 18:15:06.946 4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1018
07-28 18:15:06.946 4372-7645/com.android.bluetooth D/BtGatt.GattService: onRegisterForNotifications() - address=null, status=0, registered=1, charUuid=3ab10102-f831-4395-b29d-570977d5bf94
07-28 18:15:06.946 4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1016
07-28 18:15:06.946 4372-7684/com.android.bluetooth E/bt-btif: already has a pending command!!
07-28 18:15:06.946 4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1013
07-28 18:15:06.946 4372-7684/com.android.bluetooth E/bt-btif: already has a pending command!!
07-28 18:15:06.946 4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1013
07-28 18:15:06.946 4372-7684/com.android.bluetooth E/bt-btif: already has a pending command!!
07-28 18:15:06.976 4372-7645/com.android.bluetooth D/BtGatt.btif: btif_gattc_upstreams_evt: Event 9
GATT Notifications work fine using iOS and the app basically does the same as on Android (registering for notification etc.).
Has anyone else experienced this with a possible solution?
I've experienced the problems with notifications for BLE on Android as well. However there's a fully working demo that includes a bluetooth wrapper around
BluetoothAdapter
. The wrapper is calledBleWrapper
and ships with the demo application called BLEDemo contained in the Application Accelerator package. Download here: https://developer.bluetooth.org/Pages/Bluetooth-Android-Developers.aspx. You need to register with your email address at the top right before downloading. The project's license allows for free use, code modification and publication.To my experience the Android demo application handles BLE notification subscriptions very well. I've not yet dived too much into the code to see how the wrapper actually wraps.
There's an Android app available in Play Store that is a customization of the Application accelerator demo. As the user interface looks nearly the same I suppose that it also uses
BleWrapper
. Download the app here: https://play.google.com/store/apps/details?id=com.macdom.ble.blescannerHere's a simple way to do it, but let me know if you see any drawbacks.
Step 1 Declare boolean variables
Step 2 subscribe to the first characteristic in the onServicesDiscovered callback:
Step 3
Subscribe to any others after the onCharacteristicChanged callback fires
I assume (you did not provide your source code) that you did not implement it as Google wanted:
(1)
and then
(2)
I suppose 2 is missing. In that case I believe on low-level notification will be triggered but they will never be reported to application layer.
This one is working for me:
to notify master device that some characteristic is change, call this function on your pheripheral:
in your master device: enable setCharacteristicNotification after discover the service:
now you can check your characteristic value is change, for example onCharacteristicRead function (this also working on onCharacteristicChanged function as well) :
It seems like you forgot to write the Descriptor which tells your BLE device to go in this mode. See the code lines that deal with descriptor at http://developer.android.com/guide/topics/connectivity/bluetooth-le.html#notification
Without setting this descriptor, you never receive updates to a characteristic. Calling
setCharacteristicNotification
is not enough. This is a common mistake.code snipped
@Boni2k - I have the same issues. In my case, I have 3 notifying characteristics and a handful of read/write characteristics.
What I did find is that there is some dependency between
writeGattDescriptor
andreadCharacteristic
. All of the writeGattDescriptors must come first and complete before you issue any readCharacteristic calls.Here is my solution using
Queues
. Now I am getting notifications and everything else works fine:Create two Queues like this:
Then write all of your descriptors immediately after discovery with this method:
and this callback:
The method for reading a characteristic normally then looks like this:
and my read callback: