usb-serial-for-android


Source link: https://github.com/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 API 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.

Quick Start

1. Link your project to the library.

2. Copy 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 for help).

<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:

// 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 in git, which is a simple application for reading and showing serial data.

A simple Arduino application 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:

// 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

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 wiki page.

For other help and discussion, please join our Google Group, usb-serial-for-android.

Are you using the library? Let us know on the group and we'll add your project to ProjectsUsingUsbSerialForAndroid.

Resources

An Android Wear Menu implementation.

It's a very simple custom views library according to UIButton of iOS, all of the views can be automatically set a pressed effect to a button with a simple background image without writing a selector.xml

Dagger2Scopes is a sample Android app to implement usual Dagger 1 multiple scoping logic with the brand new Dagger 2 Google lib. In this sample you will be able to find the following features:

  • Multiple scopes with Dagger 2 (Components and subcomponents).
  • An approach to Clean Architecture.
  • An approach to MVP pattern.

RxCupboard brings the excellent Android Cupboard library into the world of RxJava. Using a small set of convenience Observables, you can fluently store and retrieve streams of POJOs from your database.

An efficient sparse bit set implementation for Java.

This library provides you a Spinner with the Material style. You can use it like any "normal" Spinner and add floating label text, hint and error messages.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes