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();
}
&#13;
我开发了一个应用程序,用于从USB HID设备读取串行数据。它在Nexus 7平板电脑上运行良好。 现在我试图从我的应用程序中读取连接到pcDuino3(Android OS 4.2.2)的USB HID设备的传入串行数据。但我无法从我的应用程序中检测到USB HID设备。我使用了USB Host API。
然而,我能够通过终端窗口检测设备。 我的设备详细信息
产品编号:0010
供应商ID:1658
以下是我的项目的一些观察。 在终端模拟器中当我发出命令&#34; ls / dev / usb&#34;时,我得到以下输出:
@android:/ $ ls / dev / usb
hiddev0
input3-1.1
input3-1.2
input3-1.4
当我发出命令&#34; busybox lsusb&#34;我得到以下输出:
@android:/ $ busybox lsusb
总线001设备002:ID 0bda:8179
总线003设备002:ID 1a40:0101
总线001设备001:ID 1d6b:0002
总线002设备001:ID 1d6b:0001
。 。
。 。
总线003设备007:ID 1658:0010
任何人都可以帮我解决这个问题或者让我知道发生了什么事吗?