¿Cómo obtener el nivel de la batería después de conectarse al dispositivo BLE?

Estoy desarrollando una aplicación donde tengo que conectarme al dispositivo Bluetooth en Android 4.3.

Y quiero obtener el nivel de la batería utilizando Battery_Service y 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);); } 

Pero el valor de mBluetoothGatt.readCharacteristic(batteryLevel); No es el valor del nivel de la batería

¿Cómo leer la batería ????

He resuelto este problema.

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

Cuando llama a la función getbattery() , llamará onCharacteristicRead . Y onCharacteristicRead llamará a broadcastUpdate y le transmitirá la característica y la acción.

Y la characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0) en broadcastUpdate es el valor de la batería del dispositivo BLE.

  • Android Bluetooth status 133 en onCharacteristicwrite
  • Cómo escribir consecutivos Característica rápida y estable para BLE en Android?
  • API de baja energía de Bluetooth en Java
  • Problema de emparejamiento de Bluetooth Low Energy (BLE) de Moto G
  • Vinculación mediante programación al dispositivo BLE en Android
  • Android BLE: ¿Identificar el tipo de característica?
  • Cómo borrar mediante programación la caché Bluetooth utilizando GattServer
  • Android BLE: onCharacteristicChanged nunca se dispara
  • "El recurso compartido Bluetooth ha dejado de funcionar" al realizar LeScan
  • ¿Cómo obtener RSSI sin conexión al dispositivo BLE?
  • Análisis BLE de Android en el servicio de fondo
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.