¿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 .
- Publicidad de Android 5.0 y BLE del servicio personalizado UUID
- Parámetros de conexión de baja energía de Bluetooth para Android, iOS y Win8
- API Bluetooth de Baja Energía para Jelly Bean 4.2
- BluetoothGatt writeCharacteristic devuelve false
- Seguridad de emparejamiento BLE
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 ????
- ¿Podemos convertir nuestro dispositivo Android en un faro?
- Android Bluetooth LE - BluetoothGatt - onNotify deja de recibir datos
- La publicidad de Bluetooth LE falla cuando se inicia y se detiene cíclicamente
- Bluetooth de baja energía para Samsung Galaxy S3 y TI CC2540 monitor de ritmo cardíaco
- Cómo conectar un dispositivo Android a un dispositivo iOS a través de BLE (Bluetooth Low Energy)
- Android BluetoothGatt - estado 133 - registro de devolución de llamada
- Soporte Bluetooth Smart (4.0) / GATT en Android 4.0?
- Notificaciones de proximidad BLE de Android
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.