如何拿到电池后级连接BLE设备? [英] How to get the battery level after connect to the BLE device?

查看:657
本文介绍了如何拿到电池后级连接BLE设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序,其中我需要连接到蓝牙设备上的Andr​​oid 4.3。

I am developing an application where I have to connect to Bluetooth device on Android 4.3.

和我想通过获得电池电量 Battery_Service Battery_Level

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););
    }

但价值 mBluetoothGatt.readCharacteristic(batteryLevel); 是不是电池的电平值

But the value of mBluetoothGatt.readCharacteristic(batteryLevel); is not the battery level value

如何阅读电池????

How to read the battery ????

推荐答案

我解决这个问题。

    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 和传输特性和行动了。

When you call the function getbattery() , it will call onCharacteristicRead. and onCharacteristicRead will call broadcastUpdate and transmit the characteristic and action to it.

characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8,0)在broadcastUpdate是BLE设备的电池值。

And the characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0) in broadcastUpdate is the battery value of the BLE device.

这篇关于如何拿到电池后级连接BLE设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