How to get the battery level after connect to the BLE device?
Asked Answered
D

1

17

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?

Dutcher answered 23/10, 2013 at 10:45 Comment(6)
what value do you get?Quoin
the value of mBluetoothGatt.readCharacteristic(batteryLevel); is "true"Dutcher
sorry, I don't think I can help... maybe this would: developer.samsung.com/forum/board/thread/…Quoin
heyy...could u please tell me how to get UUID,Major and Minor id of my ble device beacon ??I use the native code for ble device discovery from android developer site..Pupillary
can you explain what this is? private static final UUID Battery_Service_UUID = UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb");Metaphrast
@MarianPaździoch That is the BLE Service UUID of Battery ServiceDutcher
D
29

I have solved this problem.

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 onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

        if(status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    }


    private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {

        final Intent intent = new Intent(action);   
        Log.v(TAG, "characteristic.getStringValue(0) = " + characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));
        intent.putExtra(DeviceControl.EXTRAS_DEVICE_BATTERY, characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));  
        sendBroadcast(intent);
    }

    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);
        Log.v(TAG, "batteryLevel = " + mBluetoothGatt.readCharacteristic(batteryLevel));
    }
}

When you call the function getbattery() , it will call onCharacteristicRead. and onCharacteristicRead will call broadcastUpdate 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.

Dutcher answered 31/10, 2013 at 8:20 Comment(8)
It gives battery level as true.Varanasi
@Dutcher @RajeshNarwal How did you get the UUID?Ppm
@M.S. Its a public service, defined in Bluetooth SIG site. See HereDysthymia
@Dysthymia Where is UUID number written on the page? I can only see hexadecimal 0x180F.Ppm
@Dysthymia Thank you. I wanted to know is UUID available online otherwise how would we find out base and battery level UUID?Ppm
The field name BluetoothGattCharacteristic batteryLevel is misleading here. It is not the actuall battery level...Neri
Can you share full project of Heartrate monitor and Battery in single ble serviceNakada
What if multiple BLE devices are connected? How can I know which device's battery level is being retrieved?Overglaze

© 2022 - 2024 — McMap. All rights reserved.