0

I have the BleFindMeClient working on the HTC 1X+ in conjunction with the TI Mini Keyfob. I'm trying to extend it slightly to read the battery level (not register for battery updates). I can do it from the BTool, but my Android prog fails, and I get the debug message:

getCharacteristic - Service data not found

What does this mean? And where would I find out what this and other error messages mean?

Clearly I can write characteristics because I can set the alarm. But there is something rather basic that I have not grasped about reading characteristics, but I can't find example code.

Could someone throw me a better code fragment please, or spot something dumb in the following?

public class BatteryProfileClient extends BleClientProfile {

private static String TAG = "BatteryProfileClient";
static public BleGattID myUuid = new BleGattID("0000180f-0000-1000-8000-00805f9b34fb");
private static final BleGattID BATTERY_CHARACTERISTIC = new BleGattID("00002a19-0000-1000-8000-00805f9b34fb");

private BatteryServiceClient mBatteryServiceClient = new BatteryServiceClient();

public BatteryProfileClient(Context context) {
    super(context, myUuid);

    Log.d(TAG, "Instantiated");

    ArrayList<BleClientService> services = new ArrayList<BleClientService>();
    services.add(mBatteryServiceClient);

    init(services, null);
}


public void batt(BluetoothDevice device) {
    BleCharacteristic battLevelCharacteristic = mBatteryServiceClient.getCharacteristic(device, BATTERY_CHARACTERISTIC);
    byte b[] = battLevelCharacteristic.getValue();
    Log.d(TAG, "battery " + b); 
}

}

2 Answers2

0

Dont know if im late, but that should solve your problem -

public int getbattery(BluetoothGatt mBluetoothGatt) {

    BluetoothGattService batteryService = mBluetoothGatt
            .getService(BATTERY_SERVICE_UUID);
    if (batteryService == null) {
        Log.d(TAG, "Battery service not found!");
        return 0;
    }

    BluetoothGattCharacteristic batteryLevel = batteryService
            .getCharacteristic(BATTERY_LEVEL_UUID);
    if (batteryLevel == null) {
        Log.d(TAG, "Battery level not found!");
        return 0;
    }
    mBluetoothGatt.readCharacteristic(batteryLevel);
    return batteryLevel.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT8, 0);
}
crazyPixel
  • 2,301
  • 5
  • 24
  • 48
0

Visit https://stackoverflow.com/questions/19539535/how-to-get-the-battery-level-after-connect-to-the-ble-device

same problem use BluetoothGattCallback for callBack and read Battery Value

Community
  • 1
  • 1
Hamid-Ghasemi
  • 275
  • 2
  • 6