@Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "BroadcastReceiver-onRecieve"); String action = intent.getAction(); if (ACTION_USB_PERMISSION.equals(action)) { Log.i(TAG, "BroadcastReceiver-onRecieve- ACTION_USB_PERMISSION"); synchronized (this) { UsbAccessory accessory = UsbManager.getAccessory(intent); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { openAccessory(accessory); startService(); doBindService(); } else { Log.d(TAG, "permission denied for accessory " + accessory); } mPermissionRequestPending = false; } } else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) { Log.i(TAG, "BroadcastReceiver-onRecieve- ACTION_USB_ACCESSORY_DETACHED"); UsbAccessory accessory = UsbManager.getAccessory(intent); if (accessory != null && accessory.equals(mAccessory)) { closeAccessory(); } } }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ACTION_USB_PERMISSION.equals(action)) { synchronized (this) { UsbAccessory accessory = UsbManager.getAccessory(intent); Logger.d("onReceive accessory:" + accessory); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { openAccessory(accessory); } else { Logger.d("permission denied for accessory " + accessory); } mPermissionRequestPending = false; } } else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) { UsbAccessory accessory = UsbManager.getAccessory(intent); if (accessory != null && accessory.equals(mAccessory)) { closeAccessory(); } } }
private void openAccessory(UsbAccessory accessory) { Logger.d("openAccessory accessory:" + accessory.toString()); ParcelFileDescriptor fd = mUsbManager.openAccessory(accessory); Logger.d("openAccessory fd:" + fd); if (fd != null) { mAccessory = accessory; mUSBCon.start(fd, mUsbReceiveHandler); mRunning = true; } else { Logger.d("accessory open fail"); } }