Android:如何检测已连接的USB设备? [英] Android : how to detect already connected usb device?

查看:2066
本文介绍了Android:如何检测已连接的USB设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测已连接到android的usb设备. 我了解可以检测到何时连接或分离了USB. 但是在将USB设备连接到android之后,我并没有真正了解如何检查设备. 另外,我发现每个USB设备都有其设备类代码,但是如何确定连接了哪种设备呢?例如,我需要同时检测USB鼠标和键盘.我该如何区分它们?

I'm trying to detect usb devices which are already connected to android. I understand there are actions to detect when USB is either attached or detached. But I don't really get how to check devices after connecting usb device to android. Also, I've found that each USB device has it's device class code, but how do I figure out what kind of device is connected? For instance, I need to detect both usb mouse and keyboard; how do I differentiate them?

推荐答案

尝试一下:

  1. 第一个寄存器广播用于USB连接. 清单许可:
  1. First register Broadcast for USB connection. manifest permission:

:

<intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter>

  1. 使用此方法获取带有详细信息的USB设备列表

  1. 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();

}}

这篇关于Android:如何检测已连接的USB设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