26

Suppose I have a USB-powered LED lamp connected through a USB OTG adapter to a Toshiba Excite 10 tablet running Android 4.0.3, which provides the power.

I want to programmatically turn the USB OTG host port's power pins on and off from an app. (Ideally without root, but I'll take what I can get). Any ideas?

AlcubierreDrive
  • 3,654
  • 2
  • 29
  • 45
  • Not sure if my kernel is compatible but it looks like I might just need to write to the file `/sys/bus/usb/devices/usb3/power/level`. I tried this without root but the write fails, so next I guess I need to root my tablet and try again. Source: http://stackoverflow.com/questions/4702216/controlling-a-usb-power-supply-on-off-with-linux – AlcubierreDrive Jan 04 '13 at 04:49

2 Answers2

4

You must have write permission on /sys directory. Rooting makes the process very easy. Just root your device and issue the right echo command to toggle kernel status bits like :

String[] command = { "/system/bin/sh", "-c", "echo xxx > /sys/bus/usb/devices/usb3/power/level", };
Runtime.getRuntime().exec(command);

Another way is to use libusb, dig the library for the function that cuts the power off. You can use android.hardware.usb.UsbManager to request proper permission for libusb.

Dr. Ehsan Ali
  • 4,735
  • 4
  • 23
  • 37
  • I want to trigger usb inserted(android tablet) even though usb is already inserted. Is it possible? Is there any way to tell system the usb is inserted, so it will be detected again? – Tushar Thakur Sep 21 '15 at 05:43
  • for kernels newer than 2.6.32 "suspend" for .../usbX/power/control doesn't work anymore https://android.googlesource.com/kernel/mediatek/+/android-3.18/Documentation/usb/power-management.txt – user724861 Mar 18 '21 at 08:15
2

As I know from here https://askubuntu.com/questions/342061/power-on-off-usb-ports "All of the previous answers talk about a USB suspend mechanism, i.e. a "logical power-off", they will never physically cut the VBUS +5V from the USB port.".

Maybe a hardware solution is bad idea, but I can advise you to use MOSFET transistor with programmed Atmega8U4 chip on PCB, that is programmed to cutoff power to led lamp.

cs95
  • 379,657
  • 97
  • 704
  • 746