Android Question Android WIFI conversion not working

DickD

Active Member
Licensed User
I'm trying to convert my Android app from using Bluetooth to WIFI. I've been following the Asynch Streams sample and came up with the following two modules. The first module uses sockets to try to connect to a service on another phone. It times out after 10 seconds with Success = False. The second code section is the service. It never moves beyond Server.Listen. The Sub Server_NewConnection is never triggered. What am I doing wrong?

B4X:
Sub Process_Globals
 Dim Socket1 As Socket
End Sub
Sub Activity_Create(FirstTime As Boolean)
 Socket1.Initialize("Socket1")
End Sub
Sub Doit
 MacAddress = "192.168.2.8"
 Try
 ProgressDialogShow("Loading Profile ")
 Socket1.Connect(MacAddress,5500,10000)
 Catch
 Log("WIFI Loader failed: "  & LastException.Message)
 End Try
End Sub
Sub Socket1_Connected (Success As Boolean)
 Log("WIProfileLoader connected: " & Success)
 If Success = False Then
 Msgbox("No Connection","")
 Socket1.close
 AStream.Close
 Activity.Finish
 StartActivity("DisplayMatches")
 End If
.....

B4X:
'this is the server service

Sub Process_Globals
 Dim Server As ServerSocket
 Dim Socket1 As Socket
End Sub
Sub Service_Create
 Server.Initialize(5500, "Server")
End Sub
Sub Service_Start(StartingIntent As Intent)
 Log("WIFI Listener Service is starting")
 serial1.Initialize("serial1")
 Try
 ServicePass = ServicePass + 1
 Server.Listen
 Catch
 Log(LastException.message)
 End Try
End Sub
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
 If Successful  = True Then
 Log("WIFI connection successful")
 Socket1 = NewSocket
 Try
 If Astream.IsInitialized = False Then
 Astream.InitializePrefix(Socket1.InputStream, False, Socket1.OutputStream, "AStream")
 End If
 
 Catch
 Log(LastException.message)
 End Try
 Else
 Log("WIFI Listener failed")
 End If
...
 

DickD

Active Member
Licensed User
There are several mistakes in this code.

See this example: https://www.b4x.com/android/forum/threads/network-asyncstreams-b4xserializator.72149/#content (download NetworkExample7Plus if you are using B4A v7+)
I can find no functional difference between your code in NetWorkExample.B4a and mine which I am repeating below. If there are any obvious mistakes in my code could you point them out? Bottom line: Socket1.Connect fails with Success = False. It happens almost immediately and doesn't wait the 20 seconds indicated.

B4X:
Sub Process_globals
 Dim socket1 as socket
 Dim AStream As AsyncStreams
 Dim IPAddress as string
End sub

Sub Activity_Create(FirstTime As Boolean)
 Socket1.Initialize("Socket1")
End Sub

Sub Doit
 IPAddress = "192.168.2.4"
 Try
 ProgressDialogShow("Loading Profile ")
 Socket1.Connect(IPAddress,5500,20000)
 Catch
 Log("WIProfileLoader failed "  & LastException.Message)
 End Try
End Sub

Sub Socket1_Connected (Success As Boolean)
 Log("WIProfileLoader connected: " & Success)
 If Success = False Then
 Msgbox("Server is busy, off or out of range. Try again later.","")
 Socket1.close
 AStream.Close
 Activity.Finish
 StartActivity("Display")
 Else
 If AStream.IsInitialized = False Then
 AStream.InitializePrefix(Socket1.InputStream, True, Socket1.OutputStream, "AStream")
 End If
 End If
End Sub

B4X:
'this is the server running on a different phone
Sub Process_Globals
 Dim Server As ServerSocket
 Dim Socket1 As Socket
End Sub

Sub Service_Create
 Server.Initialize(5500, "Server")
 Server.Listen
End Sub

Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
 If Successful  = True Then
 Log("WIFI connection successful")
 If Astream.IsInitialized Then Astream.Close
 If Socket1.IsInitialized Then Socket1.Close
 Socket1 = NewSocket
 Try
 If Astream.IsInitialized = False Then
 Astream.InitializePrefix(Socket1.InputStream, False, Socket1.OutputStream, "AStream")
 End If
 
 Catch
 Log(LastException.message)
 End Try
 Else
 Log("WIFI Listener failed")
 End If
 Server.Listen
End Sub
 
Upvote 0

DickD

Active Member
Licensed User
It is worth starting with the example as-is. Does it work for you? Does it connect?
Hmmmmm...... No it doesn't work or even connect. This is strange since it did work when I tested this same example program several months ago. I' m not aware of any change in the configuration on my phones or router. Not sure where to look next. I may try with different phones.
 
Upvote 0

DickD

Active Member
Licensed User
It is worth starting with the example as-is. Does it work for you? Does it connect?
I have tried this on several phones running Android 4.4 to 6.0 to 7.0 and can not connect on any of them using the NetworkExample app.
 
Upvote 0

DickD

Active Member
Licensed User
It is possible that your router blocks incoming connections inside the local network.
Sigh..... yes, it was a router problem. The phones are connecting but I'm now having a problem with asynch streams. I'll post separately after further testing.
 
Upvote 0
Top