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 aboce but adds the required flag for Pending Intents when targeting SDK 31+.
 

Attachments

  • UsbSerial2.3.zip
    99.2 KB · Views: 6,025
  • UsbSerial2.5.zip
    36.3 KB · Views: 665
Last edited:

mrodriguez

Member
Licensed User
Longtime User
What happens when the program crashes? Can you post the logs?


Maybe your program is getting low on memory? What are you doing with the data?

It was device problem, I'm using an android mini-pc. It works on galaxy s4. Thanks!!
 

ernschd

Active Member
Licensed User
Longtime User
I've a question about "RequestPermission": in my opinion there should be always a dialog when calling this function (without adding the changes to the manifest). But when I open the FTDI UART Terminal App, which is requesting permission too, my App doesn't shows the dialog any more. The FTDI App asks every time. Do I have to change something for calling this each time too?
 

pixelpop

Active Member
Licensed User
Longtime User
I am just getting started with this lib, but have a couple of quick questions.

1. The readme file in the 2.3 zip file says "Move both jars and the xml file...." but there is only one jar file in the zip file. This may be because, as stated in post #1, "The library is based on...but no longer needs a separate jar file as the project source code is incorporated in the library." Please confirm.

2. If usb-serial-for-android-v010.jar is no longer needed for this library, should it be deleted from the additional libraries directory?

Thanks...
 

pixelpop

Active Member
Licensed User
Longtime User
I am trying to connect a postal scale to a small Android device. The scale is a "DYMO Pelouze M10 Digital USB Postal Scales, 10lb, USB Connect, PC/Mac Compatible (1772057)" and the device is a "Coby Kyros 4.3-Inch Android 4.0 4 GB Internet Tablet 16:9 Capacitive Multi-Touch Widescreen, Black (MID4331-4)". Of course, to connect the scale to the device I use a female-female adapter to connect the cable from the scale to the cable attached to the device. Both the scale and device are recognized by Windows (without the need for the adapter).

I installed Bridge on the device and compiled the example USB Serial app. However, when I run the app, and click Start, I get the message "No USB device or accessory detected!" I don't even know where to start to get this working, so any help would be greatly appreciated. Thanks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

pixelpop

Active Member
Licensed User
Longtime User
Erel,

Fantastic! I rooted the device and made the mods suggested in the post you referenced. Success! :)

You may want to edit that post due to a small error. In step 3, the instruction is to add the entry <feature name="android.hardware.usb.host"> to the xxx_core_hardware.xml file. I think the entry is missing a "/" before the closing ">" character. The line I added was <feature name="android.hardware.usb.host" />.

You may want to fix this for anyone following in my footsteps.

Thanks again!!!
 

pixelpop

Active Member
Licensed User
Longtime User
Agreed...but <feature...> is not closed properly in step 3).

"3 - open in /system/etc/permissions file named handheld_core_hardware.xml or tablet_core_hardware.xml and add <feature name="android.hardware.usb.host"> into <permissions> section"

Wrong: <feature name="android.hardware.usb.host">
Correct: <feature name="android.hardware.usb.host" />
 

pixelpop

Active Member
Licensed User
Longtime User
I have hit the Realtek WiFi vs. USB wall that has been discussed here and elsewhere. When I began, the USB port was not recognized by the USBSerial 2.3 demo app. I rooted the device and applied the mods suggested above. Now the app recognizes the port, but as a Realtek WLAN.

Has anyone been able to come up with a work-around for this issue? Also, there are many 4.3" and 7" minis in the market. Has anyone had success using USB with any of these other devices (Chromo, Dragon, ProntoTec, etc.)?

Thanks!

Edit: Just for grins, I tried the USBSerial demo app on a Galaxy S3 and old Galaxy 10.1 tablet I had laying around. Both failed (no USB device attached). I didn't bother rooting either of them. The device I am trying to read is a Dymo postal scale with USB output. I'm starting to think my project is dead before it even got started. :-(

Edit2: I installed USB Host Diagnostics app. The results are mixed:

Android API
Claims support - Yes
Classes found - Yes
Device detected - No

Rooted API
Claims support - Yes
Device detected - Yes

Kernel
Claims support - Yes
Device detected - No

Verdict
OS support - No
3rd party apps - No

Any clues here?
 
Last edited:

ernschd

Active Member
Licensed User
Longtime User
Hello Agraham,

after communicating with FTDI i have to enable RTS/CTS Flow Control for my Device to have a proper setup.
Do you have a suggestion how i can activate this parameter with your library?

Thank you and regards.
 

mrodriguez

Member
Licensed User
Longtime User
Hi, is there any way to control 2 ftdi usb2serial modules on the same android device?
I have attached 2 ftdi usb2serial modules to 1 android minipc, just need to send different serial data to each one at the same time.
I dont find the reference to a ftdi module on the code.
Thanks
 

mrodriguez

Member
Licensed User
Longtime User
Agraham might correct me, but I believe that this library uses the first connected USB device that it founds. This means that it will not work with two different USB devices.
Thanks Erel, may be for the next release.
 
Top