gpt4 book ai didi

java - 如何捕获来自未知 USB OTG 设备的信号?

转载 作者:行者123 更新时间:2023-12-01 14:03:52 31 4
gpt4 key购买 nike

我有一个像鼠标一样的 USB OTG 设备,在中国购买,关于所用 Controller 的信息不多。当我将它连接到我的 android 时,有一个鼠标光标,并且该设备有 5 个硬按钮(左、右、上、下、输入)。我想对我的应用程序的按钮进行编程以执行特定任务。所以我需要读取输入信号并覆盖它们。
我怎样才能捕捉到信号?
我找到了供应商 (0x04D9) 和产品 ID (0x2519) 以及 Controller 名称 Lenovo Calliope USB Keyboard。但不知道用过的芯片,它是隐蔽的。
它不适用于 onKeyDown 方法。或 dispatchKeyEvent .也不与 USB serial Lib因为没有使用提供的 VID 和 PID 找到/识别设备(请参阅下面与 Fatih Şennik 的讨论,其他设备也可以识别)。
我目前的假设是我无法获得信号是硬件/芯片问题。但奇怪的是,该设备在其他方面做了它应该做的事情。

最佳答案

您可以使用 USB 串行库,例如 https://github.com/mik3y/usb-serial-for-android并提供您的 USB OTG 设备的供应商和产品 ID 来控制它。因此,您可以在任何监视器中捕捉左、右、上、下和十六进制代码,并且基于原始字节,您可以进行类似切换的操作。

UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() { 
//Defining a Callback which triggers whenever data is read.
@Override
public void onReceivedData(byte[] arg0) {
String data = null;
try {
data = new String(arg0, "UTF-8");
data.concat("/n");
switch() // etc
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
};
让我们首先尝试将我们的 android 手机连接到 USB otg 设备。至于产品和供应商 ID,您可以在将其插入 PC 时找到它。如果建立了连接,那么剩下的就来了。如果您可以在此处共享 USB Otg 设备数据表,将会很有帮助。
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
ProbeTable customTable = new ProbeTable();
customTable.addProduct(0x0403, 0x6001, CdcAcmSerialDriver.class);

List<UsbSerialDriver> availableDrivers = new UsbSerialProber(customTable).findAllDrivers(manager);
ArrayAdapter<String> deviceList = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_2);
ListView listDevices = (ListView) findViewById(R.id.listView);
if(!availableDrivers.isEmpty()) {
for(UsbSerialDriver driver: availableDrivers) {
deviceList.add(driver.getDevice().getDeviceName());
}
listDevices.setAdapter(deviceList);
} else {
Toast.makeText(getApplicationContext(), "No devices found", Toast.LENGTH_LONG).show();
}
}
}
如果它是 USB 隐藏设备,尝试此代码可能会有所帮助;
UsbManager mManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = mManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

while (deviceIterator.hasNext())
{
UsbDevice device = deviceIterator.next();
Log.i(TAG,"Model: " + device.getDeviceName());
Log.i(TAG,"ID: " + device.getDeviceId());
Log.i(TAG,"Class: " + device.getDeviceClass());
Log.i(TAG,"Protocol: " + device.getDeviceProtocol());
Log.i(TAG,"Vendor ID " + device.getVendorId());
Log.i(TAG,"Product ID: " + device.getProductId());
Log.i(TAG,"Interface count: " + device.getInterfaceCount());
Log.i(TAG,"---------------------------------------");
// Get interface details
for (int index = 0; index < device.getInterfaceCount(); index++)
{
UsbInterface mUsbInterface = device.getInterface(index);
Log.i(TAG," ***** *****");
Log.i(TAG," Interface index: " + index);
Log.i(TAG," Interface ID: " + mUsbInterface.getId());
Log.i(TAG," Inteface class: " + mUsbInterface.getInterfaceClass());
Log.i(TAG," Interface protocol: " + mUsbInterface.getInterfaceProtocol());
Log.i(TAG," Endpoint count: " + mUsbInterface.getEndpointCount());
// Get endpoint details
for (int epi = 0; epi < mUsbInterface.getEndpointCount(); epi++)
{
UsbEndpoint mEndpoint = mUsbInterface.getEndpoint(epi);
Log.i(TAG," ++++ ++++ ++++");
Log.i(TAG," Endpoint index: " + epi);
Log.i(TAG," Attributes: " + mEndpoint.getAttributes());
Log.i(TAG," Direction: " + mEndpoint.getDirection());
Log.i(TAG," Number: " + mEndpoint.getEndpointNumber());
Log.i(TAG," Interval: " + mEndpoint.getInterval());
Log.i(TAG," Packet size: " + mEndpoint.getMaxPacketSize());
Log.i(TAG," Type: " + mEndpoint.getType());
}
}
}
Log.i(TAG," No more devices connected.");
}

关于java - 如何捕获来自未知 USB OTG 设备的信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63600942/

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