batteryStatus not displaying correctly if plugged into usb charger in a car

StackOverflow https://stackoverflow.com/questions/18587569

  •  27-06-2022
  •  | 
  •  

سؤال

I have an app that displays if an android device is being charged by a plug, a usb cable, a wireless device or not charging at all. I am using the code below, it will display the correct charging method (if any) until I charge my phone (has android 4.1) or tablet (has android 4.3) into a usb charger in a car. It will say it's charging by a plug, when it should say a usb device. What is the reason behind this?

        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = registerReceiver(null, filter);

        int chargeState = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);

        if(chargeState == BatteryManager.BATTERY_PLUGGED_USB)
        {
            chargingBy = "charging via usb cable";
        }
        else if (chargeState == BatteryManager.BATTERY_PLUGGED_AC)
        {
            chargingBy = "charging via plug";
        }
        else if (chargeState == BatteryManager.BATTERY_PLUGGED_WIRELESS)
        { 
            chargingBy = "charging via wireless device";
        }
        else
        {
            chargingBy = "not charging";
        }
هل كانت مفيدة؟

المحلول

From a hardware point of view there won't be any difference between an AC charger and a car charger so it's not surprising. It's quite a lengthy read but the full specifications for USB charging may be found here as a 1.3MB zip file:

Battery Charging v1.2 Spec and Adopters Agreement

Presumably BATTERY_PLUGGED_USB has been reserved for USB hosts whereas BATTERY_PLUGGED_AC will be anything identified as a charger only. Wireless charging would require hardware support within the phone / tablet so hardware capable of that feature would be able to detect when it's being used.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top