B4A Library UsbSerial library 2.0 - supports more devices

This is an expanded version of the original UsbSerial library. It has added support for Prolific PL2303 USB to serial converters, Android ADK devices and USB permissions. All devices use the same simple interface intended to be used with AsyncStreams and AsyncStreamsText. Note that AsyncStreams prefix mode is not supported. The library is based on the same open source project Android USB host serial driver library as the existing UsbSerial library but no longer needs a separate jar file as the project source code is incorporated in the library.

The specific enhancements to the library over the original UsbSerial library are :

UsbPresent, HasPermission and RequestPermission are added to identify any attached device or Accessory available to the library and deal with permission to access it.

SetParameters, which must be used after Open(), and the constants for SetParameters provides acess to all the serial line parameters instead of just baud rate.

DeviceInfo provides a string containing information about a device. This works for slave devices only.

Android Accessories, which are host mode devices, are recognised and can be used in the same way as the other slave mode devices.

Prolific PL2303 support is added.

Silicon Labs CP210x support is added - maybe only the CP2102 as I have no hardware to test.

The FTDI "status byte" bug on reading input that existed in version 1.0 of this library is hopefully fixed.


The usb-serial-for-android project and therefore also this library is licensed under the GNU Lesser General Public License v3. http://www.gnu.org/licenses/lgpl.html|http://www.gnu.org/licenses/lgpl.html
Copies of both the General Public License and Lesser General Public License are in the provided archive.

The user has to give your application permission to access the USB device before it can be opened. You can do this in two ways.

As with the original UsbSerial library you can 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" />)
Then copy device_filter.xml from the demo in the attached archive to: <your project>\objects\res\xml and mark it as read-only. Note that this is an expanded version of the original device_filter.xml file.

Finally install the program and attach the USB device. A dialog will appear asking whether you want to start your program. If you check the “Use by default…” checkbox from now on when the USB device is plugged in your program will be started. If you don’t check the checkbox then you will be asked each time the device is plugged in.

A similar procedure can be used for Accessories as detailed in the “Using an intent filter” section here USB Accessory | Android Developers


Alternatively you can use the new HasPermission and RequestPermission methods without requiring any of the above steps. The demo in the archive incorporates both ways of obtaining permission.

EDIT:- Version 2.1 now posted. See post #4 for details

EDIT:- Version 2.2 now posted. See post #14 for details

EDIT:- Version 2.3 now posted. See post #26 for details

V2.4 is available here: http://www.b4x.com/android/forum/th...pports-more-devices.28176/page-11#post-259167
This update adds support for devices connected to multiple USB adapters.


V2.5 is available as an attachment to this post. It is identical to version 2.4 referenced above but adds the required flag for Pending Intents when targeting SDK 31+.

V2.6 is available as an attachment to this post. It is identical to version 2.5 referenced above but adds the required flag for Pending Intents when targeting SDK 34+.
 

Attachments

  • UsbSerial2.3.zip
    99.2 KB · Views: 6,253
  • UsbSerial2.5.zip
    36.3 KB · Views: 928
  • UsbSerial2.6.zip
    36.3 KB · Views: 51
Last edited:

jjspike

Member
Licensed User
Longtime User
I tried v2.4 of USBSerial. Port1 indicated a Qualcomm and port 2 indicated an FTD device. Debug messages stated error opening port 1 and 2. I ran the demo as is therefore everything should be in place.

Thank you!
 

pobss

Member
Licensed User
Longtime User
Hello everyone, I run the example library v2.4 in an Android 4.4 device connected with Prolific 2303, wireless mouse and keyboard, usb to ethernet adapter.
the example works and I can send and receive data from my microcontroller.
the problem is that often the ethernet adapter and the wireless mouse are disconnected so I no longer have control of the device.
does anyone know how I can fix this?
thank you
 

raphaelcno

Active Member
Licensed User
Longtime User
I don't think that you can fix it as the problem is in the internal USB host which is managed by the OS.
Is there an event that can be used to detect a change in the number of USB devices? With such an event it could be possible to close the USB Serial connection and open it again with the new device/port number.
 

raphaelcno

