gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:16:10 24 4
gpt4 key购买 nike

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

现在我想用鼠标做同样的事情。然而,Android 正在抓取鼠标并呈现鼠标光标。当我使用鼠标的供应商和产品 ID 编写设备过滤器时,我并没有像 SpaceNavigator 那样得到它(奇怪的是,两者都是 HID —— SpaceNavigator 没有光标)。

有没有办法在没有光标的情况下获取原始鼠标数据?

将与股票 Android 完美结合。但我也会考虑为此更改 ROM。

最佳答案

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

基本流程:

  • 枚举设备
  • 请求访问 USB 设备的权限
  • 认领设备
  • 从 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 ...
}

关于android - 在android上读取原始鼠标数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13280402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com