I am trying to write a Bluetooth LE application that accesses the Zephyr HxM Smart heart monitor. This monitor has several Bluetooth services, but I'm interested in the battery service, the heart rate service, and a special service that has activity and peak acceleration. There is one characteristic for each: battery level, (BAT), heart rate measurement (HR), and user measurement (CUS). HxM is updated approximately once per second.
I am doing this with a Galaxy S4 with Android 4.4.
It does not work properly from the documentation.
My initial approach was to do:
Read BAT Set notification for HR Set notification for CUS.
Then wait for the callbacks. Setting notification means calling
BluetoothGatt.setCharacteristicNotification(Characteristic char , boolean enabled)
(You can also make a notification for the BAT, however the specification does not require support for this. HxM, however, supports it.)
This did not work. I received BAT and notifications for HR, but not CUS. If I deleted the second step, I received notifications for CUS. I could not get both. (This indicates that I am reading the specifications correctly, so this is [probably] not a problem.)
I found some signs of problems with the fact that the Bluetooth stack for Android is synchronous, but there is no hard documentation . Then I tried the following:
Read BAT. Wait for the BAT reading, then set notification for HR, Get HR, then disable notification for HR, and start notification for CUS. Get CUS, then disable notification for CUS, and start notification for HR. And continue to loop.
I have a BTA, and thatβs it.
As a result of trial and error, I found the following works:
Read BAT. Wait for the BAT reading, then set notification for HR, Get HR, then start notification for CUS. Get CUS, then start notification for HR. And continue to loop.
(Same as above, but without turning off notifications.) Usually I get an HR read, then CUS for 200 ms. We can assume that they are from the same update. (There are no timestamps in the data that need to be short in order to be LE). In fact, the logic is more complicated, because timers are needed if expected reads are not included. This logic is much more complex (and more error prone) than my first attempt, which the documentation apparently says.
I contacted Zephyr and they say that HxM Smart has been extensively tested on Windows and will do simultaneous notifications as it should. There are also indications that it works as it should on iOS.
There is another problem that I do not understand. To receive notifications, you must enable local attributes for notifications with something like:
BluetoothGattDescriptor descriptor = characteristic .getDescriptor(UUID_CLIENT_CHARACTERISTIC_CONFIG); resSet = descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); resWrite = mBluetoothGatt.writeDescriptor(descriptor);
This parameter is for each parameter, and it needs to be done only once, when the characteristic is first obtained. Instead, I believe I should do this every time I set up notifications. Perhaps this simply causes a sufficient time delay for work. I dont know. This trial and error takes a lot of time. It would be nice to have a final statement on how this works.
It should be noted that for all calls that return a result, the result is true (success).
I apologize for the long statement. My question is:
I do not find the documentation I have to describe. Everything indicates that you are setting up notifications and expect callbacks. Is there any documentation, or is this a bug, or is it just a poor implementation? (or is this my mistake?) I would especially like to know where the documentation is for what I needed to do.
Secondly, there is another complication. I tried debugging in routines to see what the code actually does. When I get to the BluetoothGatt.class class, the source lines do not match what the debug stack says. Thus, I assume that S4 does not use standard Android. I do not know where to go from there. This was frustrating, and although I have something that seems to work, it is kludgy and almost certainly less reliable.
Thanks for any help.