imrich
imrich

Reputation: 383

What does Battery level state (0x2A1B) Bluetooth specification mean?

Battery Level State (0x2A1B) is not listed under Battery Service specification in Bluetooth docs, only the Battery Level (0x2A19) is. However, when reading the characteristic Battery Level State (0x2A1B) under Battery Service, the 0x2A1B characteristic is found and false is returned, instead of an error or null.

Found this example, where true is returned when reading the Battery Level (0x2A19, org.bluetooth.characteristic.battery_level), which is listed as one of the value fields in the description of Battery Level State (0x2A1B).

Battery Level State "includes" Battery Level, as per the specs:

Battery Level State Description

EDIT: When reading the Battery Level State (0x2A1B) using the Nordic Connect Android app, this is the Value: Invalid data syntax: (0x) 64, "d".

Questions:

  1. What Bluetooth Service does Battery Level State (0x2A1B) falls under? Is it the Battery Service, as false is successfully returned? Other StackOverflow question asks this of Battery Power State (0x2A1A), which is also listed under Battery Level State. How does this work?
  2. What does the false and true indicate as returned from reading the characteristics?

-

Background: I am trying to read battery level via an Android app from an nRF51822 module (based on Adafruit Bluefruit 32u4 LE).

Among discovered services and characteristics are Battery Service (0x180F) and Battery level state characteristic (0x2A1B). Also tried adding custom characteristic to read battery level, as per Adafruit tutorial but the newly added characteristic is not found by the Android app. However, reading the characteristic using AT commands works - the module returns an int.

I am reading the 0x2A1B characteristic within onServicesDiscovered. Other characteristics I can read successfully. Here is the code within the Android app, based on this thread:

static String BATTERY_SERVICE_UUID = "0000180f-0000-1000-8000-00805f9b34fb";
static String BATTERY_LEVEL_UUID = "00002a1b-0000-1000-8000-00805f9b34fb";

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        //Read the battery level
        getbattery(gatt);
    } 
}

void getbattery(BluetoothGatt mGatt) {
    BluetoothGattService batteryService = mGatt.getService(BATTERY_SERVICE_UUID);
    if(batteryService == null) {
        Log.i("BATTERYX", "Battery service not found!");
        return;
    }
    Log.i("BATTERYX", "Battery service found!");

    BluetoothGattCharacteristic batteryLevel = batteryService.getCharacteristic(BATTERY_LEVEL_UUID);
    if(batteryLevel == null) {
        Log.i("BATTERY", "Battery characteristic not found!");
        return;
    } Log.i("BATTERYX", "Battery characteristic found!");
    Log.i("BATTERYX", String.valueOf(mGatt.readCharacteristic(batteryLevel)));
}

Here is the full list of discovered services and characteristics.

## SERVICES
[{UUID=00001800-0000-1000-8000-00805f9b34fb, NAME=Service Unknown}, 
{UUID=00001801-0000-1000-8000-00805f9b34fb, NAME=Service Unknown}, 
{UUID=00001530-1212-efde-1523-785feabcd123, NAME=Service Unknown}, 
{UUID=0000180a-0000-1000-8000-00805f9b34fb, NAME=Service Unknown}, 
{UUID=6e400001-b5a3-f393-e0a9-e50e24dcca9e, NAME=SERIAL_TRANSFER_SERVICE_UUID}, 
{UUID=0000180f-0000-1000-8000-00805f9b34fb, NAME=BATTERY_SERVICE_UUID}]


## CHARACTERISTICS
[[{UUID=00002a00-0000-1000-8000-00805f9b34fb, NAME=Characteristic Unknown}, //Device
{UUID=00002a01-0000-1000-8000-00805f9b34fb, NAME=Characteristic Unknown},   //Appearance
{UUID=00002a04-0000-1000-8000-00805f9b34fb, NAME=Characteristic Unknown}],  //Peripheral Preferred Connection Parameters
[{UUID=00002a05-0000-1000-8000-00805f9b34fb, NAME=Characteristic Unknown}], //Service Changed

[{UUID=00001532-1212-efde-1523-785feabcd123, NAME=Characteristic Unknown}, 
{UUID=00001531-1212-efde-1523-785feabcd123, NAME=Characteristic Unknown}, 
{UUID=00001534-1212-efde-1523-785feabcd123, NAME=Characteristic Unknown}],

[{UUID=00002a29-0000-1000-8000-00805f9b34fb, NAME=Characteristic Unknown}, //Manufacturer Name String
{UUID=00002a24-0000-1000-8000-00805f9b34fb, NAME=Characteristic Unknown},  //Model Number String
{UUID=00002a28-0000-1000-8000-00805f9b34fb, NAME=Characteristic Unknown},  //Software Revision String
{UUID=00002a26-0000-1000-8000-00805f9b34fb, NAME=Characteristic Unknown},  //Firmware Revision String
{UUID=00002a27-0000-1000-8000-00805f9b34fb, NAME=Characteristic Unknown}], //Hardware Revision String

[{UUID=6e400003-b5a3-f393-e0a9-e50e24dcca9e, NAME=SERIAL_TRANSFER_CHARACTERISTIC}, 
{UUID=6e400002-b5a3-f393-e0a9-e50e24dcca9e, NAME=Characteristic Unknown}],

[{UUID=00002a1b-0000-1000-8000-00805f9b34fb, NAME=Characteristic Unknown}]] //Battery level state

Upvotes: 2

Views: 7953

Answers (1)

Artem
Artem

Reputation: 303

Battery Level State should gives you battery level form 0 to 64 (100) percents

Upvotes: 4

Related Questions