Android USB OTG: programmatically turn off power to the port

最后都变了- 提交于 2019-12-03 06:50:04

问题


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?


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/14151077/android-usb-otg-programmatically-turn-off-power-to-the-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!