Android Question Bluetooth and Barcode SPP issue

EduardoElias

Well-Known Member
Licensed User
Longtime User
Hi All

following this example:

https://www.b4x.com/android/forum/t...bluetooth-scanner-via-spp-mode.83490/#content

I could successfully connect and read from a Bluetooth barcode scanner.

The problem is that scanner goes to sleep I loose the connection.

When the scanner is in keyboard emulator mode, any time the user wants to read the barcode it just scan it and it is fine. However it have another problems for me (it is causing to crash the application, and sometimes I need wire free scanners)

So, is there a way to reconnect without having the user to click on a button on the tablet to reconnect?
 

KMatle

Expert
Licensed User
Longtime User
Change the code a bit and store the Scanner's MAC address.

In SPP mode:

If the scanner goes to sleep the connection is broken and AStream_Error will be called

B4X:
Sub AStream_Error
    Log("Connection broken...")

    AStream.Close
    serial1.Disconnect

    START TIMER HERE
End Sub

You can then start a timer and try to reconnect every 2 secs or so and in the Sub Timer_Tick with

B4X:
serial1.Connect(ScannerMACAddress)

B4X:
Sub Serial_Connected (success As Boolean)
    If success = True Then
       Log("Scanner is now connected. Waiting for data...")
       AStream.Initialize(serial1.InputStream, serial1.OutputStream, "AStream")
       STOP THE TIMER HERE
    End If
End Sub

If it's connected (woke up) everything is fine. If not, it's ok (timer is till runing and will retry every x secs)

Stop the Timer in Pause and start it in Resume
 
Upvote 0

EduardoElias

Well-Known Member
Licensed User
Longtime User
MANY THANKS KMATLE !!

did y ou have any experience with usb barcode readers? for me it could be also interesting to connect with them, I have a tablet with one connected as keyboard. However, it crash the program in a way that cant catch on the debugger. I receive all the numbers by Activity_KeyPress, I see that all the number are there, when comes the ENTER in the sequence the program crashes, the app find the item by barcode and do everything needed, but crashes when comes back from the Activity_KeyPress.

Just one tablet (motorola xoom) it works fine.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
usb barcode readers

I don't own one but it works (as you mentioned) as a simple keyboard. Did you try it with the BT Scanner in "keyboard" mode? It's then acting like a USB scanner as a keyboard.

but crashes when comes back from the Activity_KeyPress

This will for sure be logged (uncheck "filter" in the logs). Can you post the relevant code?
 
Upvote 0
Top