26

For some reasons not related to the battery life, I need to disable the battery charging when a USB host is connected to my phone (a rooted Nexus 4). Taking a look around on the internet (link1, link2), it looks like I should modify a system file. I have found a series of interesting files located in /sys/class/power_supply/battery and /sys/class/power_supply/usb, that could be the right ones to be modified in order to achieve the usb charging disabling (e.g. /sys/class/power_supply/usb/present 0/1).

Has anyone tried something like that (control the usb charging via software and modify such system files)? Which is the best way to modify those system files? Should I use the adb shell or another tool?

I tried the "root explore" app in order to modify those files, however when I modified a file (after changing its permissions), it was automatically kept back to its original value.

6
  • 2
    While this may or may not contribute to the question, I'm curious as to why you want to disable USB charging.
    – dotVezz
    Commented Oct 11, 2013 at 12:39
  • What I have here is something more similar to an embedded system: a phone (USB ACCESSORY) and a piece of hardware (USB HOST), connected through USB. The phone battery supplies the attached hardware through a specific connection (Vdd). However, the USB HOST is supposed to supply (or charge) the phone through the VBUS line of the USB. What I want to avoid is a loop of charge between phone and attached hardware. Despite the attached hardware is the HOST, it is the Android phone that supplies the whole system. Hope my explanation was clear to you.
    – STiLLeN
    Commented Oct 11, 2013 at 12:55
  • 2
    /sys is a virtual file system where the kernel exposes knobs and switches to alter and look up kernel configuration. You can't change the file permissions. I guess your nexus phone is unlocked and rooted. The charging circuit is probably based on a Maxim chip that may be configured. There are altered kernels available which allow overriding of the detection for power charging. The reverse should also be possible. You may need to patch and recompile the kernel though.
    – ce4
    Commented Oct 11, 2013 at 13:19
  • 1
    Franco Kernel for the nexus 4 allows fast charging, sources are available. maybe you can have a look at it and figure out how to switch it off: github.com/franciscofranco
    – ce4
    Commented Oct 11, 2013 at 13:33
  • 1
    Here's the driver: github.com/franciscofranco/mako/blob/nightlies-4.3-JWR/drivers/…
    – ce4
    Commented Oct 11, 2013 at 22:06

10 Answers 10

8

I just tried and found that you need to modify a file:
sys/class/power_supply/usb/device/charge

its default content is '1' which means 'enable charging' you need to set it to '0' to 'disable charging'

$ su

$ echo 0 > /sys/class/power_supply/usb/device/charge

I tested on Nexus 4 and it's working successfully.

2
  • I can confirm that echo 0 > /sys/class/power_supply/battery/charging_enabled works for me, on a Sony Z1 Compact with CM11. Thanks so much! :)
    – seanlano
    Commented Oct 5, 2015 at 2:58
  • Please note that the /sys interface is virtual and does not persist reboots
    – Kevin
    Commented Mar 7, 2018 at 7:32
4
adb shell dumpsys battery set ac 0; dumpsys battery set usb 0;
2
  • These commands are interesting, but they don't really affect charging! You could also do dumpsys battery set level 42 and the Android will think it's 42% charged, regardless of real battery state. Do dumpsys battery reset to return to normal.
    – EvgEnZh
    Commented Nov 14, 2020 at 9:20
  • Also, these fake changes freeze reported battery state and get recorded in battery statistics, which might mess them up and result in wrong "time remaining" predictions.
    – EvgEnZh
    Commented Nov 14, 2020 at 9:30
2

I'm not sure if it will work with your particular phone's charger, but often with cables that transfer both power and data, there are separate cords inside the cable for power, and data.

I'm not especially interested in tearing up my phone charger to confirm my idea, but you could try stripping part of the cable, identifying and cutting the power cords inside the cable, and wrapping it back up with electric tape.

There is likely a way easier means of doing this, but I'm not aware of it.

