Android Question Barcode scanning and keyboard input

nibbo

Active Member
Licensed User
Longtime User
Hello again, more questions...
I have paired with Bluetooth barcode reader which I have changed to serial mode to allow the keyboard and scanner to be used together. How do I detect when something has been scanned and retrieve the scanned data?
Thanks
 
Last edited:

nibbo

Active Member
Licensed User
Longtime User
You should connect to the scanner with the serial library and then communicate with it. You should check the scanner documentation for the exact details (protocol, UUID channel, ...).
Thanks Erel, I have this working now.
 
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
As I said, I do have this working.
I am curious though, why do I have to make the Bluetooth device discoverable before the serial.connect when the device is already paired.
 
Upvote 0

santiago

Member
Licensed User
Longtime User
Hello Nibbo :

I have the same problem like you .
I have a bluetooth barcode scanner . When I paired it with the android device, a smarphone, it works fine but I am not able to use software keyboard.
The scanner has several ways to work : SPP and HID.
When I try the Bluetooth software that Erel wrote , when I communicate with the device it finds the scanner but

If I use the serial library to connect using the example Erel wrote too , when the barcode reader is linked the software stops (well, do nothing and I must stop it )

Have you solved the problems?
Could you give me some information about?
Thanks for your time

Santiago
 
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
Hi Santiago,
The scanner must be set to SPP mode and then you need listen for input.

To achieve this I did the following:

In process globals declare a serial device and an async stream thus:

Dim scanner As Serial
Dim barcodeData As AsyncStreams

When the activity loads I call a sub which tries to find the barcode reader and connect with it:

scanner.Initialize("scanner")
If scanner.IsEnabled Then
Dim devices AsMap = scanner.GetPairedDevices
For i = 0 To devices.Size - 1
Dim deviceName AsString = devices.GetKeyAt(i)
If deviceName.StartsWith("OPN") Then
scanner.Connect(devices.GetValueAt(i))
End If
Next
end if

Obviously I have a lot more code to display errors if this does not work...

The serial has a connected event so we code this to check we could connect:
If it connects OK then we set it to listen and initialise the async serial object to receive the data
Sub scanner_Connected (Success As Boolean)
If Success Then
ToastMessageShow("Barcode scanner connected OK", False)
scanner.Listen
barcodeData.Initialize(scanner.InputStream,scanner.OutputStream,"barcodeData")
Else
error.....

The barcode object will have a new data event which we use to detect a scan and extract the scanned barcode:

Sub barcodeData_NewData (Buffer() AsByte)

Dim barcode As String = ""
barcode = BytesToString(Buffer, 0, Buffer.Length, "UTF8")

Hope this helps.
 
Last edited:
Upvote 0

santiago

Member
Licensed User
Longtime User
Dear Nibbo :

Thanks for your answer .

I left the problem away for a couple of days . Tomorrow I will "investigate" again.
But , a priori, I tested the code you sent me . (well ,a similar one )
I will try your code and I will tell you about .

Thanks again . Sorry I did not replay this post before but ,like I told you before, the last days I did not work around this problem.

Sincerely,
Santiago
 
Upvote 0

GaNdAlF89

Active Member
Licensed User
Longtime User
Hi Santiago,
The scanner must be set to SPP mode and then you need listen for input.

To achieve this I did the following:

In process globals declare a serial device and an async stream thus:

Dim scanner As Serial
Dim barcodeData As AsyncStreams

When the activity loads I call a sub which tries to find the barcode reader and connect with it:

scanner.Initialize("scanner")
If scanner.IsEnabled Then
Dim devices AsMap = scanner.GetPairedDevices
For i = 0 To devices.Size - 1
Dim deviceName AsString = devices.GetKeyAt(i)
If deviceName.StartsWith("OPN") Then
scanner.Connect(devices.GetValueAt(i))
End If
Next
end if

Obviously I have a lot more code to display errors if this does not work...

The serial has a connected event so we code this to check we could connect:
If it connects OK then we set it to listen and initialise the async serial object to receive the data
Sub scanner_Connected (Success As Boolean)
If Success Then
ToastMessageShow("Barcode scanner connected OK", False)
scanner.Listen
barcodeData.Initialize(scanner.InputStream,scanner.OutputStream,"barcodeData")
Else
error.....

The barcode object will have a new data event which we use to detect a scan and extract the scanned barcode:

Sub barcodeData_NewData (Buffer() AsByte)

Dim barcode As String = ""
barcode = BytesToString(Buffer, 0, Buffer.Length, "UTF8")

Hope this helps.
Hi, I have the same problem.
I tried nibbo's example but "success" in the scanner_Connected event is always false. Can someone help me? My barcode reader is OPN-3002n. Thanks a lot.

Edit: I tried with ConnectInsecure and, for the first time is success=false, if I try again it is true. But when I read a barcode, nothing happens in asyncstream. What is the problem?
 
