コード例 #1
1
  /** Called when the activity is resumed from its paused state and immediately after onCreate(). */
  @Override
  public void onResume() {
    super.onResume();

    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");
    }
  }
コード例 #2
0
ファイル: AdkService.java プロジェクト: yokmama/adkworks
 private boolean connectDevice() {
   if (mRunning) {
     return true;
   } else {
     UsbAccessory[] accessories = mUsbManager.getAccessoryList();
     UsbAccessory accessory = (accessories == null ? null : accessories[0]);
     Logger.d("onResume accessory:" + accessory);
     if (accessory != null) {
       if (mUsbManager.hasPermission(accessory)) {
         Logger.d("onResume openAccessory START");
         openAccessory(accessory);
       } else {
         Logger.d("permission ERROR?");
         synchronized (mUsbManager) {
           if (!mPermissionRequestPending) {
             mUsbManager.requestPermission(accessory, mPermissionIntent);
             mPermissionRequestPending = true;
           }
         }
       }
       return true;
     } else {
       Logger.d("not attachd USB.");
       return false;
     }
   }
 }
コード例 #3
0
 @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();
     }
   }
 }
コード例 #4
0
ファイル: BaseActivity.java プロジェクト: hansys65/gast-lib
  /** @see android.app.Activity#onCreate(android.os.Bundle) */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    if (getLastNonConfigurationInstance() != null) {
      mAccessory = (UsbAccessory) getLastNonConfigurationInstance();
      openAccessory(mAccessory);
    }

    setContentView(R.layout.aoa_base);

    accessoryAttached(false);

    if (mAccessory != null) {
      showTemp();
    } else {
      hideTemp();
    }
  }
コード例 #5
0
ファイル: AdkService.java プロジェクト: yokmama/adkworks
 @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();
     }
   }
 }
コード例 #6
0
ファイル: AdkService.java プロジェクト: yokmama/adkworks
 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");
   }
 }
コード例 #7
0
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    setContentView(R.layout.main);
    adcValueTextView = (TextView) findViewById(R.id.adc_value_text_view);
    adcValueProgressBar = (ProgressBar) findViewById(R.id.adc_value_bar);
  }
コード例 #8
0
 private void openAccessory(UsbAccessory accessory) {
   mFileDescriptor = mUsbManager.openAccessory(accessory);
   if (mFileDescriptor != null) {
     mAccessory = accessory;
     FileDescriptor fd = mFileDescriptor.getFileDescriptor();
     mInputStream = new FileInputStream(fd);
     mOutputStream = new FileOutputStream(fd);
     Thread thread = new Thread(null, commRunnable, TAG);
     thread.start();
     Log.d(TAG, "accessory opened");
   } else {
     Log.d(TAG, "accessory open fail");
   }
 }
コード例 #9
0
ファイル: BaseActivity.java プロジェクト: hansys65/gast-lib
 /**
  * TODO
  *
  * @param accessory
  */
 private void openAccessory(UsbAccessory accessory) {
   mFileDescriptor = mUsbManager.openAccessory(accessory);
   if (mFileDescriptor != null) {
     mAccessory = accessory;
     FileDescriptor fd = mFileDescriptor.getFileDescriptor();
     mInputStream = new FileInputStream(fd);
     mOutputStream = new FileOutputStream(fd);
     Thread thread = new Thread(null, this, "AOATempSensor");
     thread.start();
     Log.d(TAG, "accessory opened");
     accessoryAttached(true);
   } else {
     Log.d(TAG, "accessory open fail");
   }
 }
コード例 #10
0
  public void prepareUsbConnection() {
    Log.d(TAG, "prepareUsbConnection");
    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);

    if (getLastNonConfigurationInstance() != null) {
      Log.d(TAG, "getLastnonConfigurationInstance()!=null");
      mAccessory = (UsbAccessory) getLastNonConfigurationInstance();
      openAccessory(mAccessory);
      startService();
      this.doBindService();
    }
  }
コード例 #11
0
ファイル: AdkService.java プロジェクト: yokmama/adkworks
 @Override
 public void onCreate() {
   super.onCreate();
   {
     try {
       mUsbManager = UsbManager.getInstance(this);
     } catch (Exception e) {
       Logger.e(
           "<uses-library android:name=\"com.android.future.usb.accessory\" />をマニフェストに追加してますか?",
           e);
     }
     mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
     IntentFilter filter = new IntentFilter();
     filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
     registerReceiver(mUsbReceiver, filter);
   }
   {
     mUSBCon = new USBConnection();
   }
 }
コード例 #12
0
 private void openAccessory(UsbAccessory accessory) {
   Log.d(TAG, "openAccessory");
   mFileDescriptor = mUsbManager.openAccessory(accessory);
   if (mFileDescriptor != null) {
     Log.d(TAG, "openAccessory- mFileDescriptor!=null");
     mAccessory = accessory;
     /* */
     FileDescriptor fd = mFileDescriptor.getFileDescriptor();
     mInputStream = new FileInputStream(fd);
     mOutputStream = new FileOutputStream(fd);
     /* */
     enableControls(true);
     Log.d(TAG, "openAccessory -2 ");
     if (mInputController != null) mInputController.accessoryAttached();
     if (mOutputController != null) mOutputController.accessoryAttached();
     this.enableControls(true);
   } else {
     Log.d(TAG, "accessory open fail");
   }
 }
コード例 #13
0
  /**
   * 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, Intent intent) {
    // 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);
      deviceManager = UsbManager.getInstance(context);

      // 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);

          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;
  }
コード例 #14
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);
              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)) {

          }
        }