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,273
  • UsbSerial2.5.zip
    36.3 KB · Views: 953
  • UsbSerial2.6.zip
    36.3 KB · Views: 101
Last edited:

Ferraz71

Member
Licensed User
Longtime User
I would like to use comunication from usb port to control i2c peripherals, like ADC´s and GPIOS expanders, without external microcontrolers. The android apk, will do control trough a USB port, with only an FT232H IC or an MCP2221 IC like bridge. Is it possible with SerialUSB Library?
 

Creideiki

Active Member
Licensed User
Longtime User
The source is in the zip file...
I need the possibility to use ASyncStreams; felUsbSerial doesn't seem to use them.

I have an adapter with a FT232R, but it throws exceptions (e.g. java.lang.NullPointerException), so I have to figure out what happens.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I need the possibility to use ASyncStreams; felUsbSerial doesn't seem to use them.
felUsbSerial implements a similar solution internally. If you are using AsyncStreamsText then you can use the modified version that I posted (I will help you find it).

If you have the source code then you should be able to build it.
 

Creideiki

Active Member
Licensed User
Longtime User
The same code uses Bluetooth with AsyncStreams, so that should work with UsbSerial, too.

Is there a HowTo, how to make a B4A-library from java source code?
 

DonManfred

Expert
Licensed User
Longtime User

BarryW

Active Member
Licensed User
Longtime User
How to remove the message box after the connection is successfull.

I try to connect a arduino to my phone the executes the program. It works fine but there is a messagebox if the connection success.

Tnx.
 

slugger

Member
Licensed User
Longtime User
Hi,

I have been using this library in one of my apps for the last 2 years and it worked just fine in my old smartphone running Android 4.3.

Basically there is a foreground service which reads and writes from/to the USB to RS232 adapter and everything works Ok even when the screen turns off.

Now I am trying to run it on a new smartphone with Android 6.0.1 on it.

The very same APK stops working when the screen turns off.

At that point the USBPRESENT always returns no adapters even if I use a timer to periodically check for the presence of the USB-RS232 adapter.

When I turn the screen back on the adapter is found and the serial communication resumes.

The only solution I have found is to use the Phone library to keep the screen always on but although the backlight is dimmed it doesn't do too much good to the screen and the battery life.

Is it a new feature of Android 6 ?

Any workarounds?


Thank you in advance.
 

jahswant

Well-Known Member
Licensed User
Longtime User
Basically there is a foreground service which reads and writes from/to the USB to RS232 adapter and everything works Ok even when the screen turns off.
I think this is the best moment to pass either to a background service or use a different thread for the background task.
 

slugger

Member
Licensed User
Longtime User
Thanks for the replies.

I use the PhoneWakeState's PartialLock and KeepAlive to keep the screen ON and dimmed.

If I use only the PartialLock the screen goes OFF and then the library doesn't find the adapter anymore...when I turn the display back on I am greeted again with the Android's request to grant USB permission to the application and then the communication starts to work again.

The same APK installed in my old smartphone running Android 4.3 works perfectly even after the display turns off.

I guess there's something in Android 6 that deactivates the USB permission granted to the app as soon as the screen goes off.
 

rtek1000

Active Member
Licensed User
Longtime User
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.


App example for test of USB Serial Port cable

Tested with FT232LR and PL2303 cables


(for personal non-commercial use only)

You can use this for test Arduino board, PIC (Microchip) and other systems!
 

Attachments

  • USBserial_example.zip
    10 KB · Views: 424
  • Online.png
    Online.png
    34.4 KB · Views: 387
  • Offline.png
    Offline.png
    36.4 KB · Views: 376
Last edited:

GaNdAlF89

Active Member
Licensed User
Longtime User
Hi to all! I have an usb barcode reader but it is not detected from this library (usbReader.UsbPresent(1) is equal to usbReader.USB_NONE). I think the problem is that the device is like a keyboard (it needs the focus on an editText).
How can I solve the problem? Or, if the problem is the model of my usb barcode reader, how can I find a device to work correctly with this library? I need asynchronous reading. Thanks!
 
Last edited:

rtek1000

Active Member
Licensed User
Longtime User
Hi to all! I have an usb barcode reader but it is not detected from this library (usbReader.UsbPresent(1) is equal to usbReader.USB_NONE). I think the problem is that the device is like a keyboard (it needs the focus on an editText).
How can I solve the problem? Or, if the problem is the model of my usb barcode reader, how can I find a device to work correctly with this library? I need asynchronous reading. Thanks!

Hello,

I believe a Serial USB library is not for your case if it's like a keyboard.

Well, if it's like a keyboard, try to see it in the Android Menu, Settings> Language and Input.

I believe that if you use a common keyboard, maybe you need to define a key function like F2, F6 etc, or by touchscreen to set the focus.

Does your scanner send initial string?
 

rtek1000

Active Member
Licensed User
Longtime User
Hi to all! I have an usb barcode reader but it is not detected from this library (usbReader.UsbPresent(1) is equal to usbReader.USB_NONE). I think the problem is that the device is like a keyboard (it needs the focus on an editText).
How can I solve the problem? Or, if the problem is the model of my usb barcode reader, how can I find a device to work correctly with this library? I need asynchronous reading. Thanks!

I think you will need to start a new thread
 

drponciano

Member
Licensed User
Longtime User
I am in need to acquire a continuous stream of byte data from an Serial Device (that I made myself, although AVR MCU based,
Using a FTDI chip in between the Android Tablet and the Device)
The application should continuously receive bytes in range 0x00 to 0xF9, 0xFA is acting as a EOL mark, and draw graphics according to this data.
The issue is the following:
Since the device is sending bytes (data) continuously and might have been turned on prior the connecting to the Tablet,
after the App connects to the external device (by plugging in the micro-B USB cable), The program (UsbSerialDemo) starts, I use the Start Button, the Stop and Send buttons become enabled and then it all goes immediately to the astreams_Error subroutine and displays the following error message:
(IOException) java.io.IOException: Expected at least 2 bytes
My question is: is there a workaround in order to solve this problem and achieve my goal:
Reading the Serial data without ending up in this exception (only) indicating Subroutine (AStreams_Error) ?
(Since I am a newbie in Android and BasicForAndroid, my wildguess is that AsyncStreams are using UTF8 and it expects 2 bytes for each character/byte (of the stream received)

There is an aside question: How could one ignore/process such an exception, since Try..Catch..End Try does not apply here (the exception raises for its reasons, and I cannot trap it) ?


I hope someone could help me out (pray)
Hello Krokiz! Did you get your program workin with FTDI? I have the exact same problem with the 2 bytes error. Any help?
 
Top