public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
   if (ACTION_USB_PERMISSION.equals(action)) {
     synchronized (this) {
       // mAccessory = UsbManager.getAccessory(intent);
       mAccessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
       if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
         if (mAccessory != null) {
           openAccessory(); // call method to set up accessory communication
         }
       } else {
         Log.d(TAG, "permission denied for accessory " + mAccessory);
       }
     }
   } else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
     // UsbAccessory accessory = UsbManager.getAccessory(intent);
     UsbAccessory accessory =
         (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
     if (accessory != null && accessory.equals(mAccessory)) {
       closeAccessory(); // call your method that cleans up and closes communication with the
                         // accessory
     } else {
       Context contextss = getApplicationContext();
       CharSequence texts = "Something went wrong.";
       int durations = Toast.LENGTH_LONG;
       Toast toasts = Toast.makeText(contextss, texts, durations);
       toasts.show();
       System.exit(10);
     }
   }
 }
示例#2
0
 @Override
 public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
   if (ACTION_USB_PERMISSION.equals(action)) {
     synchronized (this) {
       UsbAccessory accessory = intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
       if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
         openAccessory(accessory);
       } else {
         Log.d(TAG, "permission denied for accessory " + accessory);
       }
       mPermissionRequestPending = false;
     }
   } else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
     UsbAccessory accessory = intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
     if (accessory != null && accessory.equals(mAccessory)) {
       closeAccessory();
     }
   }
 }
示例#3
0
        @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)) {

          }
        }