2
  • There are 4 pins in USB generally - 2 for 5v power and 2 for data.
    – NoBugs
    Commented Jul 12, 2014 at 15:32
  • 1
    If you cut Vbus and Ground it will also stop the data lines from working. USB cable connection is detected from the power (see the A-connector, the power pins are slightly longer so they will connect first).
    – diidu
    Commented Feb 28, 2017 at 10:10
1

I'm pretty sure it's not something you can change in software since the would phone charge when plugged into a computers USB port. You'd probably need to get your hands dirty and short a connection or two on the phone itself.

1
  • Yes it can also be done via software. Look at this app for rooted Nexus 4 stopCharge
    – STiLLeN
    Commented Jun 11, 2014 at 20:00
1

You can use adb shell dumpsys battery set usb 0; // for disabling USB charging and

adb shell dumpsys battery set usb 1; // for enabling USB charging

1
  • 1
    This is easy, but unfortunately it does not disable charging. It only makes it look like it is disabled.
    – diidu
    Commented Feb 28, 2017 at 9:26
1

I asked my colleague, who is an expert in battery driver. On our Nut Pro 2, whose kernel version is 4.4.21, the node for battery charging on/off is: /sys/class/power_supply/battery/input_suspend.

1
  • Same here on my Xiaomi Redmi K20 Pro with 4.14.
    – hacker1024
    Commented Feb 9, 2021 at 3:33
1

you can always use a shottky diode between pin 1 with the blocking side towards the accessory. that way the pin one gets a 5v signal so communication works, but there will be NO current flowing from the accesory to your phone. or if you want it the other way just place the diode in the different direction.

0

You can resolve the issue in hardware by modifying the pins on the micro-USB connector side. One obvious way is to not connect Pin#1 (VBUS) and the other way is to ground Pin#4 (ID) to pretend your host (PC) is an OTG device. If you do that, you have to disconnect Pin#1 (VBUS) otherwise one, if not both, of your devices will get burned.

As for the command line solution, you just have to find the right (device dependent) /sys/... files to play with. But as already mentioned, you need write access and root permissions to change these, and whatever you do it will only last until next reboot or change of USB peripheral.

1
  • 2
    The hardware solution suggested is incorrect. Almost every Android phone out there is a USB device that gets the power supply from USB hosts (e.g. laptops), thus working in Accessory mode. If you cut VBUS line the USB data communication stops working. I have tested this myself. I would say the same for the other way (ID pin) even though I did not test it. You are right about the software solution, however not all Android phones have the /sys/... files necessary to do so (Nexus 4 yes, Nexus 5 no). Another solution should be the changing of the charging driver behaviour into the kernel.
    – STiLLeN
    Commented Jun 11, 2014 at 19:58
0

Similar to the op, my Host who is connected via USB is also connected as a dummy battery. Can't simply use USB cable and no battery, android won't boot, nor does usb provide enough current.

You cannot fix this in software as Android will use Vbus during boot. Unless you also want to hack bootloader code.

The charging circuit needs to be modified to avoid usb charging vs battery voltage loop.

I confirmed on Galaxy S3 it requires 5V bus to enable data (d+ d-) it's part of USB standard.

OTG could solve the problem electrically but then the host client rules are not correct. Also this phone is able to OTG and charge (i use y-cable + gamepad)

Edit reference: https://forum.xda-developers.com/showthread.php?t=1863303

1
  • Will update once I figure out charging circuit changes
    – Kevin
    Commented Mar 7, 2018 at 7:14
-1

No, It is not possible on devices which are not yet rooted. 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. I know you would be asking how do I do that: Here is the link for a similiar app:https://play.google.com/store/apps/details?id=com.jim2&hl=en

3
  • 1
    What has ADB_ENABLED got to do with USB charging? Does the app you link allow to disable USB charging?
    – Dan Hulme
    Commented May 12, 2014 at 10:54
  • The app kinda allows you to disable usb charging after you alter write secure settings. Commented May 12, 2014 at 16:30
  • "Kinda"? What are the steps to follow?
    – Dan Hulme
    Commented May 12, 2014 at 16:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .