[![Actions Status](https://github.com/mik3y/usb-serial-for-android/workflows/build/badge.svg)](https://github.com/mik3y/usb-serial-for-android/actions)
[![Jitpack](https://jitpack.io/v/mik3y/usb-serial-for-android.svg)](https://jitpack.io/#mik3y/usb-serial-for-android)
[![Codacy](https://api.codacy.com/project/badge/Grade/4d528e82e35d42d49f659e9b93a9c77d)](https://www.codacy.com/manual/kai-morich/usb-serial-for-android-mik3y?utm_source=github.com&utm_medium=referral&utm_content=mik3y/usb-serial-for-android&utm_campaign=Badge_Grade)
[![codecov](https://codecov.io/gh/mik3y/usb-serial-for-android/branch/master/graph/badge.svg)](https://codecov.io/gh/mik3y/usb-serial-for-android)
# 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 Mode (OTG)](http://developer.android.com/guide/topics/connectivity/usb/host.html)
available since Android 3.1 and working reliably since Android 4.2.
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.
## Quick Start
**1.** Add library to your project:
Add jitpack.io repository to your root build.gradle:
```gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
Add library to dependencies
```gradle
dependencies {
implementation 'com.github.mik3y:usb-serial-for-android:Tag'
}
```
**2.** If the app should be notified when a device is attached, add
[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 and configure in your `AndroidManifest.xml`.
```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>
```
**3.** Use it! Example code snippet:
open device:
```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) {
// add UsbManager.requestPermission(driver.getDevice(), ..) handling here
return;
}
UsbSerialPort port = driver.getPorts().get(0); // Most devices have just one port (port 0)
port.open(connection);
port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
```
then use direct read/write
```java
port.write(request, WRITE_WAIT_MILLIS);
len = port.read(response, READ_WAIT_MILLIS);
```
or direct write + event driven read:
```java
usbIoManager = new SerialInputOutputManager(usbSerialPort, this);
Executors.newSingleThreadExecutor().submit(usbIoManager);
...
port.write("hello".getBytes(), WRITE_WAIT_MILLIS);
@Override
public void onNewData(byte[] data) {
runOnUiThread(() -> { textView.append(new String(data)); });
}
```
and finally:
```java
port.close();
```
For a simple example, see
[UsbSerialExamples](https://github.com/mik3y/usb-serial-for-android/blob/master/usbSerialExamples)
folder in this project.
For a more complete example with background service to stay connected while
the app is not visible or rotating, see separate github project
[SimpleUsbTerminal](https://github.com/kai-morich/SimpleUsbTerminal).
## 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
This library supports USB to serial converter chips:
* FTDI FT232R, FT232H, FT2232H, FT4232H, FT230X, FT231X, FT234XD
* Prolific PL2303
* Silabs CP2102 and all other CP210x
* Qinheng CH340, CH341A
and devices implementing the CDC/ACM protocol like
* Arduino using ATmega32U4
* Digispark using V-USB software USB
* BBC micro:bit using ARM mbed DAPLink firmware
* ...
## Help & Discussion
For common problems, see the
[Troubleshooting](https://github.com/mik3y/usb-serial-for-android/wiki/Troubleshooting)
wiki page.
Are you using the library? Add your project to
[ProjectsUsingUsbSerialForAndroid](https://github.com/mik3y/usb-serial-for-android/wiki/Projects-Using-usb-serial-for-android).
没有合适的资源?快使用搜索试试~ 我知道了~
USB转串口读写.zip
共76个文件
java:21个
xml:20个
gradle:6个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 185 浏览量
2021-12-31
19:15:36
上传
评论
收藏 178KB ZIP 举报
温馨提示
USB转串口读写.zip
资源推荐
资源详情
资源评论
收起资源包目录
USB转串口读写.zip (76个子文件)
usb-serial-for-android-master
gradle.properties 54B
gradle
wrapper
gradle-wrapper.jar 54KB
gradle-wrapper.properties 239B
.github
workflows
build.yml 276B
LICENSE.txt 1KB
gradlew.bat 3KB
build.gradle 264B
.idea
runConfigurations.xml 564B
vcs.xml 167B
misc.xml 3KB
modules.xml 729B
.gitignore 46B
settings.gradle 51B
usbSerialForAndroid
src
androidTest
java
com
hoho
android
usbserial
CrossoverTest.java 5KB
DeviceTest.java 81KB
util
UsbWrapper.java 10KB
TelnetWrapper.java 6KB
AndroidManifest.xml 292B
main
java
com
hoho
android
usbserial
driver
CdcAcmSerialDriver.java 12KB
Ch34xSerialDriver.java 10KB
FtdiSerialDriver.java 17KB
UsbSerialDriver.java 703B
ProlificSerialDriver.java 18KB
CommonUsbSerialPort.java 9KB
UsbSerialProber.java 3KB
ProbeTable.java 3KB
Cp21xxSerialDriver.java 12KB
UsbId.java 3KB
UsbSerialPort.java 8KB
util
SerialInputOutputManager.java 7KB
AndroidManifest.xml 164B
proguard-rules.pro 56B
build.gradle 1KB
coverage.gradle 2KB
publishToMavenLocal.gradle 491B
gradlew 6KB
CHANGELOG.txt 790B
usbSerialExamples
src
main
java
com
hoho
android
usbserial
examples
DevicesFragment.java 6KB
TerminalFragment.java 14KB
CustomProber.java 670B
MainActivity.java 2KB
util
HexDump.java 4KB
res
mipmap-xxhdpi
ic_launcher.png 2KB
mipmap-hdpi
ic_launcher.png 1KB
menu
menu_terminal.xml 332B
menu_devices.xml 366B
mipmap-mdpi
ic_launcher.png 895B
mipmap-xxxhdpi
ic_launcher.png 3KB
mipmap-xhdpi
ic_launcher.png 2KB
xml
device_filter.xml 2KB
values
colors.xml 358B
arrays.xml 427B
strings.xml 237B
styles.xml 292B
layout
device_list_item.xml 918B
device_list_header.xml 634B
activity_main.xml 1KB
fragment_terminal.xml 4KB
drawable
ic_send_white_24dp.xml 329B
ic_delete_white_24dp.xml 373B
AndroidManifest.xml 2KB
build.gradle 773B
test
arduino_leonardo_bridge_multi_cdc
USBCore.cpp 20KB
USBDesc.h 2KB
arduino_leonardo_bridge_multi_cdc.ino 2KB
CDC.cpp 9KB
USBCore.h 9KB
README.md 423B
rfc2217_server.diff 2KB
arduino_leonardo_bridge_castrated_cdc
arduino_leonardo_bridge_castrated_cdc.ino 2KB
CDC.cpp 8KB
USBCore.h 8KB
README.md 415B
arduino_leonardo_bridge
arduino_leonardo_bridge.ino 2KB
.gitignore 562B
README.md 6KB
共 76 条
- 1
资源评论
mYlEaVeiSmVp
- 粉丝: 1910
- 资源: 19万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功