Active Member
Licensed User
Longtime User
Hello everyone, I run the example library v2.4 in an Android 4.4 device connected with Prolific 2303, wireless mouse and keyboard, usb to ethernet adapter.
the example works and I can send and receive data from my microcontroller.
the problem is that often the ethernet adapter and the wireless mouse are disconnected so I no longer have control of the device.
does anyone know how I can fix this?
thank you
Maybe you can try to program your microcontroller to send a simple message to your Android device every second, and your Android app checks when this message arrives. If the app doesn't receive any message for 2-3 seconds, it closes the USB serial connection and opens it again with the new device number.
 

pobss

Member
Licensed User
Longtime User
Maybe you can try to program your microcontroller to send a simple message to your Android device every second, and your Android app checks when this message arrives. If the app doesn't receive any message for 2-3 seconds, it closes the USB serial connection and opens it again with the new device number.

thanks for the answers, when I send a command to the micro I open the connection, sending the command and out. in this way I never problems.
But I need the app to remain even listening to receive data from the micro.
then I open the connection timer wait 1 second to receive data and out.
this is repeated every 30 seconds.
this solution seems to work for the moment.
the device I use is a dongle tv Measy U2A.
 

Bob Sabrook

Member
Licensed User
Longtime User
Hello folks, I am successfully using UsbSerial 2.4 with an STM32 USB port using the usb type DRIVER_CDCACM.
However I am occasionally loosing ASCII space characters (0x20) from my received data (UTF8). Can anyone suggest how I may fix this?
Problem does NOT occur when interfacing with an FTDI type device. It seems like a speed issue but why only spaces?
Also problem does not happen when talking to same device from a PC/Windows, which suggests a framing problem.
 
Last edited:

Bob Sabrook

Member
Licensed User
Longtime User
How are you collecting the data? You should assume that messages will be split into multiple "packets".

like this;

B4X:
Sub do_Astreams_Newdata(Buffer() As Byte,  index As Int) 'index is 1 based port 
    Dim p As port_t = port(index-1) 'p.str will contain received string. p.sent set when string requested
    Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    p.str = p.str & s                           '<============accumulate string
    If p.sent AND p.str.EndsWith(Chr(10)) Then  'when terminator seen
      Label1.Text = p.str         'copy to label
      p.str=""                    'clear cumulative string now used
      p.sent = False              'clear flag that got us here
    End If
End Sub
index is the port in use 1 or 2.
 
Last edited:

Bob Sabrook

Member
Licensed User
Longtime User
Yes, it is wrong (at least in the general case where two messages can be merged).

You can use AsyncStreamsText class: https://www.b4x.com/android/forum/threads/27002/#content

Thank you Erel. I see what you mean now, but I am not using the general case where messages can be merged. I send a command and await a 6kbyte reply ending in linefeed, then silence - until I send another command. I have studied your class and can see it assumes data is a text file and feeds the consumer with whole lines when available. Nevertheless your use of stringbuilder to append new data is interesting and its efficiency might even solve my loss of data.

Occasional Data loss seems to occur after about 600 bytes of 6k bytes and it is not only spaces lost. I could request smaller amounts of data, but that may only reduce the probability of data loss , add complication, and not address the cause of the problem.
 

Bob Sabrook

Member
Licensed User
Longtime User
Experimental observations;
* Adding a delay of 2 milliseconds outside the 'if' statement in the new_data handler above gives near 100% reliability (all 6171 bytes are received in 4 to 10 seconds) - this is only an observation - not a fix.
 
Last edited:

jahswant

Well-Known Member
Licensed User
Longtime User
All my attempts to use this lib are unsucessful both versions still unable to open the usb port...
 

jahswant

Well-Known Member
Licensed User
Longtime User
Yep i used it detects very well my USB Printer but fails to connect to it ...
 

Stevanus

New Member
Licensed User
Longtime User
Hi Erel, is there anyway to connect to USB Device (act as USB Host) but still can do charging for the android device ?
i have project to connect Android Device with serial device, first i plan to use wiznet and wifi, but after b4a launch this usbserial lib, i change my plan, but new problem occur, i cannot charge my android device
 
Top