sub Process_globals
Public printer_admin As BluetoothAdmin
Public printer_AStream As AsyncStreams
Public printer_serial1 As Serial
Public printer_foundDevices As List
Public PrinterList as List
Public connectedPrinterDevice As NameAndMac
end sub
Sub HasPrinter(Paireddevices As Map,sPrinter As String) As Boolean
Dim i As Int
Dim bFound As Boolean=False
Dim sKey As String
For i =0 To Paireddevices.Size-1
sKey=Paireddevices.GetKeyAt(i)
If LTrim(sKey)=LTrim(sPrinter) Then
bFound=True
End If
Next
Return(bFound)
End Sub
Sub LTrim(s As String) As String
Dim m As Matcher = Regex.Matcher("^(\s+)", s)
If m.Find Then
Return s.SubString(m.GetEnd(1))
Else
Return s
End If
End Sub
Sub PrinterSerial_Connected(Success As Boolean)
If Success Then
Main.bGlobalPrinterSerialConnected=True
If printer_AStream.IsInitialized = False Then
printer_AStream.Initialize(printer_serial1.InputStream,printer_serial1.OutputStream,"PrinterAStream")
End If
Log("Printer connected")
Else
Main.bGlobalPrinterSerialConnected=False
Log("Printer did not connect")
End If
End Sub
Sub ConnectToPrinterBlueTooth()
Try
CallSubDelayed2(Main,"DisplayProgressDialogShow","Connecting to Printer Blue Tooth")
Dim PairedDevices As Map
If printer_serial1.IsInitialized=False Then
printer_serial1.Initialize("PrinterSerial")
EndIf
PairedDevices=printer_serial1.GetPairedDevices
If HasPrinter(PairedDevices,"GD200P")=False Then
'CallSubDelayed2(Main,"DisplayServiceErrorMessage","You must pair the Blue Tooth Printer Manually then try again")
If printer_admin.IsInitialized=False Then
printer_admin.Initialize("PrinterAdmin")
End If
If printer_foundDevices.IsInitialized=False Then
printer_foundDevices.Initialize
End If
printer_foundDevices.Clear
If printer_admin.StartDiscovery = False Then
CallSubDelayed2(Main,"DisplayProgressDialogShow","Error start discovery printer process")
Else
CallSubDelayed2(Main,"DisplayProgressDialogShow","Searching for printer devices")
End If
Else
Dim oPDList As List
oPDList.Initialize
Dim nm AsNameAndMac
If nm.IsInitialized=False Then
nm.Initialize
End If
For i =0 To PairedDevices.Size-1
nm=PairedDevices.get(i)
If LTrim(PairedDevices.GetKeyAt(i))="GD200P" Then
oPDList.Add(PairedDevices.GetKeyAt(i))
End If
Next
Dim bFound As Boolean=False
For i =0 To oPDList.size-1
printer_serial1.Connect(PairedDevices.Get(oPDList.get(i)))
CallSubDelayed2(Main,"SetPrinterLabel",printer_serial1.name & " " & printer_serial1.address)
bFound=True
Next
If bFound=False Then
CallSubDelayed2(Main,"SetPrinterLabel","No Printer Devices Found")
End If
End If
CallSubDelayed2(Main,"InvokeProgressDialogHide","")
Catch
CallSubDelayed2(Main,"DisplayServiceErrorMessage","printservice Error: ConnectToBlueTooth: " & LastException.Message)
End Try
End Sub
Sub PrinterAdmin_DeviceFound (Name As String, MacAddress As String)
'Log(Name & ":" & MacAddress)
Dim nm As NameAndMac
If nm.IsInitialized=False Then
nm.Initialize
End If
If Name.Contains("GD200P") Then
nm.Name = Name
nm.Mac = MacAddress
printer_foundDevices.Add(nm)
End If
CallSubDelayed2(Main,"DisplayProgressDialogShow","Searching For Printer devices (~ device found)...".Replace("~", printer_foundDevices.Size))
End Sub
Sub PrinterAdmin_DiscoveryFinished
Try
CallSubDelayed2(Main,"InvokeProgressDialogHide","")
If Main.bGlobalPrinterSerialConnected=False Then
If printer_foundDevices.Size = 0 Then
CallSubDelayed2(Main,"SetPrinterLabel","No Printer Device Found")
Else
SelectPrinter
End If
End If
CallSub2(Main,"PrinterAdminSelectionDone","")
Catch
'oError.ShowLastException("Error : PrinterAdmin_DiscoveryFinished")
CallSubDelayed2(Main,"DisplayServiceErrorMessage","printservice Error: PrinterAdmin_DiscoveryFinished: " & LastException.Message)
End Try
End Sub
Public Sub SelectPrinter()
Try
If oPrinterList.IsInitialized=False Then
oPrinterList.Initialize
End If
oPrinterList.Clear
For i = 0 To printer_foundDevices.Size - 1
Dim nm As NameAndMac
If nm.IsInitialized=False Then
nm.Initialize
EndIf
nm = printer_foundDevices.Get(i)
oPrinterList.Add(nm.Name)
Next
If connectedPrinterDevice.IsInitialized=False Then
connectedPrinterDevice.Initialize
End If
CallSub2(Main,"SelectPrinter","")
'Get the Mac address from an input dialog
printer_serial1.Connect(connectedPrinterDevice.Mac)
CallSubDelayed2(Main,"SetPrinterLabel",connectedPrinterDevice.name & " " & connectedPrinterDevice.Mac)
Catch
CallSubDelayed2(Main,"DisplayServiceErrorMessage","printservice Error: selectprinter " & LastException.Message)
End Try
End Sub
'****Main*****
Sub selectprinter(sMsg As String)
Dim res As Int
Dim connectedDevice As NameAndMac
res = InputList(PrintService.oPrinterList, "Choose a printer device to connect", -1)
If res <> DialogResponse.Cancel Then
connectedDevice = PrintService.printer_foundDevices.Get(res)
ProgressDialogShow("Trying to connect to printer : " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")
ProgressDialogHide
PrintService.connectedPrinterDevice.name=connectedDevice.name
PrintService.connectedPrinterDevice.Mac=connectedDevice.Mac
End If
End Sub