Hi
I've tried to create a service to print in a bluetooth printer using the bluetooth example with the below results.
First problem is that it prints only the first time the service is called. If i try to run the service again without restarting the application serial fails to connect with error Service discovery failed. If i restart the appl first time works properly. This happens in rapid mode, in other modes legacy or release it doesn't print anything.
Below the code
I've tried to create a service to print in a bluetooth printer using the bluetooth example with the below results.
First problem is that it prints only the first time the service is called. If i try to run the service again without restarting the application serial fails to connect with error Service discovery failed. If i restart the appl first time works properly. This happens in rapid mode, in other modes legacy or release it doesn't print anything.
Below the code
B4X:
#Region Service Attributes
#StartAtBoot: False
#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 admin As BluetoothAdmin
Dim serial1 As Serial
Dim foundDevices As List
Type NameAndMac1 (Name As String, Mac As String)
Dim connectedDevice As NameAndMac1
Dim nm As NameAndMac1
Dim cMac As String = "00:02:5B:B3:22:09"
Dim cStatus As Int = 0
Dim AStream As AsyncStreams
End Sub
Sub Service_Create
' If admin.IsInitialized = False Then admin.Initialize("admin")
' If serial1.IsEnabled = False Then serial1.Initialize("serial1")
admin.Initialize("admin")
serial1.Initialize("serial1")
End Sub
Sub Service_Start (StartingIntent As Intent)
If admin.IsEnabled = False Then
If admin.Enable = False Then
ToastMessageShow("Error enabling Bluetooth adapter.", True)
Else
ToastMessageShow("Enabling Bluetooth adapter...", False)
'the StateChanged event will be soon raised
End If
End If
bConnect
End Sub
Public Sub bConnect
cStatus = 0
'ToastMessageShow(admin.IsInitialized , True)
If cMac <> "" Then
' ProgressDialogShow("Connecting printer, please wait..")
cStatus = 2
serial1.Disconnect
serial1.Connect(cMac)
' serial1.Listen
Else
SearchDevice
End If
End Sub
Sub Serial1_Connected (Success As Boolean)
'ProgressDialogHide
' Log("connected: " & Success)
ToastMessageShow(Success , True)
If Success = False Then
If cStatus = 1 Then
SearchDevice
Else
serial1.Disconnect
Log(LastException.Message)
ToastMessageShow("Error connecting: " & LastException.Message, True)
CallSub(Main, "StopSrv")
End If
Else
cMac = serial1.Address
cStatus = 9
PrintReceipt("werwer")
'StartActivity(ChatActivity)
End If
End Sub
Sub SearchDevice
cStatus = 2
foundDevices.Initialize
nm.Initialize
If admin.StartDiscovery = False Then
ToastMessageShow("Error starting discovery process.", True)
Else
'ProgressDialogShow("Searching for devices...")
End If
End Sub
Sub Admin_DeviceFound (Name As String, MacAddress As String)
nm.Name = ""
nm.Mac = ""
If Name = "MPT-II" Then
nm.Name = Name
nm.Mac = MacAddress
admin.CancelDiscovery
End If
'ProgressDialogShow("Searching for devices (~ device found)...".Replace("~", foundDevices.Size))
End Sub
Sub Admin_DiscoveryFinished
ProgressDialogHide
If nm.Name = "MPT-II" Then
'ProgressDialogShow("Connecting " & nm.Name )
serial1.Connect(connectedDevice.Mac)
End If
End Sub
Public Sub Disconnect
serial1.Disconnect
End Sub
Public Sub PrintReceipt(instr As String)
Try
If AStream.IsInitialized = False Then
'AStream.InitializePrefix(serial1.InputStream, True, serial1.OutputStream, "AStream")
AStream.Initialize(serial1.InputStream, serial1.OutputStream, "AStream")
End If
Dim buf(2) As Byte
buf(0) = 0x1B
buf(1) = 0x40
Log(AStream.Write(buf))
Log(AStream.Write(instr.GetBytes("UTF8")))
'AStream.Write(Chr(27)&Chr(64).GET )
Dim buf(1) As Byte
buf(0) = 0x0A
AStream.Write(buf)
'writeLine(Chr(27) & Chr(116) & Chr(33))
Catch
ToastMessageShow("Error.." , False)
End Try
serial1.Disconnect
CallSub(Main, "StopSrv")
End Sub
Sub Service_Destroy
serial1.Disconnect
End Sub