连接到BLE设备后如何获取电池电量?

17

我正在开发一个应用程序,需要在Android 4.3上连接蓝牙设备。

我想通过使用Battery_ServiceBattery_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););
    }

}

但是mBluetoothGatt.readCharacteristic(batteryLevel);的值并不是电池电量值。

如何读取电池电量?


你得到了什么值? - Tim Tisdall
mBluetoothGatt.readCharacteristic(batteryLevel) 的返回值为 "true"。 - Wun
抱歉,我不认为我能帮忙...也许这个链接可以:http://developer.samsung.com/forum/board/thread/view.do?boardName=SDK&messageId=240110 - Tim Tisdall
嘿...你能告诉我如何获取我的BLE设备信标的UUID、Major和Minor ID吗?我使用来自Android开发者网站的本地代码进行BLE设备发现。 - Nevaeh
你能解释一下这是什么吗?private static final UUID Battery_Service_UUID = UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb"); - Marian Paździoch
@MarianPaździoch 这是电池服务的BLE服务UUID。 - Wun
1个回答

29

我已经解决了这个问题。

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));
    }
}
当您调用函数getbattery()时,它将调用onCharacteristicRead。并且onCharacteristicRead将调用broadcastUpdate并将特征和动作传输给它。
而在broadcastUpdate中的characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0)是BLE设备的电池值。

2
它会返回电池电量的真实值。 - Rajesh Narwal
@Wun @RajeshNarwal 你们是如何获取 UUID 的? - Malwinder Singh
@M.S. 这是一个公共服务,定义在 Bluetooth SIG 网站上。 请点击此处查看 - IronBlossom
1
@IronBlossom 谢谢。我想知道UUID是否可以在线获取,否则我们如何找到基础和电池级别的UUID? - Malwinder Singh
你能分享心率监测器和电池在单个BLE服务中的完整项目吗? - Shankar
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接