Sahil
Sahil

Reputation: 468

How to get Bluetooth Headset battery level?

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.

Upvotes: 6

Views: 13634

Answers (5)

Reza
Reza

Reputation: 924

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

Upvotes: 0

MohitJadav
MohitJadav

Reputation: 954

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

Upvotes: 0

Kirill Martyuk
Kirill Martyuk

Reputation: 113

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
}

Upvotes: 6

MarkWang
MarkWang

Reputation: 301

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.

Upvotes: 1

hank15
hank15

Reputation: 151

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.

Upvotes: 3

Related Questions