how to update the battery level for every 5seconds in ble in android
Solution 1:
If I well understood your question, you will need a Timer in order to check you battery level regularly.
For instance, you could use this code after starting your device control activity, maybe at the end of the onServiceConnected method :
please put the timer at the end of onServiceConnected() method of mServiceConnection object
Timer timer = new Timer("batteryTimer");
TimerTask task = new TimerTask() {
@Override
public void run() {
mBluetoothLeService.getBattery();
}
};
timer.scheduleAtFixedRate(task, 0, 5000);
And do not forget to call timer.cancel() when the activity is finishing.
And in the service, you could put something like that :
public void getBattery() {
if (mBluetoothGatt == null) {
Log.e(TAG, "lost connection");
}
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);
}
It is an example which would need to be modified but that gives you an idea on how to do it.
Somebody already did access to the battery value in the link below : How to get the battery level after connect to the BLE device?