Android Example Check the Network State and Wifi SSID Name with reflection

============= MANIFEST =====================

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
AddReceiverText(CheckWifi,
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>)
AddPermission (android.permission.ACCESS_NETWORK_STATE)
AddPermission (android.permission.ACCESS_WIFI_STATE)




============add CheckWifi service ==================

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim R As Reflector

End Sub
Sub GetWIFIType As String
 
    'Requires Permission to be added with the Manifest Editor:
    '                         AddPermission (android.permission.ACCESS_NETWORK_STATE)
 
    R.Target=R.GetContext
    Try
        R.Target=r.RunMethod2("getSystemService","connectivity","java.lang.String")
        R.Target=R.RunMethod("getActiveNetworkInfo")
        R.Target=R.RunMethod("getTypeName")
        Return R.Target
    Catch
        Return "none"
    End Try
End Sub

 Sub GetWIFIName As String
 
    'Requires Permission to be added with the Manifest Editor:
    '                         AddPermission (android.permission.ACCESS_NETWORK_STATE)
 
    R.Target=R.GetContext
    Try
        R.Target=r.RunMethod2("getSystemService","wifi","java.lang.String")
        R.Target=R.RunMethod("getConnectionInfo")
        R.Target=R.RunMethod("getSSID")
        Return R.Target
    Catch
        Return "none"
    End Try
End Sub
Sub Service_Create
End Sub

Sub Service_Start(startingIntent As Intent)
    Dim ConnectionType As String =GetWIFIType
 
   If startingIntent.Action = "android.net.conn.CONNECTIVITY_CHANGE" Then
        ToastMessageShow("xxxxxxxxx wifichanged-Connection Type:" & ConnectionType & _       
        " WifiName:" & GetWIFIName, True)
   End If
End Sub
 

junglejet

Active Member
Licensed User
Longtime User
This is a very nice and quick method to retrieve Wifi SSID.
It worked well all the time on Android <=6.
Unfortunately on Android 9 the SSID retrieval shows "<unknown ssid>".
Is there a workaround?
 

Andrea Falcucci

Member
Licensed User
Longtime User
Hi Erel. we are using MLWifi and even with latest version, we are unable to get Wifi SSID. I've added
AddPermission(android.permission.ACCESS_COARSE_LOCATION) in manifest but without success.

Thank you for your help
 
Top