Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"/>
like image 823
Zim Avatar asked Apr 23 '18 09:04

Zim


People also ask

Why does my WIFI say unknown SSID?

If your network's name is not on the list, the AP or router may be hiding its SSID. Click Add Network to configure your network name manually. If your network is on the list but Connected does not appear beneath its name, tap your network to try to connect.

What does unknown SSID mean in Android?

This lets the client devices know that there is an available Wi-Fi network, but its SSID is hidden, and consequently the client device is not able to connect to it, unless the network name is previously known, since this works as a first filter or first security step through obscurity.

What is an unknown SSID?

What is hiding an SSID? Hiding an SSID is simply disabling a wireless router's SSID broadcast feature. Disabling the SSID broadcast stops the router from sending out the wireless network's name, making it invisible to users. However, this only hides the name from showing up on device lists of nearby networks.


2 Answers

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);
        }
    }
}
like image 165
Nikita Kurtin Avatar answered Oct 07 '22 00:10

Nikita Kurtin


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

like image 25
behelit Avatar answered Oct 07 '22 00:10

behelit