How to get Bluetooth Headset battery level?
Asked Answered
V

5

6

I'm trying to build an app which gets battery level of currently connected Bluetooth headset. This app can be used on phones which don't have this functionality built-in.
While searching on stackoverflow, I found How to get Bluetooth Headset battery status in android this question. I got the currently connected Bluetooth headset using BluetoothProfile.HEADSET profile.
But in the device object of type BluetoothDevice I don't see any method or property to get battery level of Bluetooth Headset.
I can get the device name and isAudioConnected.

Viaticum answered 26/10, 2018 at 6:36 Comment(4)
possible duplicate #19540035Because
You need the headset to provide that information.Mufinella
How do I check whether the headset is providing that information or not? On play store there are apps which provide battery level. How do they work?Viaticum
@Viaticum Try batON play.google.com/store/apps/…Circumferential
C
6

I found a solution, but it only works on android 8 and above

I took this code from here

Kotlin

 fun getBatteryLevel(pairedDevice: BluetoothDevice?): Int {
    return pairedDevice?.let { bluetoothDevice ->
        (bluetoothDevice.javaClass.getMethod("getBatteryLevel"))
            .invoke(pairedDevice) as Int
    } ?: -1
}
Copolymerize answered 6/9, 2020 at 13:12 Comment(0)
K
3

If question is about Bluetooth HFP feature: HF indicators feature is optional for the both sides. If the both sides support it, BluetoothHeadset will broadcast BluetoothHeadset.ACTION_HF_INDICATORS_VALUE_CHANGED with BluetoothHeadset.EXTRA_HF_INDICATORS_IND_ID equal 2 (Battery Level) and BluetoothHeadset.EXTRA_HF_INDICATORS_IND_VALUE with scope 0..100. Do not remember Android version were it was implemented, you should check it.

Also battery level can be implemented in device using vendor specific HFP AT commands (especially for old handsfree devices) and maybe BLE.

Koran answered 26/10, 2018 at 14:46 Comment(0)
N
1

The first thing to register BroadcastReciver by "android.bluetooth.device.action.BATTERY_LEVEL_CHANGED"

and you can receive this action by the broadcast receiver then get extra data by "android.bluetooth.device.extra.BATTERY_LEVEL"

and if you want to trigger this action, you need to reconnect your Bluetooth device or Bluetooth device battery level happened to change.

Good luck for you.

Neodarwinism answered 18/12, 2019 at 4:47 Comment(0)
B
0

I am Able to achieve the handset battery Level in Java

   try {                      
          BluetoothDevice device = bluetoothAdapter.getRemoteDevice("Connected device ID");
          java.lang.reflect.Method method;
          method = device.getClass().getMethod("getBatteryLevel");
          int value = (int) method.invoke(device);
          result.success(value);
       } catch (Exception ex) {
          result.error("invalid_argument", "'deviceId' argument is required to be string", null);
          break;
       }
Bonaventura answered 1/3, 2022 at 6:43 Comment(0)
B
0

This is @Kirill Martyuk answer as an Extension variable

val BluetoothDevice.batteryLevel
    get() = this.let { device ->
        val method = device.javaClass.getMethod("getBatteryLevel")
        method.invoke(device) as Int?
      } ?: -1

Usage would be something like

val manager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?
val adapter = manager?.adapter
val devices = adapter?.bondedDevices.orEmpty()
devices.forEach { device ->
    Log.d("DEVICE_NAME", device.name)
    Log.d("CHARGE_LEVEL", device.batteryLevel.toString())
}
Boneyard answered 2/4, 2022 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.