首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在android上读取原始鼠标数据

在android上读取原始鼠标数据
EN

Stack Overflow用户
提问于 2012-11-08 08:02:17
回答 1查看 2.5K关注 0票数 19

在我的Android应用程序中,我通过USB-OTG从3DConnexion SpaceNavigator读取数据来控制AR.Drone

现在我想用鼠标做同样的事情。然而,Android正在抓取鼠标并呈现鼠标光标。当我使用鼠标的供应商和产品ID编写设备筛选器时,我不能像使用SpaceNavigator (strangely, both are HID -- I get no cursor with the SpaceNavigator).那样获得它

有没有一种方法可以在没有光标的情况下获得原始鼠标数据?

将完美的股票安卓。但我也会考虑为此修改ROM。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-14 04:07:03

一旦你的应用程序声明鼠标(在主机上作为USB HID设备),Android应该隐藏光标,你就可以读取原始数据。这应该可以在现有的android上工作,但您的设备必须支持USB主机模式,并且需要USB OTG电缆来连接鼠标。

基本步骤:

  • 枚举设备
  • 请求访问USB
  • 声明device
  • 从HID端点读取数据包
  • 从数据包中解析X和Y位置、按钮单击和滚轮旋转

适用于我的示例代码(Android 5.0):

UsbManager usbManager;
UsbDevice usbDevice;

private void connect() {
    this.usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();

    // just get the first enumerated USB device
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    if (deviceIterator.hasNext()) {
        this.usbDevice = deviceIterator.next();
    }

    if (usbDevice == null) {
        Log.w(TAG, "no USB device found");
        return;
    }

    // ask for permission

    final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if(device != null){
                            // call method to set up device communication
                            Log.i(TAG, "permission granted. access mouse.");

                            // repeat in a different thread
                            transfer(device);
                        }
                    }
                    else {
                        Log.d(TAG, "permission denied for device " + device);
                    }
                }
            } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (device != null) {
                    // TODO:
                    // call your method that cleans up and closes communication with the device
                    // usbInterface.releaseInterface();
                    // usbDeviceConnection.close();
                }
            }
        }
    };

    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    context.registerReceiver(mUsbReceiver, filter);

    usbManager.requestPermission(usbDevice, mPermissionIntent);
}

private void transfer(UsbDevice device) {

    int TIMEOUT = 0;
    boolean forceClaim = true;

    // just grab the first endpoint
    UsbInterface intf = device.getInterface(0);
    UsbEndpoint endpoint = intf.getEndpoint(0);
    UsbDeviceConnection connection = this.usbManager.openDevice(device);
    connection.claimInterface(intf, forceClaim);

    byte[] bytes = new byte[endpoint.getMaxPacketSize()];

    connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT);

    // depending on mouse firmware and vendor the information you're looking for may
    // be in a different order or position. For some logitech devices the following 
    // is true:

    int x = (int) bytes[1];
    int y = (int) bytes[2];
    int scrollwheel = (int) bytes[3]

    // call a listener, process your data ...
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13280402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档