Beispiel #1
0
 @Override
 public void onReceive(Context arg0, Intent arg1) {
   if (arg1.getAction().equals(ACTION_USB_PERMISSION)) {
     boolean granted = arg1.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED);
     if (granted) // User accepted our USB connection. Try to open the device as a serial
                  // port
     {
       Intent intent = new Intent(ACTION_USB_PERMISSION_GRANTED);
       arg0.sendBroadcast(intent);
       connection = usbManager.openDevice(device);
       serialPortConnected = true;
       new ConnectionThread().run();
     } else // User not accepted our USB connection. Send an Intent to the Main Activity
     {
       Intent intent = new Intent(ACTION_USB_PERMISSION_NOT_GRANTED);
       arg0.sendBroadcast(intent);
     }
   } else if (arg1.getAction().equals(ACTION_USB_ATTACHED)) {
     if (!serialPortConnected)
       findSerialPortDevice(); // A USB device has been attached. Try to open it as a Serial
                               // port
   } else if (arg1.getAction().equals(ACTION_USB_DETACHED)) {
     // Usb device was disconnected. send an intent to the Main Activity
     Intent intent = new Intent(ACTION_USB_DISCONNECTED);
     arg0.sendBroadcast(intent);
     serialPortConnected = false;
     serialPort.close();
   }
 }
Beispiel #2
0
    @Override
    public void run() {
      serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection);
      if (serialPort != null) {
        if (serialPort.open()) {
          serialPort.setBaudRate(BAUD_RATE);
          serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8);
          serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1);
          serialPort.setParity(UsbSerialInterface.PARITY_NONE);
          serialPort.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF);
          serialPort.read(mCallback);

          // Everything went as expected. Send an intent to MainActivity
          Intent intent = new Intent(ACTION_USB_READY);
          context.sendBroadcast(intent);
        } else {
          // Serial port could not be opened, maybe an I/O error or if CDC driver was chosen, it
          // does not really fit
          // Send an Intent to Main Activity
          if (serialPort instanceof CDCSerialDevice) {
            Intent intent = new Intent(ACTION_CDC_DRIVER_NOT_WORKING);
            context.sendBroadcast(intent);
          } else {
            Intent intent = new Intent(ACTION_USB_DEVICE_NOT_WORKING);
            context.sendBroadcast(intent);
          }
        }
      } else {
        // No driver for given device, even generic CDC driver could not be loaded
        Intent intent = new Intent(ACTION_USB_NOT_SUPPORTED);
        context.sendBroadcast(intent);
      }
    }
Beispiel #3
0
  public void setDevice(UsbManager usbManager, UsbDevice usbDevice) {
    //		Log.d(TAG, "setDevice " + usbDevice);

    device = usbDevice;

    if (device != null) {
      usbDeviceDump(usbDevice);

      UsbInterface controlInterface = device.getInterface(1);

      endpoint = controlInterface.getEndpoint(0);

      UsbDeviceConnection connection = usbManager.openDevice(device);

      if (connection != null && connection.claimInterface(controlInterface, true)) {
        controlConnection = connection;
        serial = UsbSerialDevice.createUsbSerialDevice(device, controlConnection);
        boolean t = serial.open();
        Log.d("Set DEVICE : ", "" + t);
        serial.setBaudRate(9600);
        serial.setParity(UsbSerialInterface.PARITY_NONE);
        serial.setStopBits(UsbSerialInterface.STOP_BITS_1);
        serial.setDataBits(UsbSerialInterface.DATA_BITS_8);
      } else {
        Log.e(TAG, "open connection FAIL");
        controlConnection = null;
      }
    }
  }
Beispiel #4
0
 public void explicitSend(byte[] a) {
   // serial.write(a);
   if (a != null && serial != null) {
     Log.d("MAESTRO: SEND: ", HexData.hexToString(a));
     // serial.syncWrite(a, 5000);
     // serial.syncRead(buffer, 5000);
     serial.write(a);
     try {
       Thread.sleep(200);
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
     // Log.d("MAESTRO: ", String.valueOf(i));
   }
 }
Beispiel #5
0
 /*
  * This function will be called from MainActivity to write data through Serial Port
  */
 public void write(byte[] data) {
   if (serialPort != null) serialPort.write(data);
 }