Detecting WiFi connection

Kevin

Well-Known Member
Licensed User
Longtime User
I know this has been brought up a couple times on the forum, but I wanted to get an opinion on my code.

I considered using the ABWiFi library but it added a fine location permission, which is undesirable to me.

What I am trying to accomplish is:
1) Detect if WiFi is enabled
2) Detect if WiFi is connected to a network

This works here on my EVO 4G on Sprint, but I want to ensure that it should work "universally".....

B4X:
Sub Globals
  Dim MyLan As ServerSocket  ' Network Library - for detecting IP
End Sub

Sub Activity_Create(FirstTime As Boolean)
  If FirstTime then MyLan.Initialize(0, "")
End Sub

Sub Activity_Resume
  If MyLan.IsInitialized Then CheckWiFi
End Sub

Sub CheckWiFi
  Dim p As Phone  'Phone Library
  If p.GetSettings ("wifi_on") <> 1 Then
    MsgBox ("WiFi is OFF","")
      Else  'WiFi is on
        If p.GetDataState = "DISCONNECTED" Then ' Phone carrier's data network is disconnected
          MsgBox ("WiFi is ON and CONNECTED","")
            Else
      MsgBox ("WiFi is ON but NOT connected to a WiFi network","")
        End If                  
  End If
End Sub

It probably isn't completely foolproof but I think for most people there should be three states:

WiFi on and connected
WiFi on and not connected (and therefore phone is connected to carrier's data connection)
WiFi off and phone is connected to carrier's data connection

Mostly I care if WiFi is on and connected.
 

Inman

Well-Known Member
Licensed User
Longtime User
I considered using the ABWiFi library but it added a fine location permission, which is undesirable to me.

If you don't want location info, you can remove that permission alone and ABWiFi will still work.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
If you don't want location info, you can remove that permission alone and ABWiFi will still work.


Would this require editing the app's manifest file or the library? I'm a bit nervous about editing the manifest file because although you can set it to read-only, I then don't understand how it can be edited by B4A later on should other things change. :confused: Or are you then stuck doing it all manually?


More to the point of my post though: I think I'd be perfectly happy with the code I am using and I think it should at least be more reliable than my current method of detecting a WiFi connection. I just wanted to get an idea from someone who might know Java or Android programming better than me (easy to do) that could tell me if the code itself should work the same on any device, which I believe it should but I wanted to be sure.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Would this require editing the app's manifest file or the library? I'm a bit nervous about editing the manifest file because although you can set it to read-only, I then don't understand how it can be edited by B4A later on should other things change. :confused: Or are you then stuck doing it all manually?.

You will have to edit the manifest file and then in B4A, under Project menu, select Do Not Overwrite Manifest File. That way, B4A will create another Manifest file called AndroidManifest-Example.xml in the same folder and so in case if something doesn't work due to error in your Manifest file, you can open the Manifest-Example file and see if you miss some crucial permissions B4A needs to work correctly and if you do, you can simply copy the permissions from Manifest-Example file and paste it in the Manifest file.
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
that could tell me if the code itself should work the same on any device, which I believe it should but I wanted to be sure.

FWIW:

I tried it on a Xoom and it works ok

Joe
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
FWIW:

I tried it on a Xoom and it works ok

Joe

Thanks for the feedback. One thing that a customer has pointed out (and happens to be on a Xoom) is that if the device is in Airplane mode but has WiFi enabled and connected, the app reports that WiFi is not connected. I don't know if this is a glitch in Android or what. On my EVO 4G, if I put it in Airplane mode and then enable WiFi the app knows that WiFi is connected. In the case of the guy with the Xoom, my app told him is IP address was 192.168.1.x but still failed to detect WiFi being enabled because of Airplane mode.

So for some reason some devices appear to report no WiFi connection if it is in Airplane mode but connected via WiFi. Heck, I didn't even know you could do that until he brought it up. One other customer has complained about it too but didn't mention the device.

I am mentioning this here in case anyone else uses this code to detect an active WiFi connection. As a work-around to the Airplane mode glitch, I plan to also check to see if the IP address assigned to the device begins with "192". If so, I will just assume WiFi is connected. I know that there are other possible beginning numbers for private networks but that one is by far the most common. Besides, I originally checked for what I thought were only private address ranges and it turned out that someone's cellular network assigned similar addresses, which caused my app to think he was on WiFi and proceeded to scan the cellular network rather than his home network (he didn't have WiFi connected when he started my app). He didn't tell me what IP range it was using but I very much doubt it was 192.x.x.x.
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Ok here is what i found

WI-fi on shows it is on and connected ip=192...
Wi-Fi off shows it is off

Wi-fi on and connnected and then airplane mode switched on and it shows it is on and connected. ip address shown as 127.0.0.1
But it also shows in message box that Aiplane mode is on
Msgbox("Airplane Mode On",p.IsAirplaneModeOn)

So it seems to work correctly on this Xoom.

Hope this helps

Joe
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
I wonder if he had data disabled, or maybe if you enable & connect WiFi after enabling Airplane mode it shows the 192.x address?

Regardless, I will check for a 192.x address to get around whatever is causing his problem. If the address is 127.0.0.1 then there cannot be any data connection as far as I know because that indicates that there is no IP address assigned to the device.

Thanks for running some tests!
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
I wonder if he had data disabled, or maybe if you enable & connect WiFi after enabling Airplane mode it shows the 192.x address?

Yes that is exactly what happens

Glad to be of help

Joe
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Airplane mode disables both Bluetooth and Wifi as you can see in the Settings. Unchecking Aiplane mode will return to the previous radio state but you can overrride Airplane mode by individually turning on Bluetooth or WiFi. Confusingly, at least on a Xoom, although Airplane mode appears checked you are no longer actually in Airplane mode, which is when no radio emissions are made from the device, but are back to using whichever radio services(s) you have just manually re-enabled.
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Possible Values

WiFi on and connected
WiFi on and not connected (and therefore phone is connected to carrier's data connection)
WiFi off and phone is connected to carrier's data connection


So there are THREE potential values from iWiFiVal = p.GetSettings ("wifi_on") ?


0, 1, & 2 correct ???

Im turning of and on the WiFi and I consistently get a value of 1
 
Last edited:
Upvote 0
Top