How to stop the phone from charging via USB programmatically
Asked Answered
F

6

16

I've tried to do as much research as possible but can't find an answer to this fairly simple question (want to figure this out before I'm going to set up the SDK and everything).I'm thinking about developing my first app and am wondering whether the BATTERY_STATUS_CHARGING from the BatteryManager contains only a get function or also a set function.

I wan't to make an app in which I can manually stop the phone from charging without unplugging it from the charger (via USB) and so am wondering whether this is actually possible.

Faunie answered 13/4, 2013 at 22:27 Comment(1)
This could have answered your question: #6654689Captivate
K
10

No, Its not possible on unrooted devices. It uses Settings.System with ADB_ENABLED, but ADB_ENABLED is a Secure-System-Setting which can not be changed by a application.

Yes, It can be done on rooted phone.

There is need to add an permission android.permission.WRITE_SECURE_SETTINGS.

Same type of application on Google Play : https://play.google.com/store/apps/details?id=com.jim2&hl=en

Keeping answered 13/4, 2013 at 23:13 Comment(0)
S
7

You can hardwire it: 12 ohms in serial still makes the phone be recognized by the computer, yet slowly discharge. See attached Picture.microUSB cable plugged into PCB. PCB shows VCC connected to wire via 12Ω resistor, and D-, D+ and GND connected directly to wires. All wires go to cable out of image

Shyster answered 12/11, 2015 at 15:13 Comment(1)
Way too low resistance. That resistor gets smoking hot! Try 220 ohm. Though if you're trying to protect your battery then this hack may not work regardless of current. Better to turn off charging in the phone OS if you can. If you run something like LineageOS then you can edit the kernel source and disable charging by default and/or turn it on or off programmatically as the OP wants.Dyne
D
7

I know this thread is old but posting my ans anyways for someone reaching here later.. Try this:-
echo 0 > /sys/class/power_supply/batterycharging_enabled/charging_enabled

Daze answered 1/6, 2017 at 22:30 Comment(2)
In my phone this file was there: /sys/class/power_supply/battery/battery_charging_enabled - updating content of this file to 0 stopped charging (based on information from AccuBatery and from LED). But I'm curious if this is safe. I don't know if charger really stopped providing currency or just software is "thinking" that it's not charged.Anguish
@dey: It depends on the implementation of your phone. On both my Xperia X Performance (API Level 26) and Xperia Ray (API Level 15), there is no power draw once the corresponding file has been updated. However, the X Performance can stay for hours without losing charge while the Xperia Ray's batter loses charge quite quickly and has to be recharged (by changing the value) every now and then.Eirena
G
3

This is unfortunately not possible. Android only have read APIs for battery data. ADB_ENABLED will not help in this instance either as it's only to enable debugging over USB.

Battery charging control is internal to the Android kernel and battery IC modules. Manufacturers do change it from time to time, but it's not available to apps without root and without controlling the IC (integrated chip) modules through some tricky interface which would actually be different for almost every device out there!

At best if we're lucky, a sysfs interface might help do that, but again it requires root and may differ from device to device.

Gloriane answered 6/10, 2013 at 5:39 Comment(0)
D
0

Use the below command to stop the charging of mobile/handheld when connected to USB:

adb shell dumpsys battery set ac 0; dumpsys battery set usb 0;

Derisible answered 24/4, 2019 at 5:44 Comment(0)
J
0

Use the below command to stop charging on Android 11. device need to be rooted.

I'm using Termux but Linux Deploy can be also be used.

sudo nano /sys/class/power_supply/battery/charger.0/stop_charge

Change value to 1 to stop and 0 to start again

Joiner answered 25/10, 2022 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.