pro*_*m85 5 usb android usb-otg
我知道如何做以下事项:
所以最后我有一个UsbDevice,我有权读/写它.怎么从这里继续?
我可以打开设备并获得FileDescriptor如下内容:
UsbManager manager = (UsbManager) activity.getSystemService(Context.USB_SERVICE);
UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);
UsbDeviceConnection connection = manager.openDevice(device);
boolean interfaceClaimed = connection.claimInterface(intf, true);
int fileDescriptor = connection.getFileDescriptor();
Run Code Online (Sandbox Code Playgroud)
我FileDescriptor现在该如何使用它?或者我怎样才能访问USB设备上的文件?
编辑
android <6的解决方案:不要打开一个UsbDeviceConnection只是尝试找到USB设备路径.然而,你有问题要区分多个USB设备,不知道哪个路径属于哪个设备......
android> = 6的解决方案:使用存储访问框架,实际上我不知道它是如何工作的......
因为我和你一样困惑,所以我会选择使用命令的路线ls。
代码示例:
try {
Process process = Runtime.getRuntime().exec("ls /storage/UsbDriveA");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String listOfFiles = "";
String line;
while ((line = buffer.readLine()) != null) {
listOfFiles += line;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
lsno such file or directory如果挂载点不正确将会返回。
以下是制造商使用的许多安装点的列表:
try {
Process process = Runtime.getRuntime().exec("ls /storage/UsbDriveA");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String listOfFiles = "";
String line;
while ((line = buffer.readLine()) != null) {
listOfFiles += line;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
(基于我和其他 3 个人收集的设备,以及快速前往百思买测试最新旗舰产品的信息。我知道我唯一缺少的流行设备是位于中国的 Hawuei/Xioami 设备,这些设备在英国国家/地区越来越流行,但他们很可能使用上述之一。)
要找到正确的安装点,您将侦听no such file or directory并循环/重新执行exec()下一个安装点的语句。
完成所有操作后,您可以使用cat /storage/.../file.txt重定向轻松读取文件和写入:echo test > /storage/.../file.txt
希望这可以帮助