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?