@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(intent.getAction())) { // a new USB device has been attached DbgLog.msg("USB Device attached; app restart may be needed"); } }
@Override public void onReceive(Context context, Intent intent) { /* get the action for this event */ String action = intent.getAction(); /* * if it corresponds to the packageName, then it was a permissions * grant request */ if (actionString.equals(action)) { /* see if we got permission */ if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { /* if so, then try to open the accessory */ UsbManager deviceManager = null; UsbAccessory accessory = null; deviceManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); if (deviceManager == null) { // TODO: error. report to user? return; } accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); parcelFileDescriptor = deviceManager.openAccessory(accessory); if (parcelFileDescriptor != null) { enabled = true; open = true; outputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor()); readThread = new ReadThread(parcelFileDescriptor); readThread.start(); Log.d(TAG, "USBAccessoryManager:BroadcastReceiver()-1"); handler .obtainMessage( what, new USBAccessoryManagerMessage( USBAccessoryManagerMessage.MessageType.READY, accessory)) .sendToTarget(); } else { // TODO: error. report to user? return; } } } if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) { /* * if it was a device attach notice, then try to open the * accessory */ UsbManager deviceManager = null; UsbAccessory[] accessories = null; UsbAccessory accessory = null; deviceManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); if (deviceManager == null) { // TODO: error. report to user? return; } accessories = deviceManager.getAccessoryList(); if (accessories == null) { // TODO: error. report to user? return; } accessory = accessories[0]; parcelFileDescriptor = deviceManager.openAccessory(accessory); if (parcelFileDescriptor != null) { enabled = true; open = true; outputStream = new FileOutputStream(parcelFileDescriptor.getFileDescriptor()); readThread = new ReadThread(parcelFileDescriptor); readThread.start(); Log.d(TAG, "USBAccessoryManager:BroadcastReceiver()-2"); handler .obtainMessage( what, new USBAccessoryManagerMessage( USBAccessoryManagerMessage.MessageType.READY, accessory)) .sendToTarget(); } else { // TODO: error. report to user? return; } } else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) { /* * if it was a detach notice, then close the accessory and * notify the user */ closeAccessory(); handler .obtainMessage( what, new USBAccessoryManagerMessage( USBAccessoryManagerMessage.MessageType.DISCONNECTED)) .sendToTarget(); } else if (UsbManager.EXTRA_PERMISSION_GRANTED.equals(action)) { } }