On Oreo (8.1.0) not getting the correct Wifi SSID. It's showing <unknown ssid> though it is connected to a wifi with SSID
Asked Answered
T

8

14

I need to check the current connected wifi SSID on android. I checked with Nokia 6 and OnePlus 5 with respectively Oreo 8.1.0 and Oreo 8.0. Other phones with different OS version is working fine with this code. Is there anything wrong with my code?

private WifiInfo wifiInfo;
private String ssid = "";
private WifiManager wifiManager;

private boolean getWifiStatus() {
    wifiManager= (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    wifiInfo = wifiManager.getConnectionInfo();
    ssid = "";
    ssid = wifiInfo.getSSID();
    ConnectivityManager cm =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isWiFi = false;
    if(activeNetwork != null){
        isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
    }

    Log.d(TAG, "getWifiStatus: " + ssid);
    if(ssid.contains("TripleMZim") && wifiManager.isWifiEnabled() && isWiFi ){
        return true;
    }
    else{
        return false;
    }
}

permission in Manifest file:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
Tris answered 23/4, 2018 at 9:17 Comment(0)
D
12

Oreo 8.1+ devices require the coarse location runtime permission as well as location services to be turned on before being able to retrieve the connected SSID as it can infer the users location.

Further information is available here

Which relates to this

Disembowel answered 18/5, 2018 at 4:15 Comment(0)
W
20

There are different approaches how you can get the WIFI information (such as SSID) in android.

Some of them already listed here, for example WifiManager service.

But for OP question:

Is there anything wrong with my code?

No, the problem is not in your code. The main reason for the unexpected behavior, is because of the security patch of the last android releases.

Before the patch (or any device that didn't get an update), the hackers could steal sensitive information that was leaked through the wifi info.

For example the hackers could obtain the device geolocation, that should be accessible only with the Dangerous Permission LOCATION, that should be requested at runtime and can be revoked at any moment. But in the other hand - the WifiInfo is accessible only with the Normal Permission android.permission.ACCESS_WIFI_STATE which is granted at install time and cannot be revoked. That way, hackers could bypass the permission mechanism and access the data from dangerous permission using only normal permission.

The issue was reported and tracked by CVE-2018-9489, you can read more about it here: Sensitive information expose via WIFI

So the reason why you having those problems is because now android blocks the sensitive wifi info, unless the app holds the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.

So, if you request those permissions explicitly (and allowed by the user), your code should work fine.

For example:

Manifest

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Activity

private static final int LOCATION = 1;
protected void onStart() {
   super.onStart();
   //Assume you want to read the SSID when the activity is started
   tryToReadSSID();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if(grantResults[0] == PackageManager.PERMISSION_GRANTED && requestCode == LOCATION){
        //User allowed the location and you can read it now
        tryToReadSSID();
    }
}

private void tryToReadSSID() {
    //If requested permission isn't Granted yet
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        //Request permission from user
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION);
    }else{//Permission already granted 
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if(wifiInfo.getSupplicantState() == SupplicantState.COMPLETED){
            String ssid = wifiInfo.getSSID();//Here you can access your SSID
            System.out.println(ssid);
        }
    }
}
Windywindzer answered 7/1, 2019 at 18:14 Comment(2)
The problem has been solved. Thanks for your explanation.Tris
@bh_earth0 Checked with android 8.1 and also the newest android 10 (API 29) -> it works perfectly, so I'm guessing you have some other issue. If you still can't resolve it, please open a new dedicated question for it with all the details (source code, errors, what have you tried, etc...)Windywindzer
D
12

Oreo 8.1+ devices require the coarse location runtime permission as well as location services to be turned on before being able to retrieve the connected SSID as it can infer the users location.

Further information is available here

Which relates to this

Disembowel answered 18/5, 2018 at 4:15 Comment(0)
B
11

In Android Oreo you could use ConnectivityManager to know your wifi SSID.

Permission

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Code to get SSID

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
    String ssid = info.getExtraInfo();
    Log.d(TAG, "WiFi SSID: " + ssid);
}

This is tested in Android Oreo 8.1.0. You will get SSID enclosed with double quotes.

