androidusbinput-devicesusb-hostcontroller

How to Check or match InputDevice deviceid and UsbDevice deviceid in android?


I am getting all Attached Device List.. with 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();
        }

when i match InputDevice ID and UsbDevice Id i get different IDs.

Note : I get all the correct information, like product name,vendor id etc.

But My issue is i have multiple Mouse attached with my board. i can not get which mouse was clicked.

My Code for Mouse Clicked

@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);
}

I get Device id Different HERE.

HOW CAN I GET WHICH MOUSE WAS CLICKED


Solution

  • Use InputDevice Instead of USBDevice and you will get same 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);
                        }
                    }