javaandroidandroid-studiousbusb-otg

How can I disable and enable a USB OTG connection to power a hardware device on and off?


I am working on an android project where I use a phone to power an external device. Through a USB-OTG connection, I can power the external device.

I want to know if I can write a program on android studio where I can press a button in the app to toggle the power to this external device (turn it on and off).

I've seen it done before on an older Samsung Galaxy 3 phone (on a much older version of android) by updating data in a file; specifically this file:

/sys/devices/virtual/host_notify/usb_otg/mode

But I'm trying to write the software so it is more generic and can work on any new android device.

Is there a way to do what I need?


Solution

  • I've tried finding a generic way to do this but it doesn't seem possible because the only way I've seen it possible is by using shell commands to toggle exposed kernel bits through root files.

    I have concluded it's likely impossible to make generic code for this as most android phones root files are organised differently. So you need to discover the specific file each time you want to use a different phone for the software.

    I don't think it would be feasible for myself to create a database of different brands of phones root file structures because new phones come out all the time with new structures which would require too much maintenance on my behalf.