I am developing an application where I have to connect to Bluetooth device on Android 4.3.
And I want to get the battery level by using Battery_Service and Battery_Level.
public class BluetoothLeService extends Service {
private static final UUID Battery_Service_UUID = UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb");
private static final UUID Battery_Level_UUID = UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb");
public void getbattery() {
BluetoothGattService batteryService = mBluetoothGatt.getService(Battery_Service_UUID);
if(batteryService == null) {
Log.d(TAG, "Battery service not found!");
return;
}
BluetoothGattCharacteristic batteryLevel = batteryService.getCharacteristic(Battery_Level_UUID);
if(batteryLevel == null) {
Log.d(TAG, "Battery level not found!");
return;
}
mBluetoothGatt.readCharacteristic(batteryLevel);
// What should I do that I can get the battery level ??
Log.d(TAG, "Battery level " + mBluetoothGatt.readCharacteristic(batteryLevel););
}
But the value of mBluetoothGatt.readCharacteristic(batteryLevel);
is not the battery level value
How to read the battery ????
I have solved this problem.
When you call the function
getbattery()
, it will callonCharacteristicRead
. andonCharacteristicRead
will callbroadcastUpdate
and transmit the characteristic and action to it.And the
characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0)
in broadcastUpdate is the battery value of the BLE device.