Problem description:
Each time wen I connect USB cable between PC and my mobile phone the battery automatically is charged. I want to use ADB protocol but I don't want to charge my battery during ADB connection. Is it possible to to turn off this charging? And ofcourse how can I do this?
Environment:
Mobile phone with Android os 4 and higher
I need only mention that my referential device working on android 5 so there is no /sys/class/power_supply/battery/force_usb_charging file
BEWARE: adb shell dumpsys battery unplug only MOCKS the battery status. I checked with a current indicator on my usb cable and this does not change the amount of flowing current at all so the battery is still charging.
It only changes what applications think about the state, so for example google play would not start updating if you have configured that it can only update when charging...
Changing the value of a file like /sys/devices/battery.XX/power_supply/battery/hv_charger_set to 0 does really work but its a different file for every device. For example, for my yotaphone 2 with a snapdragon 805 there is a file: /sys/devices/qpnp-charger-14/power_supply/battery/charging_enabled
So you could make a script that you can use in tasker(if you have root):
echo $1 > /sys/devices/qpnp-charger-14/power_supply/battery/charging_enabled
if you write that to an executable file /data/setcharging.sh then you could call the script like /data/setcharging.sh 0 to disable or
/data/setcharging.sh 1 to enable.
This works, I checked with my current metering usb cable, but it does not show that it works in the battery status icon. So you could use a combination of this and adb shell dumpsys battery unplug / reset to make it look better, but the problem is that then apps like tasker don't know whether you have ac power or not, so you can not put triggers for that when you've set it to unplugged...
One more thing, I also tested what it does in recovery (twrp) and during boot. And it turns out that during boot and in recovery, the charging just works. So its not like your device will end up dead if you forget to turn on charging again. You can always charge it in recovery mode and then boot up and change the value again (or change it while in recovery if your recovery gives access to adb).
Lastly, there are apps on the play store that are made exactly for this, but I would recommend getting such a current indicating usb-cable and checking if they really do what they say and are not just mocking.
You can make the system think it is not loading th battery using
adb shell dumpsys battery unplug
Refer to this good article :
https://stanfy.com/blog/android-shell-part-1-mocking-battery-status/
Try this for rooted device:
To enable battery Charging:
adb shell dumpsys battery set ac 1
adb shell dumpsys battery set usb 1
adb shell dumpsys battery set wireless 1
To disable battery Charging:
adb shell dumpsys battery set ac 0
adb shell dumpsys battery set usb 0
adb shell dumpsys battery set wireless 0
Probably it can be done via (root privileges):
/sys/devices/battery.XX/power_supply/battery/hv_charger_set
0 - Disable USB Charging
1 - Enable USB Charging
if android os version is more than 6 use unplug and reset to revert back.
adb shell dumpsys battery unplug
adb shell dumpsys battery reset
Copied from perfetto.dev
On rooted phones the power management IC driver allows to disconnect the USB charging while keeping the USB data link active. This feature is SoC-specific, is undocumented and not exposed through any HAL. For instance on a Pixel 2 this can be achieved running, as root: echo 1 > /sys/devices/soc/800f000.qcom,spmi/spmi-0/spmi0-02/800f000.qcom,spmi:qcom,pmi8998#2:qcom,qpnp-smb2/power_supply/battery/input_suspend. Note that in most devices the kernel USB driver holds a wakelock to keep the USB data link active, so the device will never fully suspend even when turning the screen off.
Related
Currently I'm working on an app which does Bluetooth scanning in background service.
I realized that the phones behaviour is different if I force the device to sleep mode by
adb shell dumpsys battery unplug
adb shell dumpsys deviceidle force-idle
or unplug the physical cable from the device.
I can't find information about that there is a difference. But I recognised a difference already on different devices.
Is there a way to put the device in a I-physical-unplug-and-put-screen-off-mode and still be able to debug with logcat?
Thank you very much.
I need to stop and then start (restart) my MainService when the user plugs in the phone's charger. To do this, I have a so-called GodService that registers a receiver for the ACTION_BATTERY_CHANGED intent, and then I restart MainService within that BroadcastReceiver.
I can post the code to that if necessary, but that seems to work fine, since, as soon as I start debugging I'm charging, and I can step through and see MainService get stopped and restarted.
My question is, how can I test this multiple times in a row (which I need to do), since I'll have to unplug the device, thus stopping my debugging session. I'm sure there must be a way to debug the charging port removal, but I must not be searching with the correct terms.
You can actually simulate the phone disconnect and reconnect via ADB. Here is the relevant code:
Prior to Android 6+:
Disconnect USB
$ adb shell dumpsys battery set usb 0
Re-connect USB
$ adb shell dumpsys battery set usb 1
For Android 6+:
Disconnect USB
$ adb shell dumpsys battery unplug
Re-connect USB
$ adb shell dumpsys battery reset
EDIT:
Found a nice tuturial on how to mock different battery statuses. https://stanfy.com/blog/android-shell-part-1-mocking-battery-status/
I want to measure the current consumption of my device while running different test cases. (Have a tool to measure current consumption).
I have automated the test cases using UIAutomator. But for running the script I have to connect the device via USB or WiFi. In both cases current consumption of the device gets higher.
Is there any method by which I can stop the current coming from USB and run the script?
I have tried:
adb shell dumpsys battery set usb 0
This stops the charging through USB but the current reading is still more, because I think device is still getting the current it only stops the battery charging.
Connect with ADB over wifi. Start a test case that doesn't do anything and measure current increase (consider doing this multiple times). That's your baseline. When you run the real test, however much the current increases above that is due to the test.
My problem is solved. I used:
adb shell
uiautomator runtest projectname.jar -c pkg.cls &
Because of &, the script keeps running even when I disconnect the USB cable
How to measure app power consumption when I connect my device with my pc?
I've read about batterystats from Android Studio. And I've followed the instructions and get a batterystats dumpsys. However, I can't not let my device consistently plugged on usb port because the result of dumpsys will have a missing part called "Estimated power use". Which is the most important part of the whole test because the app power consumption it contained.
I've tried using StopCharge app to interrupt phone charging. But the prerequisite is that the device need to get ROOT.
Other options like power tutor is outdated. Information out from the application can't quite fit my request(Android 6.0 or upper).
Is it possible to get the power consumption measurement of an application when it's charging or is my mind out of the roof?
Thank you in advance!
p.s the following is a sample of the method of Android Studio batterystats
Estimated power use (mAh)
Capacity: 2930, Computed drain: 43.9, actual drain: 58.6-87.9
Screen: 35.6
Unaccounted: 14.7 ( )
Uid u0a2618: 2.91 ( cpu=2.17 wake=0.657 wifi=0.00656 sensor=0.0732 )
I think when you plug, the battery stats are not very accurate.
There's two ways i usually do:
Connect the adb by wi-fi and run batterystats
In Android Studio go to preferences->Plugins and search for Wi-fi, this way you connect the device on USB, go to plugin and he do the wifi connection
Or if you don't wanna a new plugin
Run adb tcpip 5555 and adb connect 192.168.0.101:5555 if you already have usb access (More info.)
-
Use the Google Battery Historian:
In this link, download the project following the guidelines
In your adb, run adb shell dumpsys batterystats --reset to clear all the history
Use your App for some time
Go back on ADB an generate the log: adb bugreport > bugreport.txt
Then Upload the file to the Battery Historian site that you will host by following the instructions, and you are done.
Image of the Battery Historian:
I want to use my phones as a remote camera system that charges via a solar panel however I was wondering whether there is any way to turn the phone on automatically or remotely.
Thats to say, if the phone runs out of battery and turns off, is there anyway to turn it back on automatically or remotely?
If the phone is rooted, here is what you can do.
(Optional) Rename /system/bin/playlpm:
adb shell remount
adb shell mv /system/bin/playlpm /system/bin/playlpm-original
Then create a playlpm file locally and put the following:
#!/system/bin/sh
/system/bin/reboot
And push it to the target:
adb push playlpm /system/bin
Then give it a proper permission:
adb shell chmod 0755 /system/bin/playlpm
adb shell chown root.shell /system/bin/playlpm
Now, whenever phone is connected to a power supply, it is going to (re)boot automatically. That's, if its battery has drained it will boot up when a power will be available from solar panel.
Also see:
Auto Power on Android when the charger is Connected
Programmatically auto-power on the phone
Is there a way to auto-power on the phone
[root] NoMoarPowah!