usb-serial-for-android/README.md

97 lines
3.5 KiB
Markdown
Raw Normal View History

2011-12-28 15:07:58 -08:00
# usb-serial-for-android
2013-09-13 09:29:42 -07:00
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+.
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
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.
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
* **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 (November 13, 2012, see [http://usb-serial-for-android.googlecode.com/git/CHANGELOG.txt CHANGELOG])
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
## Quick Start
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
**1.** Download [usb-serial-for-android-v010.jar](http://usb-serial-for-android.googlecode.com/files/usb-serial-for-android-v010.jar)
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
**2.** Copy the jar to your Android project's `libs/` directory. (See [Android's FAQ](http://developer.android.com/guide/faq/commontasks.html#addexternallibrary) for help).
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
**3.** Copy [device_filter.xml](http://usb-serial-for-android.googlecode.com/git/UsbSerialExamples/res/xml/device_filter.xml) to your project's `res/xml/` directory.
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
**4.** 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).
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
```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>
```
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
**5.** Use it! Example code snippet:
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
```java
// Get UsbManager from Android.
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
// Find the first available driver.
UsbSerialDriver driver = UsbSerialProber.acquire(manager);
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
if (driver != null) {
driver.open();
try {
driver.setBaudRate(115200);
byte buffer[] = new byte[16];
int numBytesRead = driver.read(buffer, 1000);
Log.d(TAG, "Read " + numBytesRead + " bytes.");
} catch (IOException e) {
// Deal with error.
} finally {
driver.close();
}
}
```
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
For a more complete example, see the
[UsbSerialExamples project](https://github.com/mik3y/usb-serial-for-android/master/UsbSerialExamples)
in git, which is a simple application for reading and showing serial data.
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
A [simple Arduino application](https://github.com/mik3y/usb-serial-for-android/master/arduino)
is also available which can be used for testing.
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
## Compatible Devices
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
* *Serial chips:* FT232R, CDC/ACM (eg Arduino Uno) and possibly others. See CompatibleSerialDevices
* *Android phones and tablets:* Nexus 7, Motorola Xoom, and many others. See CompatibleAndroidDevices.
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
## Author, License, and Copyright
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
usb-serial-for-android is written and maintained by *mike wakerly*.
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
This library is licensed under *LGPL Version 2.1*. Please see LICENSE.txt for the
2011-12-28 15:07:58 -08:00
complete license.
2013-09-13 09:29:42 -07:00
Copyright 2011-2012, Google Inc. All Rights Reserved.
2011-12-28 15:07:58 -08:00
Portions of this library are based on libftdi
(http://www.intra2net.com/en/developer/libftdi). Please see
FtdiSerialDriver.java for more information.
2013-09-13 09:29:42 -07:00
## Help & Discussion
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
Please join our Google Group,
[usb-serial-for-android](https://groups.google.com/forum/?fromgroups#!forum/usb-serial-for-android).
2011-12-28 15:07:58 -08:00
2013-09-13 09:29:42 -07:00
Are you using this library? Let us know and we'll add your project to ProjectsUsingUsbSerialForAndroid.
2011-12-28 15:07:58 -08:00