我想要检测已连接到安卓设备上的USB设备。
我知道可以通过一些步骤检测USB设备是否已经连接或断开。但是我不太明白如何在连接USB设备后检查它们。
另外,我发现每个USB设备都有它的设备类代码,但我该如何找出连接的设备是什么类型的呢?例如,我需要检测USB鼠标和键盘,我该如何区分它们?
请尝试以下操作:
:
<intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter>
Get the List of USB Device with details by using this
public void getDetail() {
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while (deviceIterator.hasNext()) {
UsbDevice device = deviceIterator.next();
manager.requestPermission(device, mPermissionIntent);
String Model = device.getDeviceName();
int DeviceID = device.getDeviceId();
int Vendor = device.getVendorId();
int Product = device.getProductId();
int Class = device.getDeviceClass();
int Subclass = device.getDeviceSubclass();
}}
为了回答Benny的问题,以下是mPermissionIntent可能看起来像什么:
string actionString = context.PackageName + ".action.USB_PERMISSION";
PendingIntent mPermissionIntent = PendingIntent.GetBroadcast(context, 0, new
Intent(actionString), 0);
mUsbManager.RequestPermission(device, permissionIntent);
string actionString = context.PackageName + ".action.USB_PERMISSION";
is wrong. It should be string actionString = context.PackageName + ".USB_PERMISSION";
- AkshayT
mPermissionIntent
是什么?我该如何创建它? - Benny