【问题标题】:How to get the battery level after connect to the BLE device?连接BLE设备后如何获取电量?
【发布时间】:2013-11-01 14:27:16
【问题描述】:

我正在开发一个应用程序,我必须连接到 Android 4.3 上的蓝牙设备。

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

}

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

如何读取电池?

【问题讨论】:

  • 你得到了什么价值?
  • mBluetoothGatt.readCharacteristic(batteryLevel)的值;是“真的”
  • 抱歉,我想我帮不上忙……也许这样:developer.samsung.com/forum/board/thread/…
  • 嘿...你能告诉我如何获取我的 ble 设备信标的 UUID、Major 和 Minor id 吗?跨度>
  • 你能解释一下这是什么吗?私有静态最终 UUID Battery_Service_UUID = UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb");

标签: android bluetooth-lowenergy battery android-4.3-jelly-bean batterylevel


【解决方案1】:

我已经解决了这个问题。

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设备的电池值。

【讨论】:

  • 它给出的电池电量是真实的。
  • @Wun @RajeshNarwal 你是怎么得到UUID的?
  • @M.S.它是一种公共服务,在蓝牙 SIG 站点中定义。 See Here
  • @IronBlossom 页面上的 UUID 编号写在哪里?我只能看到十六进制的0x180F。
  • @IronBlossom 谢谢。我想知道 UUID 是否在线可用,否则我们如何找出基本和电池电量 UUID?
猜你喜欢
  • 1970-01-01
  • 2016-03-21
  • 2015-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多