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,011
  • UsbSerial2.5.zip
    36.3 KB · Views: 644
Last edited:

agraham

Expert
Licensed User
Longtime User
Which "install instructions" are giving you trouble? This library installs like any other library by moving the jar and the xml file to your Additional Libraries folder, though I have just noticed that there is a typo in the Readme.txt file where jars is plural rather than singular - a hangover from the original UsbSerial library where the drivers were in a separate jar file.
 

jimTaylor

Member
Licensed User
Longtime User
Tried to run before I could walk

I backed up to the beginning and started the b4a beginners guide again. I never created the Additional Llibraries folder, so I couldn't find a place to put the stuff.

I installed it and tried the example. when I click on btnOpen I get

RuntimeException: Cannot parse: 0 as boolean.

Another problem: I am connected to my tablet with a USB cable, but I see nothing on the screen. The action is all on the virtual tablet, not the real one.
 

jimTaylor

Member
Licensed User
Longtime User
Still having problems

I just plugged my phone into a USB port on the tablet. The tablet tried to charge my phone and download my pictures, so it seems to me that the tablet thinks it's a USB host. My phone thinks so, too, because it said it was receiving power from the tablet.

I have a USB-to-serial cable plugged in to the other USB port on the tablet, with a loop-back plug at the serial port end. I know the converter is good, because I have been using it to talk to my serial devices from my PC. What I am trying to do is use the tablet to control the devices instead of the PC. The PC code is in VB, so I figured it would be a piece of cake to move the project to b4a, but I'm have a rough time getting past step one.

Assuming I can find a way to make this work, does the Android USB-to-serial code have a way to set the parameters of the link? The VB code I'm running, and the hardware I'm controlling, would like to run at 115200 baud.

Thanks for your help. I need it, if only to make me stop and think about what I'm doing.
 

jimTaylor

Member
Licensed User
Longtime User
Error on Open

I have an FTDI USB-to-Serial cable connected. The usb.RequestPermission() function work as described, and usb.DeviceInfo() returns

Manufacturer: FTD
Product: TTL
Serial: FTG

DeviceName: /dev/bus/usb/001/006
DeviceClass: USB_CLASS_PER_INTERFACE
DeviceSubClass: 0
DeviceID: 0x3EE
ProductId: 0x6001
VendorID: 0x403

B4aInterfaceNumber: 0
InterfaceClass: USB_CLASS_VENDOR_SPEC
InterfaceSubClass: 255
InterfaceProtocol: 255

plus a whole lot of other information about endpoints.

Then the program hangs up at the same place it did before, only now instead of complaining that it can't interpret 0 as boolean, it says the same about 1.

Any suggestions?
 

agraham

Expert
Licensed User
Longtime User
Are you using the unmodified demo from the UsbSerial2.3.zip archive and not an earlier version for the original UsbSerial library? It looks suspiciously like a test for successfully opening the port might be expecting a Boolean return from Usb.Open which was changed to return an Int in UsbSerial2.
 

jimTaylor

Member
Licensed User
Longtime User
Error opening USB port

I re-downloaded the usbSerial 2.3 package, replaced the jar and xml files in my additional libraries folder, and put the files and objects folders and the b4a file in a new folder. The I fired up the bridge and tried to run the program. With a little prodding I got it installed and started.

The Start and Exit buttons have bright text, the Stop and Send buttons are grey on grey, hard to see. When I click on Start, I get a display of the USB Device Information, which says, basically, that the provenance of the device is unknown, but it has three endpoints, one for interrupts and two for bulk transfer. Tapping OK at the bottom of the DI list returns to the initial state, and adds "Error opening USB port" to the log.

I replaced the FTDI serial cable with one I got from RadioShack. The DI listing showed different packet sizes, but was otherwise the same. When I disconnected one cable and attached the other, the log added "Activity (main) Pause," "Activity (main) Resume," "Checking permission," "Error opening USB port," and "Checking permission." When I tapped "OK," it added "Error opening USB port" and returned to the initial state.

For my third try I used a USB to RS485 converter from USConverters. It just worked, reporting "Connected successfully" and three copies of "NewData" and "abcde"

Now to figure out why it loves the FTDI chip in the USConverters part and hates the two RS232 cables.

Thanks immensely for you patience and help!
 

Bill

Member
Licensed User
Longtime User
michw to determine is your goclever capable of working with this library go to the google market and install "usb host diagnostics".
Thank you so much for that I have been trying to get the sample basic for android usb application to run for days duuuh.... one run of USB host diagnostics said found a problem do you want to fix it...and it did, didn't tell me what it was though, but the app works now so....Yeahhh!
 
Last edited:

Gaver Powers

Member
Licensed User
Longtime User
I read in this thread where others have had success at connecting an Android Device to an Arduino UNO board. Could someone please share what cable they used for the connection? My Arduino does not have the standard USB female connector on the board - Mine is more like a printer usb connector. So... I'm thinking the OTG cable will not work for connecting between the two.

I'm trying to connect a Samsung Galaxy Tab 7 to the Arduino.

UPDATE: For others that might be following this thread in an attempt to connect an Arduino UNO to a Samsung Tablet. The standard OTG Cable mentioned through out this thread is not compatible with the Samsung Tab 2 - 7 or 10 inch tablets. It's also unclear if the 8 Gigs of memory is adequate to store the program or if an SD card is still required.
 
Last edited:

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
help...
sorry for the lame question but i am really in a "needy" situation...

can this library be used to READ data from the audio jack ?
what i am looking for (urgently) is to be able to read a magnetic card using a reader attached to the audio jack.
i have the reader but can't read the swipped card
anyone ?
thanks
 

jcrouchley

New Member
Licensed User
Longtime User
This is a great library and as of 2.3 it supports my TI CC1111 based devices. Only one issue - I seem to get all my data twice. I am using the UsbSerialDemo code with the addition of
usb.SetCustomDevice(usb.DRIVER_CDCACM,0x0451,0x16A6)
I am getting two calls into Astreams_NewData for every packet with exactly the same data. A separate (PC based) monitor is showing the data correctly.
Any ideas?
 

jcrouchley

New Member
Licensed User
Longtime User
OK Modified the demo app to log to a label instead and it is OK - so must be something to do with logging. No longer an issue for me.
 

Thuong

Member
Licensed User
Longtime User
Good job !!! thank
I can connect Android tablet ONDA Vi60 to Arduino UNO r3
 

Thuong

Member
Licensed User
Longtime User
Could someone please share what cable they used for the connection? My Arduino does not have the standard USB female connector on the board - Mine is more like a printer usb connector. So... I'm thinking the OTG cable will not work for connecting between the two.
I'm trying to connect a Samsung Galaxy Tab 7 to the Arduino.

The fist , you should have OTG cable (has USB A female)
Second, you check USB host API is presented on your Samsung Galaxy Tab 7 (by USB host diagnostic from google store). Your galaxy should has USB Host full support
simple, you plug OTG cable to Galaxy Tab ---> to "printer" USB cable ---> to Arduino UNO
 
D

Deleted member 103

Guest
The example "UsbSerialDemo" runs correctly on my Nexus 7 (Android 4.3), which means the USB device is recognized.
On a Samsung GT-P5110 (Android 4.1.2) but is only the error message "No USB device or accessory detected".
What can it be?
 
Top