Android Question [SOLVED] Detect wireless charging?

wes58

Active Member
Licensed User
Longtime User
Does anybody know how to detect when the phone is charging via wireless charger? Since i only have a wireless charger in my car, i want to detect when the phone is charging (only wireless!) and if it is, turn the bluetooth on. I know it is in BatteryManager - BatteryManager.BATTERY_PLUGGED_WIRELESS.

I had a look at PhoneEvents, and it has a boolean value for the charging/plugged-in status in the batterychanged event. It would be good if we could find out what is the source - AC, USB or wireless, i.e. get an integer value from the BatteryManager.
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Try this:
B4X:
Public Sub GetBatteryWirelessCharging As Boolean
    Dim Context As JavaObject
    Context.InitializeContext
    Dim BatteryManager As JavaObject = Context.RunMethod("getSystemService",Array("batterymanager"))
 
    Dim IntentFilter As JavaObject
    IntentFilter.InitializeNewInstance("android.content.IntentFilter",Array("android.intent.action.BATTERY_CHANGED"))
 
    Dim BatteryStatus As Intent = Context.RunMethod("registerReceiver",Array(Null,IntentFilter))
 
'    Dim Status As Int = BatteryStatus.GetExtra(BatteryManager.GetField("EXTRA_STATUS"))
    Dim ChargePlug As Int = BatteryStatus.GetExtra(BatteryManager.GetField("EXTRA_PLUGGED"))
'    Dim USBCharge As Boolean = ChargePlug = BatteryManager.GetField("BATTERY_PLUGGED_USB")
'    Dim ACCharge As Boolean = ChargePlug = BatteryManager.GetField("BATTERY_PLUGGED_AC")
'    Dim WirelessCharge As Boolean = BatteryManager.GetField("BATTERY_PLUGGED_WIRELESS")
' 
    Return ChargePlug =  BatteryManager.GetField("BATTERY_PLUGGED_WIRELESS")
        '2 = USB , 4 = Wireless
End Sub

I have left the others commented as I don't have a wireless charger and used them to test.

From Here: https://developer.android.com/training/monitoring-device-state/battery-monitoring.html
 
Last edited:
Upvote 0

wes58

Active Member
Licensed User
Longtime User
Try this:
B4X:
Public Sub GetBatteryWirelessCharging As Boolean
    Dim Context As JavaObject
    Context.InitializeContext
    Dim BatteryManager As JavaObject = Context.RunMethod("getSystemService",Array("batterymanager"))

    Dim IntentFilter As JavaObject
    IntentFilter.InitializeNewInstance("android.content.IntentFilter",Array("android.intent.action.BATTERY_CHANGED"))

    Dim BatteryStatus As Intent = Context.RunMethod("registerReceiver",Array(Null,IntentFilter))

'    Dim Status As Int = BatteryStatus.GetExtra(BatteryManager.GetField("EXTRA_STATUS"))
    Dim ChargePlug As Int = BatteryStatus.GetExtra(BatteryManager.GetField("EXTRA_PLUGGED"))
'    Dim USBCharge As Boolean = ChargePlug = BatteryManager.GetField("BATTERY_PLUGGED_USB")
'    Dim ACCharge As Boolean = ChargePlug = BatteryManager.GetField("BATTERY_PLUGGED_AC")
'    Dim WirelessCharge As Boolean = BatteryManager.GetField("BATTERY_PLUGGED_WIRELESS")
'
    Return ChargePlug =  BatteryManager.GetField("BATTERY_PLUGGED_WIRELESS")
        '2 = USB , 4 = Wireless
End Sub

I have left the others commented as I don't have a wireless charger and used them to test.

From Here: https://developer.android.com/training/monitoring-device-state/battery-monitoring.html

Thanks for your help.
I have found an easier solution using PhoneEvents BatteryChanged event. Here is the code:
B4X:
Sub PE_BatteryChanged (Level As Int, scale1 As Int, Plugged As Boolean, Intent As Intent)
    If Plugged <> flagPlugged Then ' = True Then
        flagPlugged = Plugged
        If Plugged = True Then
            Dim chargeMethod As Int
            chargeMethod = Intent.GetExtra("plugged")
            'chargeMethod for AC = 1, USB = 2, wireless charging = 4
            Log("Charge Method: " & chargeMethod)
            If chargMethod = 4 then    
                'change profile to car mode
                'add code for switching bluetooth on
            End If      
        else
           'switch back to previous profile
           'add code to switch bluetooth off
        End If
    End If
End Sub
 
Last edited:
Upvote 0
Top