mirror of
https://github.com/mik3y/usb-serial-for-android
synced 2025-06-08 00:16:13 +00:00
Update README.md
This commit is contained in:
parent
e62e95be2e
commit
8a152071b4
69
README.md
69
README.md
@ -15,13 +15,11 @@ functions for use with your own protocols.
|
|||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
|
||||||
**1.** Download [usb-serial-for-android-v010.jar](https://github.com/mik3y/usb-serial-for-android/releases/download/v0.1.0/usb-serial-for-android-v010.jar)
|
**1.** [Link your project](https://github.com/mik3y/usb-serial-for-android/wiki/Building-From-Source) to the library.
|
||||||
|
|
||||||
**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).
|
**2.** 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.
|
||||||
|
|
||||||
**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.
|
**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).
|
||||||
|
|
||||||
**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).
|
|
||||||
|
|
||||||
```xml
|
```xml
|
||||||
<activity
|
<activity
|
||||||
@ -39,25 +37,33 @@ functions for use with your own protocols.
|
|||||||
**5.** Use it! Example code snippet:
|
**5.** Use it! Example code snippet:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
// Get UsbManager from Android.
|
// Find all available drivers from attached devices.
|
||||||
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
||||||
|
List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
|
||||||
|
if (availableDrivers.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Find the first available driver.
|
// Open a connection to the first available driver.
|
||||||
UsbSerialDriver driver = UsbSerialProber.acquire(manager);
|
UsbSerialDriver driver = availableDrivers.get(0);
|
||||||
|
UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
|
||||||
|
if (connection == null) {
|
||||||
|
// You probably need to call UsbManager.requestPermission(driver.getDevice(), ..)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (driver != null) {
|
// Read some data! Most have just one port (port 0).
|
||||||
driver.open();
|
UsbSerialPort port = driver.getPort(0);
|
||||||
|
port.open(connection);
|
||||||
try {
|
try {
|
||||||
driver.setBaudRate(115200);
|
port.setBaudRate(115200);
|
||||||
|
|
||||||
byte buffer[] = new byte[16];
|
byte buffer[] = new byte[16];
|
||||||
int numBytesRead = driver.read(buffer, 1000);
|
int numBytesRead = port.read(buffer, 1000);
|
||||||
Log.d(TAG, "Read " + numBytesRead + " bytes.");
|
Log.d(TAG, "Read " + numBytesRead + " bytes.");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// Deal with error.
|
// Deal with error.
|
||||||
} finally {
|
} finally {
|
||||||
driver.close();
|
port.close();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -68,6 +74,39 @@ 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)
|
A [simple Arduino application](https://github.com/mik3y/usb-serial-for-android/blob/master/arduino)
|
||||||
is also available which can be used for testing.
|
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();
|
||||||
|
probeTable.addProduct(0x1234, 0x0001, CdcAcmSerialDriver.class);
|
||||||
|
probeTable.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
|
## Compatible Devices
|
||||||
|
|
||||||
* *Serial chips:* FT232R, CDC/ACM (eg Arduino Uno) and possibly others.
|
* *Serial chips:* FT232R, CDC/ACM (eg Arduino Uno) and possibly others.
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
<classpathentry kind="src" path="src"/>
|
<classpathentry kind="src" path="src"/>
|
||||||
<classpathentry kind="src" path="gen"/>
|
<classpathentry kind="src" path="gen"/>
|
||||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
|
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
|
||||||
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
|
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
|
||||||
<classpathentry kind="output" path="bin/classes"/>
|
<classpathentry kind="output" path="bin/classes"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
@ -4,5 +4,6 @@
|
|||||||
<classpathentry kind="src" path="gen"/>
|
<classpathentry kind="src" path="gen"/>
|
||||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||||
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
|
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
|
||||||
|
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
|
||||||
<classpathentry kind="output" path="bin/classes"/>
|
<classpathentry kind="output" path="bin/classes"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user