int waitForUSBPermission() { UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); if (usbListener == null) return -1; while (!usbListener.permissionGranted()) { SystemClock.sleep(100); } UsbDevice depthCamera = usbListener.getDevice(); UsbDeviceConnection con = manager.openDevice(depthCamera); return con.getFileDescriptor(); }
// return true if a device is found boolean askForUSBPermission() { UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); // register someone to listen for "permission granted" dialogs PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); usbListener = new InfiniTAMUSBPermissionListener(); registerReceiver(usbListener, filter); // get a ist of USB devices HashMap<String, UsbDevice> deviceList = manager.getDeviceList(); Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); // find the depth camera UsbDevice depthCamera = null; while (deviceIterator.hasNext()) { UsbDevice device = deviceIterator.next(); if ((device.getVendorId() == 0x1d27) && (device.getProductId() == 0x0600)) { depthCamera = device; } } if (depthCamera == null) { usbListener = null; return false; } if (manager.hasPermission(depthCamera)) { usbListener.setDeviceWithPermission(depthCamera); } else { manager.requestPermission(depthCamera, permissionIntent); } return true; }