Hi Guys,
I'm working on a project that need to connect to a bluetooth barcode scanner using the SPP protocol(I was getting some scanning error using the HID protocol).
I wanna use the scanner in multiple activity.
I was able to connect to my scanner when the serial code was in an activity ( as per SPP exemple found on this web site). the problem is that I cannot use it in other activity.(at lease I dont know How to do it if it is possible to do it)
so I created a code module to execute the scanner sub from every activity.
the problem is that now the connected trigger never get hit and I don't know if the serial is connected or not .
How do you guys do to connect to a Serial Scanner and use multiple Activity?
my startup activity(Main) start my code module called Scanner(see below) this way :
and here is my Scanner Code Module :
thanks for you help once again
Yannick
I'm working on a project that need to connect to a bluetooth barcode scanner using the SPP protocol(I was getting some scanning error using the HID protocol).
I wanna use the scanner in multiple activity.
I was able to connect to my scanner when the serial code was in an activity ( as per SPP exemple found on this web site). the problem is that I cannot use it in other activity.(at lease I dont know How to do it if it is possible to do it)
so I created a code module to execute the scanner sub from every activity.
the problem is that now the connected trigger never get hit and I don't know if the serial is connected or not .
How do you guys do to connect to a Serial Scanner and use multiple Activity?
my startup activity(Main) start my code module called Scanner(see below) this way :
B4X:
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
'try to connect to serial
If Starter.kvs.ContainsKey(Starter.MAC_ADDRESS_Scanner) Then
ToastMessageShow("Tentative de connexion au scanner...",True)
Scanner.ConnectSerial
Else
Scanner.ShowPairedDevices
End If
End Sub
and here is my Scanner Code Module :
B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim serial1 As Serial
Dim AStream As AsyncStreams
Dim uiSender As uiSender
End Sub
public Sub ConnectSerial
serial1.Initialize("Serial1")
serial1.Connect(Starter.kvs.Get(Starter.MAC_ADDRESS_Scanner))
End Sub
public Sub ShowPairedDevices
Dim PairedDevices As Map
PairedDevices = serial1.GetPairedDevices
Dim l As List
l.Initialize
For i = 0 To PairedDevices.Size - 1
l.Add(PairedDevices.GetKeyAt(i))
Next
If l.Size=0 Then
l.Add("No device(s) found...")
End If
Dim res As Int
res = InputList(l, "Choose device", -1) 'show list with paired devices
If res <> DialogResponse.CANCEL Then
If l.Get(res)="No device(s) found..." Then
Return 'Just
Else
Starter.kvs.Put(Starter.MAC_ADDRESS_Scanner,PairedDevices.Get(l.Get(res))) 'Store selection for further use
serial1.Connect(Starter.kvs.Get(Starter.MAC_ADDRESS_Scanner)) 'convert the name to mac address and connect
End If
End If
End Sub
Sub AStream_NewData (Buffer() As Byte)
Log("Received: " & BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
If uiSender <> Null Then
Log("Writing to " & uiSender.Tag)
uiSender.Text = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
End If
End Sub
Sub AStream_Error
Log("Connection broken...")
AStream.Close
serial1.Disconnect
ShowPairedDevices
End Sub
Sub AStream_Terminated
Log("Connection terminated...")
AStream_Error
End Sub
Sub Serial1_Connected (Success As Boolean)
If Success = True Then
ToastMessageShow("Scanner Connecté",False)
Log("Scanner is now connected. Waiting for data...")
AStream.Initialize(serial1.InputStream, serial1.OutputStream, "AStream")
Else
If Starter.kvs.ContainsKey(Starter.MAC_ADDRESS_Scanner) Then
Log("Trying to connect to mac address : " & Starter.kvs.Get(Starter.MAC_ADDRESS_Scanner))
If Starter.firsttry Then
Starter.firsttry = False 'set to false has we have first try it once
ToastMessageShow("Tentative de connexion au scanner...",True)
serial1.Connect(Starter.kvs.Get(Starter.MAC_ADDRESS_Scanner)) 'convert the name to mac address and connect
Else
Dim result As Int = Msgbox2("Connection Impossible, Verifiez que le scanner est allumé. Voulez vous réessayer ?","Erreur de connexion","Oui","Annuler","Choisir un autre scanner",LoadBitmap(File.DirAssets,"new Icon.jpg"))
Select Case result
Case DialogResponse.POSITIVE
Log("Trying to reconnect Scanner once again...")
serial1.Connect(Starter.kvs.Get(Starter.MAC_ADDRESS_Scanner)) 'convert the name to mac address and connect
Case DialogResponse.NEGATIVE
Log("User want to change Scanner MAC Address")
ShowPairedDevices
Case DialogResponse.CANCEL
Log("Cancel selected so return")
Return
End Select
End If
Else
Log("As no scanner already connected ... ask user to choose a scanner")
ShowPairedDevices
End If
End If
End Sub
thanks for you help once again
Yannick