Android Question Socket connection problem

thanos

Member
Licensed User
Longtime User
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.
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
Regards
 
Last edited:

thanos

Member
Licensed User
Longtime User
@Erel
Thank you for your response.
I will follow your suggestions, but i have to understand something.
The small part of code I post was not clear so I edit the first post with the full code.
I am using the timer in order to check the OutputQueueSize.
I want to use this code in another application, which allows several users to print at the same time in the newtork printer.
If two users prints exactly at the same time the first one prints correctly and the second one took a connection error.
This is the purpose that I want to close and open the socket automatically until the second or the third device will be able to print.
Regards.
 
Last edited:
Upvote 0

thanos

Member
Licensed User
Longtime User
I'm not sure that I understand the question...
Thanks for the response.
I'm sorry. I will try to be more specific.
The scenario is:
There are more than one users who are using the app and they will print to a wifi network printer at the same time.
With the code I post if two users send something to printer at exactly the same time, or the second one send its printing before the printer finish the first job.
In both cases the second user can not connect and took the message:
ToastMessageShow("Service not available... " & Tries2OpenSocket & " attempt" , False)
The point is that the app must trying to connect to the printer and print the report after lets say 10 tries.
The loop is executed for 10 times but I always took the above error message.
I suppose that I had close the socket correctly.
After the 10 tries if the second user push the print button again the job will done correctly.
Regards.
 
Upvote 0

thanos

Member
Licensed User
Longtime User
You need to add a short duration between the attempts. Use CallSubPlus for this.
Thanks a lot!
I tried a similar workaround yesterday but your code is more elegant ;)
If the printer supports response for the the job (e.g. printed ok, out of paper etc.) how can I catch it?
Regards.
 
Upvote 0
Top