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,268
  • UsbSerial2.5.zip
    36.3 KB · Views: 943
  • UsbSerial2.6.zip
    36.3 KB · Views: 83
Last edited:

rtek1000

Active Member
Licensed User
Longtime User
Well, I do not have much to add, recently I asked Samsung's support how to make the SD card work as an internal storage and they did not respond to me, they just changed the wizard until the connection ended and no one knew how to do it .

S7 have some related problems on the internet, such as slow battery charging, USB file transfer and other.

Have you tried using a USB stick to check if the USB host works well?
 

drponciano

Member
Licensed User
Longtime User
Yes, the OTG addapter is working OK. Using the FTDI UART Terminal y see my data coming fast but not so with my program.
 

rtek1000

Active Member
Licensed User
Longtime User
UsbSerial Ver 2.4
What's new: Support for multiple USB devices
Version history Updated. You can find the changes by searching "Ver_2.4"
Maybe I over documented it. It was for me... feel free to remove the Nonsense
Library and Example updated to Ver 2.4
hoho driver also updated to support multiple devices

Hi,

Demo not work:
UsbSerialDemo.b4a and b4a v7 returns error (see image please)
usb array.jpg


B4X:
If usb1.UsbPresent(1) = usb1.USB_NONE Then    ' Ver_2.4

Error description: Array expected.
Occurred on line: 40
If usb1.UsbPresent(1) = usb1.USB_NONE Then' Ver_2.4
Word: (
 

Emiliano Cejas

Member
Licensed User
Longtime User
Hi! demo is not working. I use b4a version 3.82.

It opens the app, then i click on "Start 1" and ask for permision, click ok (grant permission)... then nothing happens.

If i click again on "Start 1", then it displays info about the adapter (FT230). but then nothing happens, it doesnt receive anything (the device over serial is sending info). And it doesnt enable the "stop 1" and "send 1" buttons.

Do i have to do other steps with my b4a version?

thanks!
 

rtek1000

Active Member
Licensed User
Longtime User
Hi! demo is not working. I use b4a version 3.82.

It opens the app, then i click on "Start 1" and ask for permision, click ok (grant permission)... then nothing happens.

If i click again on "Start 1", then it displays info about the adapter (FT230). but then nothing happens, it doesnt receive anything (the device over serial is sending info). And it doesnt enable the "stop 1" and "send 1" buttons.

Do i have to do other steps with my b4a version?

thanks!
Can not you use the latest version?
 

Emiliano Cejas

Member
Licensed User
Longtime User
update! tested demo with another USB-SERIAL cable that uses the FT232 instead of FT230 and it works. Could you add support for FT230 chip?

thanks
 

hayk

Member
Hi folks. sorry for dumb question, but Whats the purpose of "src" folder in app example zip file. Where to put the drivers?
 
Last edited:

biometrics

Active Member
Licensed User
Longtime User
Hi folks. sorry for dumb question, but Whats the purpose of "src" folder in app example zip file. Where to put the drivers?
I've checked my Additional Libraries folder and I only have UsbSerial.jar and UsbSerial.xml. I guess src is the Java source code.
 

peacemaker

Expert
Licensed User
Longtime User
It's useful to know that the demo app is in v.2.4 version link.
 

peacemaker

Expert
Licensed User
Longtime User
For reference: "Serial USB terminal" app has such devices filter:
Maybe useful.
B4X:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <usb-device product-id="24577" vendor-id="1027" />
    <usb-device product-id="24592" vendor-id="1027" />
    <usb-device product-id="24593" vendor-id="1027" />
    <usb-device product-id="24596" vendor-id="1027" />
    <usb-device product-id="24597" vendor-id="1027" />
    <usb-device product-id="60000" vendor-id="4292" />
    <usb-device product-id="60016" vendor-id="4292" />
    <usb-device product-id="60017" vendor-id="4292" />
    <usb-device product-id="8963" vendor-id="1659" />
    <usb-device product-id="9123" vendor-id="1659" />
    <usb-device product-id="9139" vendor-id="1659" />
    <usb-device product-id="9155" vendor-id="1659" />
    <usb-device product-id="9171" vendor-id="1659" />
    <usb-device product-id="9187" vendor-id="1659" />
    <usb-device product-id="9203" vendor-id="1659" />
    <usb-device product-id="21795" vendor-id="6790" />
    <usb-device product-id="29987" vendor-id="6790" />
    <usb-device vendor-id="9025" />
    <usb-device product-id="1155" vendor-id="5824" />
    <usb-device product-id="8260" vendor-id="1003" />
    <usb-device product-id="4" vendor-id="7855" />
    <usb-device product-id="516" vendor-id="3368" />
    <usb-device product-id="22336" vendor-id="1155" />
</resources>
 
Top