Java anroid两次打开UsbDeviceConnection在同一时间打开

时间:2019-02-19 07:11:42

标签: java android

我的设备有两个USB端口,其中一个已连接并读取名称(这是GPS),在第二个端口上,我连接了USB存储设备,我想读取其中的文件。我只能在设备的一个存储上执行以下读取:

Map<String, File> externalLocations = ExternalStorage.getAllStorageLocations();

File externalSdCard = externalLocations.get(ExternalStorage.EXTERNAL_SD_CARD);
if (externalSdCard != null && externalSdCard.exists() && externalSdCard.listFiles() != null) {
    for (File f : externalSdCard.listFiles()) {
        if ("key.xml".equalsIgnoreCase(f.getName())) {
            String path = f.getPath().replace(".xml", "_DUMP.xml");
            File file11 = new File(path);
            if (!file11.exists()) {
                moveFile(file11, f);
            }
            return f;
        }
    }
}

我想我应该这样做来读取文件:

UsbManager usbManager = ((UsbManager)(GetSystemService(Context.UsbService)));
var deviceList = usbManager.DeviceList;
IEnumerable<UsbDevice> deviceIterator = deviceList.Values.AsEnumerable();
if ((deviceIterator.Count() > 0)) {
    UsbDevice device = deviceIterator.ElementAt(0);
    usbManager.RequestPermission(device, mPermissionIntent);
    if (usbManager.HasPermission(device)) {
        if (usbManager.HasPermission(device)) {
            System.Diagnostics.Debug.WriteLine("HasPermission");
            UsbDeviceConnection connection = usbManager.OpenDevice(device);
            if ((connection != null)) {
                System.Diagnostics.Debug.WriteLine(" connection is not null");
                var model = device.DeviceName;
                string inline = null;
                BufferedReader reader = null;
                reader = new BufferedReader(new FileReader("/mnt/media_rw/C227-875D/test/hello.txt"));
                System.Diagnostics.Debug.WriteLine("no exception occured");
                while ((reader.ReadLine() != null)) {
                    System.Diagnostics.Debug.WriteLine(inlne);
                }

            }

        }

但是我可以同时打开两个端口吗?

以及如何获得:C227-875D:"/mnt/media_rw/random-hex-id"

0 个答案:

没有答案