天天看点

Android串口通信:基本知识梳理

在Android开发中,如果涉及到与外设硬件、新设备的通信,往往需要通过串口来完成。这儿梳理总结一下Android串口的一些知识。

我就直接引用Google开源项目中的资料:

https://code.google.com/p/android-serialport-api/wiki/android_to_rs232_guideline?tm=6

[quote]

Here is a page describing the different ways you could use to connect an Android device to an RS232 peripheral.

[/quote]

[img]http://dl2.iteye.com/upload/attachment/0106/3921/412d95b6-b7ac-3eaa-835b-5489ca0c134e.png[/img]

[quote]

Solution 1

pros

No need for external API, the Android SDK provides the class BluetoothSocket

No need for hardware modifications

hardware flow control is supported

cons

Bluetooth consume battery

high latency

low bandwidth

API

Android SDK

[/quote]

参考地址(要fanqiang):

BluetoothSocket:http://developer.android.com/reference/android/bluetooth/BluetoothSocket.html

Android SDK:http://developer.android.com/guide/topics/connectivity/bluetooth.html

[quote]

Solution 2

pros

USB to RS232 adapters are cheap and easy to find

no hardware modification needed

no external battery needed

low latency

high bandwidth

cons

your Android device needs an USB host connector (most tablets have one, but phones usually don't)

your may need to root your device in order to change /dev/ttyUSB0 file permission, and to load a kernel module.

API

android-serialport-api

[/quote]

[quote]

Solution 3

pros

The cheapest solution

cons

hardware adapter must be built (http://www.instructables.com/id/Android-G1-Serial-Cable)

very few Android devices compatibles

hardware flow control is not supported (only RX/TX, no RTS/CTS signals)

API

android-serialport-api

[/quote]

[quote]

Solution 4

pros

compatible with any Android device with an USB slave connector, i.e. 99.9% of the Android devices.

no need to root your phone

low latency

high bandwidth

you may use other GPIOs of the microcontroller at the same time

cons

API

https://github.com/ytai/ioio/wiki/UART

[/quote]

大概意思如下:

[quote]

上图中用与Android与串口设备通信的方案有四种:

第一种是直接用SDK的BluetoothSocket类来进行蓝牙通信,外部设备再用蓝牙转串口进行控制。这种方式有较高延时,蓝牙模块需要供电,低带宽。

第二种是使用USB转RS232方式(使用内核驱动和使用Android驱动两种方式),这种方式不需要硬件改动,不需要另外的供电,延时很小且有较高带宽。但是Android设备需要硬件上支持USB Host接口(一般Android平板支持,Android手机一般是没有的),另外可能需要root以改变/dev/ttyUSB0文件权限来加载一个内核模块。开发需要使用android_serialport_api。

第三种是最容易的方案,直接使用串口进行连接,但是这种方式兼容性不好,只有少数设备支持,而且串口不支持流控制(由Android提供的USB Host API决定的)。使用时也用android_serialport_api。

第四种是将Android作为USB从机,外部设备作为USB主机与之通信,这种方式几乎与所有Android设备兼容(一般都有USB从口),无需root,低延迟,高带宽。

[/quote]