简体   繁体   English

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

[英]How to catch signals from a unknown USB OTG device?

I have a USB OTG device which acts like a mouse, bought in china with not much more information about the used controller.我有一个像鼠标一样的 USB OTG 设备,是在中国购买的,关于所用控制器的信息不多。 When I connect it to my android there is a mouse cursor and the device has 5 hard buttons (left, right, up, down, enter).当我将它连接到我的 android 时,有一个鼠标光标,并且该设备有 5 个硬按钮(向左、向右、向上、向下、输入)。 I want to programm the buttons for my app to performe specific tasks.我想为我的应用程序编写按钮以执行特定任务。 So I need to read the input signals and overwrite them.所以我需要读取输入信号并覆盖它们。

How can I catch the signals?我怎样才能捕捉到信号?

I found out the vendor (0x04D9) and product id (0x2519) and the controller name Lenovo Calliope USB Keyboard .我找到了供应商 (0x04D9) 和产品 ID (0x2519) 以及控制器名称Lenovo Calliope USB Keyboard But no idea about the used chip, it's covert.但不知道使用的芯片,它是隐蔽的。

It doesn't work with the methods onKeyDown or dispatchKeyEvent .它不适用于onKeyDowndispatchKeyEvent方法。 Also not with USB serial Lib because the device is not found/ recognized with the provided VID und PID (see discussion with Fatih Şennik below, other devices are recognized with it).也不适用于USB serial Lib因为无法使用提供的 VID 和 PID 找到/识别该设备(请参阅下面与 Fatih Şennik 的讨论,其他设备可以通过它识别)。

My current assumption is that it is a Hardware/ Chip issue that I cannot get the signals.我目前的假设是我无法获得信号是硬件/芯片问题。 But the strange thing is that the device otherwise does what it is supposed to do.但奇怪的是,该设备以其他方式执行它应该做的事情。

You can use a USB serial Lib such as https://github.com/mik3y/usb-serial-for-android and give vendor and product ID of your USB OTG device to control it.您可以使用 USB 串行 Lib,例如https://github.com/mik3y/usb-serial-for-android并提供您的 USB OTG 设备的供应商和产品 ID 来控制它。 So you can catch left, right, up, down and hex codes in any monitor and based on the raw byte, you can do a switch like operation.因此,您可以在任何监视器中捕获左、右、上、下和十六进制代码,并且基于原始字节,您可以进行类似操作的切换。

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

Lets first try this to connect our android phone to the USB otg device.让我们首先尝试将我们的 android 手机连接到 USB otg 设备。 As for the product and vendor ids, you can find it when you plug it to your pc.至于产品和供应商 ID,您可以在将其插入 PC 时找到它。 if the connection is established then the rest will come.如果建立了连接,那么剩下的就会到来。 it would be helpful if you can share the USB Otg device data sheets here.如果您可以在此处共享 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();
        }
    }
}

if it is USB hid device, Trying this code might help;如果是 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.");
}

The solution if nothings works: I got an arduino controller and solder it to the buttons.如果没有任何效果,解决方案是:我有一个 arduino 控制器并将其焊接到按钮上。 It's not that complicated.没那么复杂。

Important is that the controller supports hid eg the leonardo pro micro with the atmega32u4 chip.重要的是控制器支持隐藏,例如带有 atmega32u4 芯片的 leonardo pro micro。 The code for the controller can be found easily with google.控制器的代码可以通过谷歌轻松找到。

Then it works with override onKeyDown or onKeyUp etc.然后它可以覆盖 onKeyDown 或 onKeyUp 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM