react-native-device-info icon indicating copy to clipboard operation
react-native-device-info copied to clipboard

isBatteryCharging returning false while charging from a powerbank

Open Kyiomi opened this issue 1 year ago • 3 comments

Hi, I have integrated react-native-device-info into my Expo app. I need to get the charging status of my phone. When the phone is disconnected, it correctly returns false. When the phone is plugged into a socket it also correctly returns true. BUT when plugged and charging from powerbank (phone showing a little lighting icon inside battery and charging) it returns false.

Summary

Physical Xiaomi Redmi 13C Android 13 react-native-device-info: 11.1.0

Current behavior

When charging from powerbank, isBatteryCharging is returning false.

Expected behavior

isBatteryCharging should return true when charging from powerbank.

Kyiomi avatar Sep 09 '24 07:09 Kyiomi

Here you can see we ask the system for battery status, and pull the extra constant that is supposed to tell us what the charge state is

https://github.com/react-native-device-info/react-native-device-info/blob/1c85272a1714dfb41599ebc00557fb2e0bd5c45e/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java#L518-L527

We are looking for this constant which should have integer value 2 https://developer.android.com/reference/android/os/BatteryManager#BATTERY_STATUS_CHARGING

You'll need to see what value is actually returned. Xioami is notorious for corrupting their implementation of Android with all sorts of quirks, perhaps they have something different.

Or perhaps the USB is not powerful enough to actually charge and despite being plugged in you are slowly discharging or not charging, or "unknown" - each have their own constant

Analyzing that return value by modifying the code locally and retrying while logging out the result, or perhaps using our example app here on your device, should be informative

mikehardy avatar Sep 12 '24 16:09 mikehardy

Just tested it on my own Xiaomi Mi Powerbank 2S (model PLM09ZM) and my own phone Xiaomi Redmi Note 12 Pro 5G and it seems to be working correctly. Simply the returned value is correct.

Since we have encountered this wierd behaviour during our airsoft event where we used these phones for capture points, everything was outside quite a distance away, so when I saw that the battery level did not drop from 1 at all, I did not bother to go and have a look why this could happen. The phones were constantly turned on for the whole night.

Anyway I want to also test it on the exact same hardware we used back then. I should be able to pick it up some time next week and do some more tests because right now it seems like a hardware issue for me.

Thanks for your time. I will be in touch later.

Kyiomi avatar Sep 13 '24 12:09 Kyiomi

I'm going to guess it was https://developer.android.com/reference/android/os/BatteryManager#BATTERY_STATUS_FULL instead of BATTERY_STATUS_CHARGING, so it was not technically charging - it was fully charged :-)

the combo of "battery 100%" and "not charging" plus power state of "plugged in" I guess says "hey, everything is better than okay, it is as good as it can be" ?

mikehardy avatar Sep 13 '24 18:09 mikehardy

Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is sti ll here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solu tions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

rndi-bot avatar Nov 12 '24 18:11 rndi-bot

Oh sorry for the late reply, totally forgot about that. Yea you were right, the device is not technically charging when it's on 100%, so i think this is correct behaviour. Closing this issue, thanks for help.

Kyiomi avatar Nov 12 '24 18:11 Kyiomi