Hello,
I needs some help from the Android Wizards.
I'm using a Bluetooth barcode scanner in SPP Mode. If the scanner is ready and I start the Activity everything works fine. But when the scanner timeouts (because of battery saving), I cannot get it to reconnect. I have tried so many different methods to get this to work but still no go.
I thought I would post my last version of code here and hope that someone can give my their experience to resolve this issue.
Also, maybe I just missing something here but how do you destroy objects? Example the serial object. I think if you could do this and start over all would be fine.
Right now where it stands is, if the barcode scanner disconnects, it will try to reconnect and will hit the
Sub ScanDevice_Connected (Success As Boolean)
but Success is always False.
Anyway here is the code. Thank you for your help!
PS Some old variables may exist in this code that is not used. Please just focus on the primary objects and methods.
I needs some help from the Android Wizards.
I'm using a Bluetooth barcode scanner in SPP Mode. If the scanner is ready and I start the Activity everything works fine. But when the scanner timeouts (because of battery saving), I cannot get it to reconnect. I have tried so many different methods to get this to work but still no go.
I thought I would post my last version of code here and hope that someone can give my their experience to resolve this issue.
Also, maybe I just missing something here but how do you destroy objects? Example the serial object. I think if you could do this and start over all would be fine.
Right now where it stands is, if the barcode scanner disconnects, it will try to reconnect and will hit the
Sub ScanDevice_Connected (Success As Boolean)
but Success is always False.
Anyway here is the code. Thank you for your help!
PS Some old variables may exist in this code that is not used. Please just focus on the primary objects and methods.
B4X:
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim ScanDevice As Serial
Dim Timer1 As Timer
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.
Private CloseAppTimer As Int
Private Connected As Boolean
Private WaitingToConnect As Boolean
Private BTNScanQuery As Button
Private ScanIDQuery As EditText
Private WebViewQuery As WebView
Private MyScan As ABZxing
Private StatusPanel As Panel
Private ItemHTMLString As String
Dim ast As AsyncStreamsText
Private WaitingConnectCounter As Int
Dim Toggla As Toggle
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")
Log("Activity_Create")
If FirstTime Then
ScanDevice.Initialize("ScanDevice")
Timer1.Initialize("Timer1", 1000)
End If
WaitingToConnect=False
Activity.LoadLayout("queryscreen")
StartScanner(Main.connectedDevice.Name,Main.connectedDevice.mac)
WebViewQuery.Color = Colors.Transparent
If Main.AlphaSKUKeyboard Then
ScanIDQuery.InputType = ScanIDQuery.INPUT_TYPE_TEXT
Else
ScanIDQuery.InputType = ScanIDQuery.INPUT_TYPE_NUMBERS
End If
Activity.AddMenuItem("Connect Scanner", "mnuConnect")
Activity.AddMenuItem("Disconnect Scanner", "mnuDisconnect")
ScanIDQuery.RequestFocus
End Sub
Sub Activity_Resume
Log("Activity_Resume")
If ScanDevice.IsEnabled = False Then
Msgbox("Please enable Bluetooth.", "")
Else
If ScanDevice.IsInitialized Then
ScanDevice.Listen
End If
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
Log("Activity_Pause")
End Sub
Sub StartScanner(Name As String,Mac As String)
Log("StartScanner " & Name & " " & Mac)
If Name<>"" Then
ScanDevice.Connect(Mac)
ScanDevice.Listen
End If
End Sub
Sub ScanDevice_Connected (Success As Boolean)
Log("ScanDevice_Connected" )
If Success Then
ProgressDialogHide
ToastMessageShow("Scanner Connected successfully", False)
ast.Initialize(Me,"ast", ScanDevice.InputStream, ScanDevice.OutputStream) 'initialize AsyncStreamsText with the socket streams.
Connected=True
Else
Connected=False
End If
If Timer1.Enabled = False Then Timer1.Enabled=True
End Sub
Sub Timer1_Tick
If Main.BatterySaver =True Then
CloseAppTimer = CloseAppTimer+1
If CloseAppTimer> 600 Then
' Will enable once everything works.
' Activity.Finish
End If
End If
If Connected=False Then
StartScanner(Main.connectedDevice.Name,Main.connectedDevice.Mac)
End If
End Sub
Sub ast_NewText(Text As String)
ScanIDQuery.Text = Text
ProcessScan
End Sub
Sub ast_Terminated
ScanDevice.Disconnect
ToastMessageShow("Scanner Timeout/Disconnected.",True)
Connected=False
End Sub
Sub ast_Error
ToastMessageShow(LastException.Message, True)
End Sub
Last edited: