0

I am having some difficulties with Bluetooth Low Energy on Android. I have closely done the guide I linked, as well as checked the full example code. I have a BLE device I need to connect to and retrieve data frames from. The documentation at one point dictates to

Discover/Enable service: Service UUID UUID1, Characteristic UUID UUID2

Once this has been executed correctly, the device should start streaming frames of 20 bytes formatted in a particular way.

Searching for the device, connecting to it and discovering services on it I have no problem with. But then I'm stuck. To get the services, BluetoothGatt's method getServices() is called. This returns a list of BluetoothGattService's, which on their part also contain a list of BluetoothGattCharacteristic's. Obviously the BluetoothGattService's UUID must be equal to provided UUID1, and BluetoothGattCharacteristic's UUID to UUID2. But I do not know how to 'enable' this service with certain characteristic.

My documentation also does not mention descriptors. I have checked and there is only one descriptor in the UUID2 characteristic. So now I have everything one could possibly need - Service UUID, Characteristic UUID and Descriptor... But how do I read the data?

jdepypere
  • 3,453
  • 6
  • 54
  • 84

1 Answers1

1

You can iterate over all found characteristics and get the BluetoothGattCharacteristic object with UUID1 and UUID2. Use "UUID.fromString()" to convert a string representing the UUID to a UUID object, which than can be used with ".equals" to compare with "characteristic.getUuid()".

If I've understood you correctly, you want to read some data of a characteristic. When you call "connectGatt" on your BluetoothDevice, you get an object of type "BluetoothGatt". Use this gatt object after discovering the service and characteristics to call "readCharacteristic()" on it, passing the desired BluetoothCharacteristic as argument.

I hope I could help and let me know if I should clarify my answer

Linard

Linard Arquint
  • 574
  • 3
  • 12
  • 32
  • Calling `readCharacteristic(characteristic)`, the `onCharacteristicRead()`-callback is unfortunately not called. – jdepypere Apr 17 '15 at 23:04
  • @jdepypere could you test if the callback is still not called when you call readCharacteristic(...) on the main thread? There are quite a few issues with calling BLE methods on background threads... http://stackoverflow.com/a/20507449/1990080 – Linard Arquint Apr 17 '15 at 23:06
  • I used [this method](http://stackoverflow.com/a/11125271/1319187) to run `mBluetoothGatt.readCharacteristic(characteristic)`, but the callback is still not called... – jdepypere Apr 17 '15 at 23:15
  • Good catch - it return false. What could cause this? – jdepypere Apr 17 '15 at 23:22
  • I've never seen that and the documentation is as always quite sparse... Have you checked, whether the characteristic allows reading? It might be useful to double check with the nRF Master Control Panel app: https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en – Linard Arquint Apr 17 '15 at 23:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75556/discussion-between-linard-arquint-and-jdepypere). – Linard Arquint Apr 17 '15 at 23:27