Hi to all.
I am trying to connect to a wifi network printer via sockets.
If there is any problem when the connection to the printer is initialized (e.g. wifi is down etc.) then then app must be automatically tries to connect.
I tried with the following this code, but I cannot connect inside the loop of tries, even everything is connected (I manually open and close the tablet's wifi to lose the network in order to check it).
After the repeated tries loop if i press the print button manually the report is printed.
Regards
I am trying to connect to a wifi network printer via sockets.
If there is any problem when the connection to the printer is initialized (e.g. wifi is down etc.) then then app must be automatically tries to connect.
I tried with the following this code, but I cannot connect inside the loop of tries, even everything is connected (I manually open and close the tablet's wifi to lose the network in order to check it).
After the repeated tries loop if i press the print button manually the report is printed.
B4X:
Sub Process_Globals
Dim PrinterSocket As Socket
Dim StreamToPrint As AsyncStreams
Dim tmrOpenSocket As Timer
End Sub
Sub Globals
Dim Tries2OpenSocket As Int
Dim ButtonPrint As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
ButtonPrint.Initialize("OpenPrinter")
Activity.AddView(ButtonPrint, 25%x, 25%y, 25%x, 25%y)
ButtonPrint.Text = "Print"
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Activity_KeyPress(KeyCode As Int) As Boolean 'return true if you want to consume the event
End Sub
Sub OpenPrinter_Click
If (PrinterSocket.IsInitialized = False) Then
PrinterSocket.Initialize("PrinterSocket")
End If
PrinterSocket.Connect("192.168.1.3", 9100, 0)
End Sub
Sub PrinterSocket_Connected (Connected As Boolean) As Boolean
Log(Connected)
If Connected = True Then
ToastMessageShow("Connected...", False)
If (StreamToPrint.IsInitialized = False) Then
StreamToPrint.Initialize(PrinterSocket.InputStream, PrinterSocket.OutputStream, "AStreams")
End If
tmrOpenSocket.Initialize("tmrOpenSocket", 100)
tmrOpenSocket.Enabled = True
'~ Print
CallSub(Me, PrintText)
Else
ToastMessageShow("Service not available... " & Tries2OpenSocket & " attempt" , False)
Tries2OpenSocket = Tries2OpenSocket + 1
If (Tries2OpenSocket < 50) Then
'~ Close the stream and connection
StreamToPrint.Close
PrinterSocket.Close
'~ Try to open it again
OpenPrinter_Click
End If
End If
End Sub
Sub tmrOpenSocket_Tick
If StreamToPrint.OutputQueueSize <> 0 Then
Return
Else
tmrOpenSocket.Enabled = False
StreamToPrint.Close
PrinterSocket.Close
Tries2OpenSocket = 1
ToastMessageShow("Closing socket...", False)
End If
End Sub
Sub PrintText
Dim sPrint As String
'~ International character set for Epson
sPrint = Chr(27) & Chr(61) & Chr(1)
StreamToPrint.Write(sPrint.GetBytes("UTF8"))
sPrint = "Something to print"
StreamToPrint.Write(sPrint.GetBytes("UTF8"))
'~ Line feed
sPrint = Chr(27) & Chr(100) & Chr(2)
StreamToPrint.Write(sPrint.GetBytes("UTF8"))
End Sub
Last edited: