private ReadPacket read(int numOfBytes) {
    byte[] readData = new byte[numOfBytes];
    int len = 0;
    try {
      //            UsbInterface mDataInterface = mDevice.getInterface(1);
      //            UsbEndpoint mReadEndpoint = mDataInterface.getEndpoint(1);
      //            byte[] mReadBuffer;
      //            mReadBuffer = new byte[16 * 1024];
      //
      //            int readAmt = Math.min(readData.length, mReadBuffer.length);
      //            synchronized (mReadBufferLock) {
      //
      //
      //                Log.d(TAG, "Read about to call bulk transfer.");
      //                if (len < 0) {
      //                    // This sucks: we get -1 on timeout, not 0 as preferred.
      //                    // We *should* use UsbRequest, except it has a bug/api oversight
      //                    // where there is no way to determine the number of bytes read
      //                    // in response :\ -- http://b.android.com/28023
      //                    if (IO_TIMEOUT == Integer.MAX_VALUE) {
      //                        // Hack: Special case "~infinite timeout" as an error.
      //                        len = -1;
      //                    }
      //                    len = 0;
      //                }
      //
      ////              System.arraycopy(mReadBuffer, 0, readData, 0, readAmt);
      //            }
      //            len = mConnection.bulkTransfer(mReadEndpoint, readData, readAmt, IO_TIMEOUT);

      len = mSerialDevice.getPorts().get(0).read(readData, IO_TIMEOUT);

      Log.d(TAG, "Read " + len + " byte(s) complete.");

      // Add a 100ms delay for when multiple write/reads are occurring in series
      Thread.sleep(100);

      // TODO: this debug code to print data of the read, should be removed after
      // finding the source of the reading issue
      String bytes = "";
      int readAmount = len;
      for (int i = 0; i < readAmount; i++) bytes += String.format("%02x", readData[i]) + " ";
      Log.d(TAG, "Read data: " + bytes);
      ////////////////////////////////////////////////////////////////////////////////////////

    } catch (Exception e) {
      Log.e(TAG, "Unable to read from serial device.", e);
    }
    byte[] data = Arrays.copyOfRange(readData, 0, len);
    return new ReadPacket(data);
  }
 private void writeCommand(int command) {
   byte[] packet = new PacketBuilder(command).compose();
   if (mSerialDevice != null) {
     try {
       //                UsbInterface mDataInterface = mDevice.getInterface(1);
       //                UsbEndpoint mWriteEndpoint = mDataInterface.getEndpoint(0);
       //                mConnection.bulkTransfer(mWriteEndpoint, packet, packet.length,
       // IO_TIMEOUT);
       mSerialDevice.getPorts().get(0).write(packet, IO_TIMEOUT);
     } catch (Exception e) {
       Log.e(TAG, "Unable to write to serial device.", e);
     }
   }
 }
 @TargetApi(21)
 private void scanLeDeviceLollipop(final boolean enable) {
   if (enable) {
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
       lollipopScanner = bluetooth_adapter.getBluetoothLeScanner();
     }
     if (lollipopScanner != null) {
       Log.d(TAG, "Starting scanner 21");
       // Stops scanning after a pre-defined scan period.
       mHandler.postDelayed(
           new Runnable() {
             @Override
             public void run() {
               is_scanning = false;
               if (bluetooth_adapter != null && bluetooth_adapter.isEnabled()) {
                 lollipopScanner.stopScan(mScanCallback);
               }
               invalidateOptionsMenu();
             }
           },
           SCAN_PERIOD);
       ScanSettings settings =
           new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
       is_scanning = true;
       lollipopScanner.startScan(null, settings, mScanCallback);
     } else {
       try {
         scanLeDevice(true);
       } catch (Exception e) {
         Log.e(TAG, "Failed to scan for ble device", e);
       }
     }
   } else {
     is_scanning = false;
     if (bluetooth_adapter != null && bluetooth_adapter.isEnabled()) {
       lollipopScanner.stopScan(mScanCallback);
     }
   }
   invalidateOptionsMenu();
 }