0

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
  public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
   if (ACTION_USB_PERMISSION.equals(action)) {
    synchronized (this) {

     setDevice(intent);
    }
   }
   if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
    synchronized (this) {
     setDevice(intent);
    }
    if (device == null) {
     mLog("device connected");
    }
   }
   if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
    if (device != null) {
     device = null;
     btnSend.setEnabled(false);
    }
    mLog("device disconnected");
   }
  }

  private void setDevice(Intent intent) {
   device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
   if (device != null && intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
    mLog("Selected device VID:" + Integer.toHexString(device.getVendorId()) + " PID:" + Integer.toHexString(device.getProductId()));
    connection = mUsbManager.openDevice(device);
    intf = device.getInterface(0);
    if (null == connection) {
     mLog("(unable to establish connection)\n");
    } else {
     connection.claimInterface(intf, true);
    }
    try {
     if (UsbConstants.USB_DIR_OUT == intf.getEndpoint(1).getDirection()) {
      endPointWrite = intf.getEndpoint(1);
     }
    } catch (Exception e) {
     Log.e("endPointWrite", "Device have no endPointWrite", e);
    }
    try {
     if (UsbConstants.USB_DIR_IN == intf.getEndpoint(0).getDirection()) {
      endPointRead = intf.getEndpoint(0);
      packetSize = endPointRead.getMaxPacketSize();
     }
    } catch (Exception e) {
     Log.e("endPointWrite", "Device have no endPointRead", e);
    }
    btnSend.setEnabled(true);
   }
  }
 };

void showListOfDevices() {
  btnSend.setEnabled(false);
  AlertDialog.Builder builder = new AlertDialog.Builder(this);

  mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
  if (mUsbManager.getDeviceList().size() == 0) {
   builder.setTitle(MESSAGE_CONNECT_YOUR_USB_HID_DEVICE);
  } else {
   builder.setTitle(MESSAGE_SELECT_YOUR_USB_HID_DEVICE);
  }
  List<CharSequence> list = new LinkedList<CharSequence>();
  for (UsbDevice usbDevice : mUsbManager.getDeviceList().values()) {
   list.add("devID:" + usbDevice.getDeviceId() + " VID:" + Integer.toHexString(usbDevice.getVendorId()) + " PID:" + Integer.toHexString(usbDevice.getProductId()) + " " + usbDevice.getDeviceName());
  }
  final CharSequence devicesName[] = new CharSequence[mUsbManager.getDeviceList().size()];
  list.toArray(devicesName);
  builder.setItems(devicesName, new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
    device = (UsbDevice) mUsbManager.getDeviceList().values().toArray()[which];
    mUsbManager.requestPermission(device, mPermissionIntent);
   }
  });
  builder.setCancelable(true);
  builder.show();
 }

I develeped an Application to read serial data from USB HID device. It works fine on Nexus 7 tablet. Now I am trying to read incoming serial data from USB HID device connected to pcDuino3 (Android OS 4.2.2) from my Application. But I am unable to detect the USB HID device from my App. I used USB Host API.

However I am able to detect the device through terminal window. My Device Details

product ID : 0010

Vender ID : 1658

Here is some observation of my project. In Terminal emulator When I give command "ls /dev/usb", I get the following output:

@android: / $ ls/dev/usb

hiddev0

input3-1.1

input3-1.2

input3-1.4

When I give command "busybox lsusb" I get the following output:

@android: / $ busybox lsusb

Bus 001 Device 002 : ID 0bda:8179

Bus 003 Device 002 : ID 1a40:0101

Bus 001 Device 001 : ID 1d6b:0002

Bus 002 Device 001 : ID 1d6b:0001

. . .

. . .

Bus 003 Device 007 : ID 1658:0010

Can any one help me to resolve this problem or enlighten me on what is going on?

  • check this link http://stackoverflow.com/questions/29679318/reading-data-from-arduino-uno-r3-kit its the question u need to check not the answer.. – DJphy Jun 04 '15 at 06:36
  • Try using physcalloid and set configuration like baud rate, start, stop bits etc... Physicalloid lib is built for such things – DJphy Jun 04 '15 at 06:37
  • I am Unable to detect the device. If the device get detected then I can get the data. – user3926387 Jun 04 '15 at 09:10
  • mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); When this code runs if the device is detected it should display "Select your device". If not connected then it should display "Please connect your HID device". In my app even though device is connected it displays please connect your device message. This happens only on pcDuino3 – user3926387 Jun 04 '15 at 09:13
  • give some time i'll reply with the answer for u, i'm a bit busy now... – DJphy Jun 04 '15 at 10:31
  • Ok when you get time reply... :) – user3926387 Jun 04 '15 at 11:48
  • My device is rooted. I followed steps explained in answer of following question. but I could not create android.hardware.usb.host.xml file. I am not able to edit the file. it says read only file system, how can i edit it? http://stackoverflow.com/questions/11183792/android-usb-host-and-hidden-devices/11992683#11992683 In system/etc/permissions I don't have android.hardware.usb.host.xml file. It has android.hardware.usb.accessory.xml file – user3926387 Jun 04 '15 at 13:39
  • Ya they are asking u to create an xml file manually as described in answer and add it into **system/etc/permissions** and find a file named **handheld_core_hardware.xml or tablet_core_hardware.xml** in the same folder and u need edit this file and add this **** under permission section of that file(prefix_hardware.xml).. – DJphy Jun 04 '15 at 15:27
  • I am not able to edit the file. it says read only file system, how can i edit it? – user3926387 Jun 05 '15 at 03:58

0 Answers0