/** * Searches for the device and opens it if successful * * @return true, if connection was successful */ public boolean FindDevice() { _usbManager = (UsbManager) _context.getSystemService(Context.USB_SERVICE); HashMap<String, UsbDevice> deviceList = _usbManager.getDeviceList(); Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); _usbDevice = null; // Iterate all the available devices and find ours. while (deviceIterator.hasNext()) { UsbDevice device = deviceIterator.next(); if (device.getProductId() == _productId && device.getVendorId() == _vendorId) { _usbDevice = device; _deviceName = _usbDevice.getDeviceName(); } } if (_usbDevice == null) { Log("Cannot find the device. Did you forgot to plug it?"); Log(String.format("\t I search for VendorId: %s and ProductId: %s", _vendorId, _productId)); _connectionHandler.onDeviceNotFound(); return false; } // Create and intent and request a permission. PendingIntent mPermissionIntent = PendingIntent.getBroadcast(_context, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); _context.registerReceiver(mUsbReceiver, filter); _usbManager.requestPermission(_usbDevice, mPermissionIntent); Log("Found the device and requested permission."); return true; }
public static void authorize(Context svcContext, PendingIntent usbAuthPendingIntent) { androidUsbManager = ((UsbManager) svcContext.getSystemService(Context.USB_SERVICE)); if (!androidUsbManager.hasPermission(adkAccessory)) { androidUsbManager.requestPermission(adkAccessory, usbAuthPendingIntent); } }
public ZBCoordinator(Context context, String actionUsbPermission) { super(); UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE); HashMap<String, UsbDevice> deviceList = manager.getDeviceList(); Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); UsbDevice device = null; while (deviceIterator.hasNext()) { UsbDevice devi = deviceIterator.next(); // TODO Remove hardcoded IDs if (devi.getProductId() == 0x05dc && devi.getVendorId() == 0x16c0) device = devi; // TODO Make sure this is the ZBCoordinator using RAW Descriptors } PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(actionUsbPermission), 0); if (device != null && !manager.hasPermission(device)) manager.requestPermission(device, mPermissionIntent); if (device != null && manager.hasPermission(device)) { UsbInterface intf = device.getInterface(0); this.endpointInt = intf.getEndpoint(0); this.usbDeviceConnection = manager.openDevice(device); this.usbDeviceConnection.claimInterface(intf, false); this.canUse = true; } }
/** * Iterate over all attached USB devices and look for Crazyradio. If Crazyradio is found, request * permission. */ private static UsbDevice searchForCrazyradio(Context context, UsbManager usbManager) { UsbDevice device = null; HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList(); // Iterate over USB devices for (Entry<String, UsbDevice> e : deviceList.entrySet()) { Log.i( LOG_TAG, "String: " + e.getKey() + " " + e.getValue().getVendorId() + " " + e.getValue().getProductId()); if (isCrazyradio(e.getValue())) { device = e.getValue(); break; // stop after first matching device is found } } if (device != null && !usbManager.hasPermission(device)) { Log.d(LOG_TAG, "Request permission"); mPermissionIntent = PendingIntent.getBroadcast( context, 0, new Intent(context.getPackageName() + ".USB_PERMISSION"), 0); usbManager.requestPermission(device, mPermissionIntent); } else if (device != null && usbManager.hasPermission(device)) { Log.d(LOG_TAG, "Has permission"); } else { Log.d(LOG_TAG, "device == null"); } return device; }
@Override public void onResume() { super.onResume(); // Return if the streams are already initialized if (mInputStream != null && mOutputStream != null) { return; } // Initialize the streams UsbAccessory[] accessories = mUsbManager.getAccessoryList(); UsbAccessory accessory = (accessories == null ? null : accessories[0]); if (accessory != null) { if (mUsbManager.hasPermission(accessory)) { openAccessory(accessory); } else { synchronized (mUsbReceiver) { if (!mPermissionRequestPending) { mUsbManager.requestPermission(accessory, mPermissionIntent); mPermissionRequestPending = true; } } } } else { Log.d(TAG, "mAccessory is null"); } }
@Override protected void onResume() { super.onResume(); setNames(); // in case name changed, we also want the name to update on its button if (mInputStream != null && mOutputStream != null) { return; } UsbAccessory accessories[] = mUsbManager.getAccessoryList(); UsbAccessory accessory = (accessories == null ? null : accessories[0]); if (accessory != null) { if (mUsbManager.hasPermission(accessory)) { openAccessory(accessory); } else { synchronized (mUsbReceiver) { if (!mPermissionRequestPending) { mUsbManager.requestPermission(accessory, mPermissionIntent); mPermissionRequestPending = true; } } } } else { Log.d(TAG, "mAccessory is null"); debug.setText("didnt reopen accessory"); } }
@Override protected void onResume() { super.onResume(); HashMap<String, UsbDevice> deviceList = mManager.getDeviceList(); Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); while (deviceIterator.hasNext()) { UsbDevice device = deviceIterator.next(); if (device.getProductId() == PID && device.getVendorId() == VID) { if (mManager.hasPermission(device)) mManager.requestPermission(device, mPermissionIntent); break; } } }
public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) { synchronized (this) { UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (device != null) { // Log.d("1", "DEATTCHED-" + device); } } } // if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { synchronized (this) { UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { if (device != null) { // Log.d("1", "ATTACHED-" + device); } } else { PendingIntent mPermissionIntent; mPermissionIntent = PendingIntent.getBroadcast( MainActivity.this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT); mUsbManager.requestPermission(device, mPermissionIntent); } } } // if (ACTION_USB_PERMISSION.equals(action)) { synchronized (this) { UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { if (device != null) { // Log.d("1", "PERMISSION-" + device); } } } } }
/** * デバイスの接続通知。 * * @return null=正常、null以外=エラーメッセージ */ public String onAttach(UsbDevice device) { Log.d(TAG, "onAttach:" + device); usbDevice = device; if (usbDevice == null) { Log.e(TAG, "Not found USB Device."); return "Not found USB Device."; } if (usbManager.hasPermission(usbDevice)) { return onStart(usbDevice); } else { // デバイスの利用許可をユーザに求める。 // 結果は UsbReceiver.onReceive()にコールバック。 usbManager.requestPermission(usbDevice, permissionIntent); } return null; }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); sens = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); // mUsbManager = UsbManager.getInstance(this); mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED); registerReceiver(mUsbReceiver, filter); mAccessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); // mAccessory = UsbManager.getAccessory(intent); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.textView2); mUsbManager.requestPermission(mAccessory, mPermissionIntent); }
public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ACTION_USB_PERMISSION.equals(action)) { synchronized (this) { UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); mUsbManager.requestPermission(accessory, mPermissionIntent); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { if (accessory != null) { Open_USB_Port(); } } else { Log.d(TAG, "permission denied for accessory " + accessory); } } } }
public void initDevice() { try { // Get Usb Manager from Android manager = (UsbManager) context.getSystemService(Context.USB_SERVICE); sgfpLib = new JSGFPLib((UsbManager) context.getSystemService(Context.USB_SERVICE)); debugMessage("jnisgfplib version: " + sgfpLib.Version() + "\n"); mLed = false; long err = sgfpLib.Init(SGFDxDeviceName.SG_DEV_AUTO); if (err != SGFDxErrorCode.SGFDX_ERROR_NONE) { if (err == SGFDxErrorCode.SGFDX_ERROR_DEVICE_NOT_FOUND) { String message = "Either a fingerprint device is not attached or the attached fingerprint device is not supported."; debugMessage(message); // callbackContext.error(message); } else { String message = "Fingerprint device initialization failed!"; debugMessage(message); // callbackContext.error(message); } } else { UsbDevice usbDevice = sgfpLib.GetUsbDevice(); if (usbDevice == null) { String message = "SDU04P or SDU03P fingerprint sensor not found!"; debugMessage(message); // callbackContext.error(message); } // USB Permissions mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0); filter = new IntentFilter(); filter.addAction(ACTION_USB_PERMISSION); context.registerReceiver(mUsbReceiver, filter); manager.requestPermission(usbDevice, mPermissionIntent); debugMessage("jnisgfplib version: " + sgfpLib.Version() + "\n"); } // autoOn = new SGAutoOnEventNotifier(sgfpLib, this); // mAutoOnEnabled = false; } catch (Exception e) { debugMessage("initDevice() Error"); debugMessage("Error: " + e.getMessage()); } }
// 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; }
/** * Enables the * * @param context The context that the USB manager should register to * @return RETURN_CODES - the status of the enable request */ public RETURN_CODES enable(Context context) { // Grab the packageName to use for an attach Intent actionString = context.getPackageName() + ".action.USB_PERMISSION"; PendingIntent permissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(actionString), 0); // If the USB manager isn't already enabled if (enabled == false) { // Create a new filter with the package name (for the accessory // attach) try { Thread.sleep(500); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } IntentFilter filter = new IntentFilter(actionString); // Also add a few other actions to the intent... filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED); filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED); // and register the intent with the specified context context.registerReceiver(receiver, filter); UsbManager deviceManager = null; UsbAccessory[] accessories = null; UsbAccessory accessory = null; // Get a UsbManager object from the specified intent (only works for // v3.1+ devices) deviceManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); // If we were unable to get a UsbManager, return an error if (deviceManager == null) { return RETURN_CODES.DEVICE_MANAGER_IS_NULL; } // Get a list of all of the accessories from the UsbManager accessories = deviceManager.getAccessoryList(); // If the list of accessories is empty, then exit if (accessories == null) { return RETURN_CODES.ACCESSORIES_LIST_IS_EMPTY; } // Get the first accessory in the list (currently the Android OS // only // supports one accessory, so this is it) accessory = accessories[0]; // If the accessory isn't null, then let's try to attach to it. if (accessory != null) { // If we have permission to access the accessory, if (deviceManager.hasPermission(accessory)) { // Try to open a ParcelFileDescriptor by opening the // accessory parcelFileDescriptor = deviceManager.openAccessory(accessory); // try { // parcelFileDescriptor.close() ; // } catch (IOException e1) { // e1.printStackTrace(); // } // parcelFileDescriptor = deviceManager // .openAccessory(accessory); if (parcelFileDescriptor != null) { // Create a new read thread to handle reading data from // the accessory readThread = new ReadThread(parcelFileDescriptor); readThread.start(); deviceManager.requestPermission(accessory, permissionIntent); if (parcelFileDescriptor == null) { Log.d(TAG, "USBAccessoryManager:enable() parcelFileDescriptor == null"); return RETURN_CODES.FILE_DESCRIPTOR_WOULD_NOT_OPEN; } // Open the output file stream for writing data out to // the accessory outputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor()); if (outputStream == null) { Log.d(TAG, "USBAccessoryManager:enable() outputStream == null"); try { parcelFileDescriptor.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return RETURN_CODES.FILE_DESCRIPTOR_WOULD_NOT_OPEN; } Log.d(TAG, "USBAccessoryManager:enable() outputStream open"); // If the ParcelFileDescriptor was successfully opened, // mark the accessory as enabled and open enabled = true; open = true; handler .obtainMessage( what, new USBAccessoryManagerMessage( USBAccessoryManagerMessage.MessageType.READY, accessory)) .sendToTarget(); Log.d(TAG, "USBAccessoryManager:enable() device ready"); return RETURN_CODES.SUCCESS; } else { /* * If we weren't able to open the ParcelFileDescriptor, * then we will not be able to talk to the device. Due * to a bug in the Android v2.3.4 OS this situation may * occur if a user presses the "home" or "back" buttons * while an accessory is still attached. In this case * the attempt to close the ReadThread will fail if a * read() is in progress on the FileInputStream. This * results in the ParcelFileDescriptor not being freed * up for later access. A future attempt to connect to * the accessory (via reopening the app) will end up * having the openAccessory() request return null, * ending up in this section of code. */ return RETURN_CODES.FILE_DESCRIPTOR_WOULD_NOT_OPEN; } } else { /* * If we don't currently have permission to access the * accessory, then we need to request it. If we haven't * requested it already... */ if (permissionRequested == false) { // Then go ahead and request it... deviceManager.requestPermission(accessory, permissionIntent); permissionRequested = true; return RETURN_CODES.PERMISSION_PENDING; } } } return RETURN_CODES.ACCESSORIES_LIST_IS_EMPTY; } return RETURN_CODES.SUCCESS; }
/* * Request user permission. The response will be received in the BroadcastReceiver */ private void requestUserPermission() { PendingIntent mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); usbManager.requestPermission(device, mPendingIntent); }