Version 2.0 of this library is available here: http://www.b4x.com/forum/additional...serial-library-2-0-supports-more-devices.html
This library is a wrapper for this open source project: usb-serial-for-android - Android USB host serial driver library for CDC, FTDI, Arduino and other devices. - Google Project Hosting
It requires Android 3.1+.
This library together with the wrapper create a simple interface for working with USB serial devices.
Once the USB is opened all the communication is done with AsyncStreams.
Note that prefix mode is not supported.
A simple program that prints the input to the logs and sends a string (the program is attached):
Library setup instructions:
1. Download usb-serial-for-android-v010.jar: Downloads - usb-serial-for-android - Android USB host serial driver library for CDC, FTDI, Arduino and other devices. - Google Project Hosting
2. Copy this file and the attached library (extract it first) to the libraries folder.
Application setup instructions:
The user has to give your application permission to access the USB device before it can be opened.
1. Add the following code to the manifest editor:
2. Copy device_filter.xml from the attached example to: <your project>\objects\res\xml and mark it as read-only.
These two steps were already done for the example project.
3. Install the program and attach the USB device. A dialog will appear asking whether you want to start your program.
From now on the USB device will work with your program.
Tips:
1. You can use this open source Windows terminal: Download - Tera Term - SourceForge.JP
2. The example project UI is minimal. Check the logs for the input.
3. Check the unfiltered logs if there are problems opening the USB device. There might be an error message there.
4. Use B4A-Bridge to connect to the Android device.
This library is a wrapper for this open source project: usb-serial-for-android - Android USB host serial driver library for CDC, FTDI, Arduino and other devices. - Google Project Hosting
It requires Android 3.1+.
This library together with the wrapper create a simple interface for working with USB serial devices.
Once the USB is opened all the communication is done with AsyncStreams.
Note that prefix mode is not supported.
A simple program that prints the input to the logs and sends a string (the program is attached):
B4X:
Sub Process_Globals
Dim usb As UsbSerial
Dim astreams As AsyncStreams
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
End Sub
Sub btnOpen_Click
If usb.Open(9600) Then
Log("Connected successfully!")
astreams.Initialize(usb.GetInputStream, usb.GetOutputStream, "astreams")
Else
Log("Error opening USB port")
End If
End Sub
Sub Astreams_NewData (Buffer() As Byte)
Log("NewData")
Log(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub
Sub btnClose_Click
astreams.Close
End Sub
Sub btnSend_Click
astreams.Write("abcde".GetBytes("UTF8"))
End Sub
Sub AStreams_Error
Log("Error: " & LastException)
astreams.Close
End Sub
Sub Astreams_Terminated
Log("Terminated")
astreams.Close
End Sub
Library setup instructions:
1. Download usb-serial-for-android-v010.jar: Downloads - usb-serial-for-android - Android USB host serial driver library for CDC, FTDI, Arduino and other devices. - Google Project Hosting
2. Copy this file and the attached library (extract it first) to the libraries folder.
Application setup instructions:
The user has to give your application permission to access the USB device before it can be opened.
1. Add the following code to the manifest editor:
B4X:
AddActivityText(main, <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" />)
These two steps were already done for the example project.
3. Install the program and attach the USB device. A dialog will appear asking whether you want to start your program.
From now on the USB device will work with your program.
Tips:
1. You can use this open source Windows terminal: Download - Tera Term - SourceForge.JP
2. The example project UI is minimal. Check the logs for the input.
3. Check the unfiltered logs if there are problems opening the USB device. There might be an error message there.
4. Use B4A-Bridge to connect to the Android device.