// Sets the current USB device and interface
  private boolean setAdbInterface(UsbDevice device, UsbInterface intf) {
    if (mDeviceConnection != null) {
      if (mInterface != null) {
        mDeviceConnection.releaseInterface(mInterface);
        mInterface = null;
      }
      mDeviceConnection.close();
      mDevice = null;
      mDeviceConnection = null;
    }

    if (device != null && intf != null) {
      UsbDeviceConnection connection = mManager.openDevice(device);
      if (connection != null) {
        log("open succeeded");
        if (connection.claimInterface(intf, false)) {
          log("claim interface succeeded");
          mDevice = device;
          mDeviceConnection = connection;
          mInterface = intf;
          mAdbDevice = new AdbDevice(this, mDeviceConnection, intf);
          log("call start");
          mAdbDevice.start();
          return true;
        } else {
          log("claim interface failed");
          connection.close();
        }
      } else {
        log("open failed");
      }
    }

    if (mDeviceConnection == null && mAdbDevice != null) {
      mAdbDevice.stop();
      mAdbDevice = null;
    }
    return false;
  }
예제 #2
0
  /**
   * デバイスの切断通知
   *
   * @return
   */
  public String onDetach(UsbDevice device) {
    Log.d(TAG, "onDetach:" + device);

    if (!device.equals(usbDevice)) {
      Log.d(TAG, "onDetach: Other device.");
      return "Other device";
    }

    if (usbConnection != null) {
      UsbInterface usbIf = usbDevice.getInterface(INTERFACE_INDEX);
      usbConnection.releaseInterface(usbIf);
      usbConnection.close();
    }
    usbConnection = null;
    usbDevice = null;
    isReady = false;
    return null;
  }
예제 #3
0
  /** Closes connections, releases resources, cleans up variables */
  private void destroy() {
    /* Release the interface that was previously claimed and close
     * the connection.
     */
    connection.releaseInterface(intf);
    connection.close();

    /* Clear up all of the locals */
    device = null;
    manager = null;
    handler = null;
    toggleLEDCount = 0;
    lastButtonState = false;
    buttonStatusInitialized = false;
    closeRequested = false;
    connection = null;
    intf = null;
  }
예제 #4
0
  /**
   * Constructor - creates connection to device and launches the thread that runs the actual demo.
   *
   * @param context Context requesting to run the demo.
   * @param device The USB device to attach to.
   * @param handler The Handler where demo Messages should be sent.
   */
  DemoCustomHID(Context context, UsbDevice device, Handler handler) {
    /* Save the device and handler information for later use. */
    this.device = device;
    this.handler = handler;

    /* Get the USB manager from the requesting context */
    this.manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);

    /*
     * Get the required interface from the USB device.  In this case
     * we are hard coding the interface number to 0.  In a dynamic example
     * the code could scan through the interfaces to find the right
     * interface.  In this case since we know the exact device we are connecting
     * to, we can hard code it.
     */
    intf = device.getInterface(0);

    /* Open a connection to the USB device */
    connection = manager.openDevice(device);

    if (connection == null) {
      return;
    }

    /* Claim the required interface to gain access to it */
    if (connection.claimInterface(intf, true) == true) {
      thread = new Thread(this);
      thread.start();
      connected = true;
    } else {
      /* if the interface claim failed, we should close the
       * connection and exit.
       */
      connection.close();
    }
  }
예제 #5
0
        public void run() {
          Log("USB Thread started");

          UsbEndpoint readEp = null;
          UsbDeviceConnection usbConnection = null;
          UsbInterface Intf = null;
          UsbInterface Intf0 = null;
          UsbEndpoint readEp0 = null;
          UsbEndpoint writeEp0 = null;
          byte[] readbuffer = new byte[64];

          Log("Entering Read loop");
          interfaceclaimed = false;
          while (!_shouldstop) {
            if (_usbDevice == null) {
              Log("No device. Open device and Recheck in 1 sec...");
              interfaceclaimed = false;
              FindDevice();
              Sleep(10000);
              continue;
            }
            if (!interfaceclaimed) {
              Log("Read loop Aquiring Interface 0 and Endpoint 1");
              Intf = _usbDevice.getInterface(1);
              readEp = Intf.getEndpoint(0);
              Log("Read loop Aquiring Interface 0 and Endpoint 0");
              Intf0 = _usbDevice.getInterface(0);
              readEp0 = Intf0.getEndpoint(0);
              writeEp0 = Intf0.getEndpoint(1);
              if (!_usbManager.getDeviceList().containsKey(_deviceName)) {
                Log("Failed to connect to the device. Retrying to acquire it.");
                FindDevice();
                if (!_usbManager.getDeviceList().containsKey(_deviceName)) {
                  Log("No device. Recheking in 10 sec...");
                  _connectionHandler.onDeviceNotFound();
                  Sleep(10000);
                  continue;
                }
              }

              try {

                usbConnection = _usbManager.openDevice(_usbDevice);

                if (usbConnection == null) {
                  Log(
                      "Cannot start reader because the user didn't gave me permissions or the device is not present. Retrying in 2 sec...");
                  _connectionHandler.onUSBPermissionError();
                  Sleep(2000);
                  continue;
                }

                Log("USB loop claiming interface");
                // Claim and lock the interface in the android system.
                if (usbConnection.claimInterface(Intf, true)
                    && usbConnection.claimInterface(Intf0, true)) {
                  Log("USB loop Interface claimed successful");
                  interfaceclaimed = true;
                  _connectionHandler.onUSBDeviceClaimed();
                }
              } catch (SecurityException e) {
                Log(
                    "Cannot start reader because the user didn't gave me permissions. Retrying in 2 sec...");
                interfaceclaimed = false;
                Sleep(2000);
                continue;
              }
            }
            /*
            int filed = readConnection.getFileDescriptor();
            int epaddress = readEp.getAddress();
            TeensyDeviceUsbNativeHelper nativehelper = new TeensyDeviceUsbNativeHelper(filed,64,epaddress);
            nativehelper.beginUrbReadLoop();
            byte[] buffer2 = new byte[64];
            nativehelper.readUrb(1000, buffer2);
            //_receivedQueue.add(buffer2);
              	Log(String.format("Message received of lengths %s and content: %s", 64, composeString(buffer2)));
            */
            synchronized (_writelocker) {
              // Queue a USBRequest to USB if any data is in queue
              if (_writeQueue.size() > 0) {
                byte[] writebuffer = _writeQueue.poll();
                Log(String.format("Writing to USB: %s", CAN_Frame.bytesToHexString(writebuffer)));
                // Log("USB loop sending bulk write request");
                if (usbConnection.bulkTransfer(writeEp0, writebuffer, 64, 100) < 0) {
                  // failed to transfer
                  Log("USB loop sending bulk write request FAILED");
                } // else Log("USB loop sending bulk write request SUCCEEDED");
              }
            }

            synchronized (_locker) {
              // Log("Read loop Waiting for requst to complete");
              if (usbConnection.bulkTransfer(readEp0, readbuffer, 64, 100) >= 0) {
                byte[] readbuffertrimmed = new byte[readbuffer[0]];
                System.arraycopy(readbuffer, 0, readbuffertrimmed, 0, readbuffer[0]);
                _receivedQueue.add(readbuffertrimmed);
                Log(String.format("Message received: %s", CAN_Frame.bytesToHexString(readbuffer)));
                _connectionHandler.onUSBRecieve();
              }
            }
            // Sleep for 10 ms to pause, so other thread can write data or anything.
            // As both read and write data methods lock each other - they cannot be run in parallel.
            // Looks like Android is not so smart in planning the threads, so we need to give it a
            // small time
            // to switch the thread context.
            // Sleep(10);
          }
          if (usbConnection != null) {
            usbConnection.releaseInterface(Intf);
            usbConnection.releaseInterface(Intf0);
            interfaceclaimed = false;
            usbConnection.close();
            _connectionHandler.onUsbStopped();
            Log("USB Thread ENDED!");
          }
        }