I\'m here again.
So, long story short: in my app I\'m trying to receive datas from my BLE device (tickr heart rate monitor: that) with the help of Android Samples (that)
You have a mBluetoothLeService.readCharacteristic(btGattCharacteristic)
call but no onCharacteristicRead(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, int status)
callback to receive the value. It's part of a BluetoothGattCallback.
My understanding of the process is that first you find a BluetoothDevice
one way or another -- probably by a BLE scan. You identify it e.g. by its name (with device.getName()
) or by the advertising data and connect to it with device.connectGatt(context, false/true, gattCallback)
.
Then in your callback you receive the connection status in onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
. If the state is BluetoothProfile.STATE_CONNECTED
you can discover the services with gatt.discoverServices()
. This will trigger onServicesDiscovered(BluetoothGatt gatt, int status)
where you'll get the available services by gatt.getServices()
and identify the correct service by its UUID and get its characteristics by service.getCharacteristics()
and again identify the correct characteristic by its UUID.
You will then read the characteristic with gatt.readCharacteristic( service.getCharacteristic(CHARACTERISTIC_UUID))
. This then triggers the onCharacteristicRead(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, int status)
callback. Here you'll check which characteristic you received (as things are asynchonous) with characteristic.getUuid()
and read its String value with characteristic.getStringValue(0)
or Float value with getFloatValue(0)
etc. depending on the data type.
It can be confusing because of the chain of asynchronous operations. However there's nice sample code for both the server and the client in here and more specifically the client code is in this file. They are related to this excellent video about BLE on Android by NewCircle which also explains the code a little bit.
The Android sample you are referring to might be a bit confusing as it also involves Activity/Service interaction and isn't purely about Bluetooth LE. Better to have a look the NewCircle video and sample project perhaps...