@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();
     }
   }
 }
Exemple #2
0
 @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();
     }
   }
 }
        @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);
              deviceManager = UsbManager.getInstance(context);

              if (deviceManager == null) {
                // TODO: error. report to user?
                return;
              }

              // accessory = (UsbAccessory) intent
              //		.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
              accessory = UsbManager.getAccessory(intent);

              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);
            deviceManager = UsbManager.getInstance(context);

            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)) {

          }
        }