Last edited:
Upvote 0

GaNdAlF89

Active Member
Licensed User
Longtime User
@nibbo
What devices have you used (Android device and barcode reader)?
From this code
B4X:
If deviceName.StartsWith("OPN") Then
I think you are using an Opticon reader...what is the model? Thanks a lot.
 
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
Hi Gandalf, it is an Opticon OPN-2002.
Its is sort of OK but a bit fiddly to pair and you have to keep put it into pairing mode before the app tries to connect to it.
Other than that it is OK but I found more inexperienced users got very frustrated with it.
 
Upvote 0

GaNdAlF89

Active Member
Licensed User
Longtime User
Hi Gandalf, it is an Opticon OPN-2002.
Its is sort of OK but a bit fiddly to pair and you have to keep put it into pairing mode before the app tries to connect to it.
Other than that it is OK but I found more inexperienced users got very frustrated with it.
@nibbo
Thanks for reply, I have an OPN-3002n, I can not connect to it (the serial connected event always has success = false). Can you explain me how do you do the bluetooth pairing on Android (before the connection with your app) and then the connection from your app? Thanks
 
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
Not entirely sure if it will be the same.
With the 2002 you have to scan a code in the quick-start guide which puts it into serial port mode (SPP).
Then hold the little button down for 5 seconds until it beeps and the led starts flashing blue.
Start Bluetooth scanning on the android device to discover it.
If a PIN code is needed then it will be last for characters of the device name as shown in the Bluetooth scan.
Hope this helps, if not then you should be able to download the quick-start guide from their website.
Good luck.
 
Upvote 0

GaNdAlF89

Active Member
Licensed User
Longtime User
Not entirely sure if it will be the same.
With the 2002 you have to scan a code in the quick-start guide which puts it into serial port mode (SPP).
Then hold the little button down for 5 seconds until it beeps and the led starts flashing blue.
Start Bluetooth scanning on the android device to discover it.
If a PIN code is needed then it will be last for characters of the device name as shown in the Bluetooth scan.
Hope this helps, if not then you should be able to download the quick-start guide from their website.
Good luck.
I've done exactly what you described me, but, as I said previously, I always obtain Success=False in Serial_Connected event, also with code you've posted above...Any idea?
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Here you go @GaNdAlF89 and anybody else that needs this code to connect to a Bluetooth barcode scanner.

FULL CODE: Make sure that you enable SPP on your scanner.
You need the following libraries, RandomAccessFile library and the Serial library.
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim Scanner As Serial
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim BarcodeData As AsyncStreams
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    Scanner.Initialize("Scanner")
End Sub

Sub Activity_Resume
    CallSub(Null, ScanForBC)
End Sub

Sub Activity_Pause(UserClosed As Boolean)
    Scanner.Disconnect
End Sub

Sub ScanForBC
    If Scanner.IsEnabled Then
        Dim Devices As Map = Scanner.GetPairedDevices

        For i = 0 To Devices.Size - 1
            Dim DeviceName As String = Devices.GetKeyAt(i)

            If DeviceName.StartsWith("CT10") Then ' CT10 = The start of my barcode scanner name(CT1013720208)
                Log(Scanner.Name)
                Log(Devices.GetValueAt(i))
                Scanner.Connect2(Devices.GetValueAt(i), "00001101-0000-1000-8000-00805F9B34FB")
            End If
        Next
    End If
End Sub

Sub Scanner_Connected(Success As Boolean)
    If Success Then
        Scanner.Listen
        BarcodeData.Initialize(Scanner.InputStream, Scanner.OutputStream, "BarcodeData")
        ToastMessageShow("Barcode scanner connected OK", False)
    Else
        ToastMessageShow("Connection error.", False)
    End If
End Sub

Sub BarcodeData_NewData(Buffer() As Byte)
    Dim Barcode As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    ToastMessageShow(Barcode.Trim, False)
End Sub

Sub BarcodeData_Error
    'ToastMessageShow("Data error.", False)
End Sub

It's easy enough to add a small popup ListView or whatever you want to use, so that a list of devices is first shown. From there you can just click on the device(barcode scanner name) and your selected barcode scanner will then connect to your app directly.

How I select a barcode scanners in my app...
Screenshot_2015-05-06-18-19-07.png


Enjoy...
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Hello @GaNdAlF89 did you actually copy and past my code from above?
Have you made sure that your barcode scanner is set to SPP mode enabled?
Also in the barcode scanner device properties in your Android settings, try removing the check box for the barcode scanner that says 'Input Device' (leave it blank), make sure it is unchecked, well I always do just to be on the safe side.

After checking the specifications on your very expensive OPN-3002n handdeld scanner, I can't see any reason why the above code should not work @GaNdAlF89...
 
Last edited:
Upvote 0
Top