Balmoral answered 25/7, 2018 at 12:6 Comment(4)
@AmitGarg It didn't work because with Pie, you need to have enabled GPS and app has permission to "Location" and only then you will see the SSID. Otherwise you will see "<unknown ssid>" in the logs.Violist
@WahibUlHaq It doesn't work on Android Pie 9.0 event when GPS is enabled and location permission is set.. Tested on Pixel XL (first gen)Rintoul
I have tested on Pixel (9.0)as well and it works fine. You can reconfirm your wifi state using adb commands and also depends when are you calling this code. For example, when disconnected state I also don't see the wifi ssid even when GPS is enabled.Violist
Here works on Android 9 (Pixel XL) but not on OnePlus Android 8. (Location access provided)Maren
A
3

You need to request permission in manifest :

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

and request it in your activity :

    private void grantPermm()
{

    try
    {
        if (ContextCompat.checkSelfPermission(MainScreenActivity.this,
                  Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)

        {

        }
        else
        {

            ActivityCompat.requestPermissions(MainScreenActivity.this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    101);
        }

    }
    catch (Exception xx){}

}

then use this function :

    public  static String  getWifiName(Context context)
{
    String result="";
    try {
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (manager.isWifiEnabled())
        {
            WifiInfo wifiInfo = manager.getConnectionInfo();
            if (wifiInfo != null)
            {
                NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
                if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR)
                {
                    result = wifiInfo.getSSID();
                    if(result.equalsIgnoreCase("<unknown ssid>"))
                    {
                        Toast.makeText(context,"You are connected to mobile data", Toast.LENGTH_SHORT).show();
                        result="";
                    }

                    return result;
                }
                else
                {
                    Toast.makeText(context,"WIFI not connected", Toast.LENGTH_SHORT).show();
                    return "";
                }
            }
            else
            {
                Toast.makeText(context,"No WIFI Information", Toast.LENGTH_SHORT).show();
                return "";
            }
        }
        else
        {
            Toast.makeText(context,"WIFI not enabled", Toast.LENGTH_SHORT).show();
            return "";
        }
    }
    catch (Exception xx)
    {
        Toast.makeText(context, xx.getMessage().toString(), Toast.LENGTH_SHORT).show();
        return "";
    }
}
Ate answered 30/1, 2019 at 10:56 Comment(0)
P
1

If you're targeting API 29 (Android 10) then the location permission requested must now be for ACCESS_FINE_LOCATION, not just ACCESS_COARSE_LOCATION. Most of the above sample code is fine, however some answers could be misleading in the text.

https://developer.android.com/about/versions/10/privacy/changes

Pejsach answered 25/9, 2019 at 7:18 Comment(0)
P
0

If you face issues even after trying the Yaswant Narayan and behelit answers, make sure you have enabled the Location/GPS in the device while trying to get the SSID. Even though, you have acquired the relevant permissions in Android Oreo and above devices, if the Location/GPS is turned off by the user, then you will get the from those devices.

Plate answered 10/11, 2018 at 8:29 Comment(0)
C
0

You need to ask for runtime permission of read phone state. This is included for better security.

code is as follows:-

    TelephonyManager telephonyManager = (TelephonyManager) getContext()
            .getSystemService(Context.TELEPHONY_SERVICE);

    String IMEI ="";

    if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
       ActivityCompat.requestPermissions(getActivity(),
               new String[]{Manifest.permission.READ_PHONE_STATE},
               123);

    }
    int permission = ContextCompat.checkSelfPermission(getContext(),
            Manifest.permission.READ_PHONE_STATE);
    if(permission == PackageManager.PERMISSION_GRANTED){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            IMEI = telephonyManager.getImei();
        }
        else{
            IMEI = telephonyManager.getDeviceId();
        }
    }
Cockchafer answered 13/5, 2019 at 10:13 Comment(0)
A
0

if you still need to solve this try to allow location permission from the settings of the device. You need to allow your app to use location

Amyotonia answered 5/8, 2023 at 17:27 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Hagbut

© 2022 - 2024 — McMap. All rights reserved.