我想在我的安卓设备中以编程方式连接和断开USB OTG(笔式驱动器,硬盘驱动器等)
对于USB,我在连接到PC时使用以下路径绑定/取消绑定USB
/sys/bus/usb/drivers/usb/bind or unbind
以同样的方式,我想为 OTG 做这件事。
如果您在设备上具有root访问权限,则可以使用我找到的几种方法。 在应用程序中,您需要调用以下内容:
String[] commandConfigValue0 = { "su", "-c", "echo 0 > /sys/bus/usb/drivers/usb/usb1/bConfigurationValue", };
String[] commandConfigValue0 = { "su", "-c", "echo 0 > /sys/bus/usb/drivers/usb/usb1/bConfigurationValue", };
String[] commandConfigValue1 = { "su", "-c", "echo 1 > /sys/bus/usb/drivers/usb/usb1/bConfigurationValue", };
String[] commandAuthorized0 = { "su", "-c", "echo 0 > /sys/bus/usb/drivers/usb/usb1/authorized", };
String[] commandAuthorized1 = { "su", "-c", "echo 1 > /sys/bus/usb/drivers/usb/usb1/authorized", };
Process p;
try {
p = Runtime.getRuntime().exec(commandAuthorized0);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
Log.d("MainActivity", "From unbind: " + in.toString());
// I don't think "rebinding" works here but if you listen for the usb disconnect you can then call this second runtime command to bind the internal usbhub
Thread.sleep(3000);
p = Runtime.getRuntime().exec(commandAuthorized1);
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
Log.d("MainActivity", "From bind: " + in.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我查看了一些我拥有的较新的设备,这些设备没有植根,它们也有/sys/bus/usb/drivers/usb/usb2,并且可能有带有/sys/bus/usb/drivers/usb/usb3 的设备。 因此,你可能必须运行多个调用,具体取决于你希望应用运行的设备。
如果您想在没有root访问权限的情况下执行此操作。 我也在努力寻找解决方案!
祝你好运!