Android Question I need to determine when the Wifi is available

davepamn

Active Member
Licensed User
Longtime User
I tried using

Dim oHttpClient.initialize("Ping")
Dim oRequest as HttpRequest
oRequest.InitializeGet("http://www.abc.com/hello.html")
orequest.timeout=1000
if oHttpClient.execute(oRequest,1)=true then
bRetVal=true
end if

I turned off my wifi but oHttpClient is returning true instead of false.

My problem:
I need to know if the wifi is available.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
It would be helpful if you provided a solution using httputils2
A quick search of the forum yields this... http://www.b4x.com/android/forum/threads/network-library.18993/#post-113971 (courtesy of Kevin)
B4X:
Sub GetIP As String
DeviceIP = ""
InitNetInfo
Try
  DeviceIP = MyIP.GetMyWifiIP ' WiFi IP
  Catch
End Try
If DeviceIP <> "" AND DeviceIP <> "127.0.0.1" Then ' WiFi must be on and connected because we have only checked for the WiFiIP
  OnWiFi = True
  Return DeviceIP
End If
If DeviceIP = "127.0.0.1" Then 'WiFi is apparently off, so next we'll try to get the non-WiFi IP
  OnWiFi = False
End If
Try
  DeviceIP = MyIP.GetMyIP ' Non-WiFi IP
  OnWiFi = False
  Catch
  DeviceIP = ""
End Try
MyIP.Close
Return DeviceIP
End Sub
Sub InitNetInfo
Try
  If MyIP.IsInitialized = False Then
  MyIP.Initialize (1051,"")
  End If
  Catch ' Possibly left other app (free or pro) running
  If MyIP.IsInitialized = False Then
  MyIP.Initialize (1052,"")
  End If
End Try
End Sub
Its using the Network library. an alternative would be to use HttpUtils2 and if the job.success=False assume that WiFi is turned off.

Regards,
RandomCoder
 
Upvote 0
Top