java 通过 Android 3.1 上的 USB 端口读取和写入数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10340300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read and writer data via USB port on Android 3.1
提问by dan
I am trying to write an android app to read and write data from android tablet to our embedded device. The tablet and embedded device are connected via usb to serial converter. Is it possible to read data from the embedded device into our android tablet? Any sample code that I can take a look at?
我正在尝试编写一个 android 应用程序来读取和写入数据从 android 平板电脑到我们的嵌入式设备。平板电脑和嵌入式设备通过 USB 转串口转换器连接。是否可以将嵌入式设备中的数据读取到我们的安卓平板电脑中?我可以查看任何示例代码吗?
采纳答案by Alexander
As a serial converter is a USB device, the android tablet has to act as a USB host. This feature is available since Android 3.1. The API documentation is available on the android developer pageand includes code examples.
由于串行转换器是 USB设备,因此安卓平板电脑必须充当 USB主机。此功能自 Android 3.1 起可用。API 文档可在android 开发人员页面上找到,其中包括代码示例。
To actually communicate with the converter you will have to find out a bit more about it's internals: There are many different devices on the market, some using USB HID, others are using USB CDC device class or do not even comply to any class. The linux lsusbcommand might be helpful to get the device/interface descriptors.
要与转换器进行实际通信,您必须更多地了解它的内部结构:市场上有许多不同的设备,一些使用 USB HID,其他使用 USB CDC 设备类,甚至不符合任何类。linux lsusb命令可能有助于获取设备/接口描述符。
On a typical CDC device you will have to interact with two bulk endpoints and probably use some interface related requests to setup the baudrate, etc. See the Communication Device Class specification on USB implementers forum.
在典型的 CDC 设备上,您将不得不与两个批量端点交互,并且可能使用一些与接口相关的请求来设置波特率等。请参阅USB 实施者论坛上的通信设备类规范。
If your device has only one IN and one OUT bulk endpoint (beside the control endpoint) you might try to use the UsbDeviceConnection.bulkTransfer() method on the OUT endpoint to write some bytes to the serial line. There is a good chance this will just work, however for a clean implementation you should stick to the class specification.
如果您的设备只有一个 IN 和一个 OUT 批量端点(控制端点旁边),您可以尝试使用 OUT 端点上的 UsbDeviceConnection.bulkTransfer() 方法将一些字节写入串行线路。这很有可能会起作用,但是对于干净的实现,您应该坚持类规范。
回答by Vaiden
I suggest using the ported usb-for-android lib, which has the PL2303 driver already implemented: https://code.google.com/r/felixhaedicke-usb-serial-for-android
我建议使用移植的 usb-for-android lib,它已经实现了 PL2303 驱动程序:https: //code.google.com/r/felixhaedicke-usb-serial-for-android
I've used it successfully.
我已经成功使用了。