# usb-serial-for-android
This is a driver library for communication with Arduinos and other USB serial hardware on
Android, using the
[Android USB Host API](http://developer.android.com/guide/topics/connectivity/usb/host.html)
available on Android 3.1+.
No root access, ADK, or special kernel drivers are required; all drivers are implemented in
Java. You get a raw serial port with `read()`, `write()`, and other basic
functions for use with your own protocols.
* **Homepage**: https://github.com/mik3y/usb-serial-for-android
* **Google group**: http://groups.google.com/group/usb-serial-for-android
* **Latest release**: [v0.1.0](https://github.com/mik3y/usb-serial-for-android/releases)
## Quick Start
**1.** [Link your project](https://github.com/mik3y/usb-serial-for-android/wiki/Building-From-Source) to the library.
**2.** Copy [device_filter.xml](https://github.com/mik3y/usb-serial-for-android/blob/master/usbSerialExamples/src/main/res/xml/device_filter.xml) to your project's `res/xml/` directory.
**3.** Configure your `AndroidManifest.xml` to notify your app when a device is attached (see [Android USB Host documentation](http://developer.android.com/guide/topics/connectivity/usb/host.html#discovering-d) for help).
```xml
<activity
android:name="..."
...>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
```
**4.** Use it! Example code snippet:
```java
// Find all available drivers from attached devices.
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
if (availableDrivers.isEmpty()) {
return;
}
// Open a connection to the first available driver.
UsbSerialDriver driver = availableDrivers.get(0);
UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
if (connection == null) {
// You probably need to call UsbManager.requestPermission(driver.getDevice(), ..)
return;
}
// Read some data! Most have just one port (port 0).
UsbSerialPort port = driver.getPorts().get(0);
try {
port.open(connection);
port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
byte buffer[] = new byte[16];
int numBytesRead = port.read(buffer, 1000);
Log.d(TAG, "Read " + numBytesRead + " bytes.");
} catch (IOException e) {
// Deal with error.
} finally {
port.close();
}
```
For a more complete example, see the
[UsbSerialExamples project](https://github.com/mik3y/usb-serial-for-android/blob/master/usbSerialExamples)
in git, which is a simple application for reading and showing serial data.
A [simple Arduino application](https://github.com/mik3y/usb-serial-for-android/blob/master/arduino)
is also available which can be used for testing.
## Probing for Unrecognized Devices
Sometimes you may need to do a little extra work to support devices which
usb-serial-for-android doesn't [yet] know about -- but which you know to be
compatible with one of the built-in drivers. This may be the case for a brand
new device or for one using a custom VID/PID pair.
UsbSerialProber is a class to help you find and instantiate compatible
UsbSerialDrivers from the tree of connected UsbDevices. Normally, you will use
the default prober returned by ``UsbSerialProber.getDefaultProber()``, which
uses the built-in list of well-known VIDs and PIDs that are supported by our
drivers.
To use your own set of rules, create and use a custom prober:
```java
// Probe for our custom CDC devices, which use VID 0x1234
// and PIDS 0x0001 and 0x0002.
ProbeTable customTable = new ProbeTable();
customTable.addProduct(0x1234, 0x0001, CdcAcmSerialDriver.class);
customTable.addProduct(0x1234, 0x0002, CdcAcmSerialDriver.class);
UsbSerialProber prober = new UsbSerialProber(customTable);
List<UsbSerialDriver> drivers = prober.findAllDrivers(usbManager);
// ...
```
Of course, nothing requires you to use UsbSerialProber at all: you can
instantiate driver classes directly if you know what you're doing; just supply
a compatible UsbDevice.
## Compatible Devices
* *Serial chips:* FT232R, CDC/ACM (eg Arduino Uno) and possibly others.
See [CompatibleSerialDevices](https://github.com/mik3y/usb-serial-for-android/wiki/Compatible-Serial-Devices).
* *Android phones and tablets:* Nexus 7, Motorola Xoom, and many others.
See [CompatibleAndroidDevices](https://github.com/mik3y/usb-serial-for-android/wiki/Compatible-Android-Devices).
## Author, License, and Copyright
usb-serial-for-android is written and maintained by *mike wakerly*.
This library is licensed under *LGPL Version 2.1*. Please see LICENSE.txt for the
complete license.
Copyright 2011-2012, Google Inc. All Rights Reserved.
Portions of this library are based on libftdi
(http://www.intra2net.com/en/developer/libftdi). Please see
FtdiSerialDriver.java for more information.
## Help & Discussion
For common problems, see the
[Troubleshooting](https://github.com/mik3y/usb-serial-for-android/wiki/Troubleshooting)
wiki page.
For other help and discussion, please join our Google Group,
[usb-serial-for-android](https://groups.google.com/forum/?fromgroups#!forum/usb-serial-for-android).
Are you using the library? Let us know on the group and we'll add your project to
[ProjectsUsingUsbSerialForAndroid](https://github.com/mik3y/usb-serial-for-android/wiki/Projects-Using-usb-serial-for-android).
没有合适的资源?快使用搜索试试~ 我知道了~
android usb转串口数据通信示例
共49个文件
java:21个
xml:7个
png:7个
5星 · 超过95%的资源 需积分: 44 316 下载量 80 浏览量
2017-09-14
15:50:37
上传
评论 5
收藏 825KB RAR 举报
温馨提示
android usb转串口数据通信示例。物联网开发中也会经常用到usb转串口,对android手机进行通信。一般都会用otc线进行转换。我在GitHub下来一份代码,亲测可用。并进行了修改封装。我的博客地址:http://blog.csdn.net/qq_16064871
资源详情
资源评论
资源推荐
收起资源包目录
usb-serial-for-android-master.rar (49个子文件)
usb-serial-for-android-master
gradlew 5KB
settings.gradle 51B
LICENSE.txt 24KB
CHANGELOG.txt 709B
usbSerialForAndroid
src
main
AndroidManifest.xml 270B
java
com
hoho
android
usbserial
BuildInfo.java 490B
util
HexDump.java 5KB
SerialInputOutputManager.java 5KB
driver
CommonUsbSerialPort.java 5KB
UsbSerialPort.java 7KB
Cp21xxSerialDriver.java 12KB
UsbSerialProber.java 4KB
UsbId.java 3KB
ProbeTable.java 4KB
UsbSerialRuntimeException.java 1KB
ProlificSerialDriver.java 20KB
CdcAcmSerialDriver.java 16KB
Ch34xSerialDriver.java 10KB
UsbSerialDriver.java 1KB
FtdiSerialDriver.java 20KB
usbSerialForAndroid.iml 8KB
build.gradle 3KB
README.md 5KB
local.properties 459B
gradlew.bat 2KB
picture
Screenshot_20170914-140528.png 136KB
Screenshot_20170913-171536.png 38KB
Screenshot_20170914-134849.png 449KB
Screenshot_20170913-171548.png 135KB
.gitignore 506B
usbSerialExamples
src
main
AndroidManifest.xml 2KB
res
drawable-ldpi
ic_launcher.png 2KB
xml
device_filter.xml 747B
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 3KB
layout
serial_console.xml 2KB
serial_console2.xml 2KB
main.xml 2KB
values
strings.xml 354B
java
src
com
hoho
android
usbserial
examples
ConnectListener.java 283B
UsbSerialPortTopIO.java 5KB
SerialConsoleActivity.java 8KB
DeviceListActivity.java 8KB
TopDataIOListener.java 167B
CustomSerialConsoleActivity.java 4KB
libs
usb-serial-for-android.jar 46KB
usbSerialExamples.iml 9KB
build.gradle 404B
build.gradle 240B
共 49 条
- 1
mmsx
- 粉丝: 718
- 资源: 92
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论14