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,248
  • UsbSerial2.5.zip
    36.3 KB · Views: 925
  • UsbSerial2.6.zip
    36.3 KB · Views: 41
Last edited:

Stulish

Active Member
Licensed User
Longtime User
hi davelew1s,

Have you tried putting the text into a string first
B4X:
SomeString = "H101" & chr(13) & chr(10)
astreams.Write(SomeString.GetBytes("UTF8"))

Not sure if it helps

Regards

Stu
 

davelew1s

Active Member
Licensed User
Longtime User
Thanks stulish it now works.
One more question can the RTS DTR lines be set from this lib?
Thanks Dave.
 

davelew1s

Active Member
Licensed User
Longtime User
Thanks agraham that's a pity perhaps in the future. I'll have to hard wire them.
thanks again Dave.
 

Rigsby

Member
Licensed User
Longtime User
You can't, the underlying usb-serial-for-android code relies on matching VendorID and ProductID to recognise valid devices. If you don't know them you can see them with my UsbDetails app.

Thanks for that I understand now.

I have several no-name unidentified chip USB/Rs232 cables. Using usb.deviceinfo I can get the vendorID and productID.

If I hardcode these values for the time being as below into one of them that I am guessing is a Prolific(ish) clone....

baud=9600
dev = usb.Open(baud)
usb.SetParameters(baud,usb.DATABITS_8,usb.STOPBITS_1,usb.PARITY_NONE)
usb.SetCustomDevice(usb.DRIVER_PROLIFIC,6790,29987)

Then it always sends at 19200 no matter what you use as the baud.

If you accept the values for vendorID and productID from your table inside the library on detected FTDI and Prolific it works just fine, it is just when you use setCustomDevice.

Now I know you said it may be unpredictable using this function, fair enough I realise that, just letting you know my findings.

Cheers

Rigsby
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
I have an LENOVO 2109 tablet with JB 4.1.1 - but nothing happens when i connect something.
Also when i press start always the same message: no usb device is connected
 

GMan

Well-Known Member
Licensed User
Longtime User
I tried several, FTDI, Prolific and CP2102 /2103 and some others.

i connected a external keyboard from my other tablet (there it works with 2.2 without problems - connect - works)

All in USB debugging is activated and allowed...
 

agraham

Expert
Licensed User
Longtime User
USB debugging is irrelevant here. The USB keyboard should be recognised by Android and should work but will not be visible to programs so is not an indicator as whether USB host mode is supported. It's not clear from your post whether it is working on the Lenovo or not.

It looks like your tablet does not support USB host mode. I assume you are using an OTG cable. Check you have the file android.hardware.usb.host.xml in /system/etc/permissions as mentioned here
http://www.b4x.com/forum/additional...03-usb-rs232-adaptor-driver-5.html#post121125
 

GMan

Well-Known Member
Licensed User
Longtime User
Aha, not found ... several in there, but not this.

Found the solutin in the link but this Lenovo 2109 isnt rooted - so...:sign0148:
 

GMan

Well-Known Member
Licensed User
Longtime User
i contacted the ebay seller of the adapter cable.
he said, i need a cable with OTG support (which this one not have - its only a Adapter for connecting 2 handys).

I bought the new one and will mail what happens ;-
 

GMan

Well-Known Member
Licensed User
Longtime User
Got the new cable - now it works.
Without the
android.hardware.usb.host.xml

Keyboards, Devices - all are found and are working :)
 
Last edited:

ralphie911

Member
Licensed User
Longtime User
Multiple USB devices

I have a tablet with an internal USB wireless device.

When I use the UsbSerial library, it finds the first device only which is on bus 1, device 1. However, the prolific serial adapter which is on bus 2, device 1 it cannot connect to.

The USB Host Controller app finds both devices successfully.

Does anyone know of any tricks to work around this?
 

agraham

Expert
Licensed User
Longtime User
To avoid complication UsbPresent, HasPermission, RequestPermission and DeviceInfo all work with the first USB device they encounter. This is a valid assumption for 99% of cases. However you might get it to work in your case as follows.

In the code for your app take out any permission checks from your app and just Open the port.

Try giving the PL2303 automatic permission by following the steps in the first post of this thread with the manifest additions and the device_filter.xml file. Then, once your app is installed, when you plug in the PL2303 you should be asked something like "Open ... when this USB device is connected". Press OK.

Now your app will open and you may be able to immediately open the port. Check the return from Open which should be UsbSerial.USB_DEVICE if it has succeeded.
 

drponciano

Member
Licensed User
Longtime User
USBSerial2

Is there a simple example of a timer based character reader from a USBSerial connected device? I'd like to have control myself of the bytes received instead of using asyncstream. I'm receiving large amounts of data in*115 or 230kbps so I need complete control. I'll appreciate any help.
 

dune3000

Member
Licensed User
Longtime User
Hi, agraham

Does the library support an event (like USB_CONNECTED) after I give it permission?
 

agraham

Expert
Licensed User
Longtime User
I don't really understand the question but the answer is no. If you explicitly request permission within your app the USB device must already be connected for your app to see it. If you have the intent filter in the manifest and give the "always open" permission when the USB device is first plugged in after app installation then your app will be opened by Android with permission already available.
 

rauldasel

Member
Licensed User
Longtime User
Android 2.3.4

I try tu execute the UsbSerialDemo in a device with Android 2.3.4 and don't works.
I've seen that it's necesary an Android 3.1 or later.

It's possible works in my version? How?

Thanks for replies and sorry for my english
 
Top