如何在Android中检查或匹配InputDevice deviceid和UsbDevice deviceid?

时间:2018-09-25 12:18:06

标签: android usb input-devices usb-hostcontroller

我正在获取所有具有ID的附加设备列表。

  UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
            HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
            Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
            deviceInfoModels.clear();


        while (deviceIterator.hasNext()) {
            UsbDevice device = deviceIterator.next();
            DeviceInfoModel deviceInfoModel = new DeviceInfoModel();
            deviceInfoModel.setDeviceClass(device.getClass() + "");
            deviceInfoModel.setDeviceID(device.getDeviceId()+"");
            deviceInfoModel.setDeviceName(device.getDeviceName() + " " + device.getProductName());
            deviceInfoModel.setVendorID(device.getVendorId() + "");
            deviceInfoModel.setDeviceSubClass(device.getDeviceSubclass() + "");
            deviceInfoModel.setProductID(device.getProductId() + "");
            deviceInfoModels.add(deviceInfoModel);
        }

**This Code For Input Device**

 InputManager inputManager = (InputManager) getSystemService(Context.INPUT_SERVICE);
        for (int i = 0; i < inputManager.getInputDeviceIds().length; i++) {
          InputDevice inputDevice=inputManager.getInputDevice(inputManager.getInputDeviceIds()[i]);
          inputDevice.getVendorId();
          inputDevice.getId();
        }

当我匹配InputDevice ID和UsbDevice Id时,我获得了不同的ID。

注意:我会获得所有正确的信息,例如产品名称,供应商ID等。

但是我的问题是我的板上连接了多个鼠标。 我找不到单击的鼠标。

我的鼠标点击代码

@Override
    public boolean onTouchEvent(MotionEvent event) {
        // Mouse input is treated differently:


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
            InputDevice.SOURCE_MOUSE == InputDevice.SOURCE_MOUSE) {
        Toast.makeText(this, event.getDeviceId() + "", Toast.LENGTH_LONG).show();
    } 
    return super.onTouchEvent(event);
}

我在此处获得的设备ID不同。

我如何找到被选中的鼠标

1 个答案:

答案 0 :(得分:2)

使用 InputDevice代替USBDevice ,您将获得相同的ID 。.

InputManager inputManager = (InputManager) getSystemService(Context.INPUT_SERVICE);
        for (int i = 0; i < inputManager.getInputDeviceIds().length; i++) {
                    if (InputDevice.SOURCE_MOUSE == inputManager.getInputDevice(inputManager.getInputDeviceIds()[i]).getSources()) {
                        InputDevice inputDevice = inputManager.getInputDevice(inputManager.getInputDeviceIds()[i]);
                        DeviceInfoModel deviceInfoModel = new DeviceInfoModel();
                        deviceInfoModel.setDeviceClass(inputDevice.getClass() + "");
                        deviceInfoModel.setDeviceName(inputDevice.getName() + " " + inputDevice.getProductId());
                        deviceInfoModel.setVendorID(inputDevice.getVendorId() + "");
                        deviceInfoModel.setDeviceID(inputDevice.getId() + "");
        //                deviceInfoModel.setDeviceSubClass(inputDevice.isEnabled() + "");
                        deviceInfoModel.setProductID(inputDevice.getProductId() + "");
                        deviceInfoModels.add(deviceInfoModel);
                    }
                }