Not receiving data from BLE device

纵饮孤独 提交于 2019-12-03 20:36:29

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...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!