Android USB device charging signals

How can I check if the USB connector is connected or not (charging with AC / USB)? (version for Android 2.1 / 2.2)

I am writing a wireless charging application that I want to receive for different types of charging signals (wireless, AC charging / USB). I used broadcastReceiver to receive a charging signal and found that using wireless charging and AC charging returns the same signal (AC charging). To distinguish between charging from the mains and charging from the AC, I want the USB connector to be connected or not. I am trying to use Intent.ACTION_UMS_CONNECTED to detect a USB signal, but it also returns an SD card.

How can I check if the USB connector is connected or not (charging with AC / USB)? (version for Android 2.1 / 2.2)

0
source share
2 answers

To check your device’s charge using AC / USB , try this,

Call registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)) . This will return an intent that has additional parameters defined on the BatteryManager so you know if it is connected or not.

Something with the code,

 public class PowerUtil { public static boolean isConnected(Context context) { Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB; } } 
+1
source

As of Jellybean 4.2 there is a BatterManager.BATTERY_PLUGGED_WIRELESS

+1
source

Source: https://habr.com/ru/post/1392716/


All Articles