Beispiel #1
0
  public String Open_USB_Port() {
    List<UsbSerialDriver> availableDrivers =
        UsbSerialProber.getDefaultProber().findAllDrivers(mUsbManager);
    String message = "null";
    if (availableDrivers.isEmpty()) {
      Log.v(TAG, "No USB Drivers connected");
      return "No Drivers Detected";
    }

    final UsbSerialDriver driver = availableDrivers.get(0);

    connection = mUsbManager.openDevice(driver.getDevice());
    if (connection == null) {
      Log.v(TAG, "No USB connection found");
      return "No USB Available";
    } else {
      final List<UsbSerialPort> portList = driver.getPorts();
      port = portList.get(0);
      try {
        port.open(connection);
        port.setParameters(38400, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
        return "USB Connected";
      } catch (IOException e) {
        System.out.print("Can't Connect to ELM327!");
        // alertView("Connection Problem", "Can't find the ELM327");
      }
    }
    return message;
  }
Beispiel #2
0
  public String Write_USB_Port(byte[] message_press) {
    String message = "";

    if (connection != null) {
      try {
        // port.write(message,10);
        // byte[] Release =new byte[]{ (byte)0x48, 0x0d };
        port.write(message_press, 100);
        message = " Sent: " + new String(message_press);
        Log.v(TAG, "Value Writing to the Press: " + new String(message_press));
        SystemClock.sleep(110);
        // new Thread(new Runnable() {
        //     @Override
        //     public void run() {
        //         try {
        //             Thread.sleep(1500);

        //         } catch (InterruptedException e) {
        //             e.printStackTrace();
        //         }
        //     }
        // }).start();
        // SystemClock.sleep(400);
        // Thread.sleep(400);
        // port.write(message_release, 100);

        // HeaderText.append(" Also Sent: " + new String(message_release));
        // port.write(concat(message,Release),10);

      } catch (IOException e) {
        Log.v(TAG, "Error Sending Command!");
        message = "Couldn't Send message";
        // alertView("Port Error", "Can't Send Message");
      }
      try {
        port.close();
        Log.v(TAG, "Port Successfully Closed");

      } catch (IOException e) {
        Log.v(TAG, "Couldn't close Port");
        // alertView("Port Error", "Can't Close Port");
      }

    } else {
      Log.v(TAG, "Not Connected to USB!");
      message = "Not Connected to USB";
    }
    return message;
  }