简体   繁体   English

如何从USB设备接收消息

[英]How to receive messages from a usb device

I have a usb device connected to Android that when I press a button(located on the usb device) It sends a 'S' string. 我有一个连接到Android的USB设备,当我按下一个按钮(位于USB设备上)时,它将发送一个“ S”字符串。 How can I detect such string? 如何检测到这样的字符串?

When I connect the Usb device to windows, and open any text editor such notepad++ and press the button the 'S' string appears. 当我将Usb设备连接到Windows并打开任何文本编辑器(如notepad ++)并按下按钮时,将出现“ S”字符串。

I have managed to connect with the device which is a camera and open it. 我已经成功连接了相机设备并打开了它。 But is there any way to detect when the button is pressed by receaving the string 'S'. 但是有什么方法可以通过接收字符串“ S”来检测何时按下按钮。

From documentation: USB Host 从文档: USB主机

USB SERVICE contains a Service to isolate all Usb operations. USB SERVICE包含隔离所有USB操作的服务。 It's a sample code to read from serial port: 这是从串行端口读取的示例代码:

String YOUR_DEVICE_NAME;
byte[] DATA;
int TIMEOUT;

USBManager manager = getApplicationContext().getSystemService(Context.USB_SERVICE);
Map<String, USBDevice> devices = manager.getDeviceList();
USBDevice mDevice = devices.get(YOUR_DEVICE_NAME);
USBDeviceConnection connection = manager.openDevice(mDevice);
USBEndpoint endpoint = device.getInterface(0).getEndpoint(0);

connection.claimInterface(device.getInterface(0), true);
connection.bulkTransfer(endpoint, DATA, DATA.length, TIMEOUT);

Hope it helps. 希望能帮助到你。

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

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