WifiManager.WifiInfo getSSID & getBSSID not working if targetSdkVersion >= 26
Asked Answered
E

5

6

I'm getting correctly those values if I compile with API 27 but target API 25. If I set targetSdkVersion to 27, then, those values are not correctly retrieved.

Targeting SDK 25 or less, values are correct, but targeting 26 or more, I get those values:

SSID gives <unknown ssid>

BBSSID gives 02:00:00:00:00:00

These are my manifest permissions, all are normal permissions and don't require user grant:

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

And this is the sample code:

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
connectionInfo.getSSID();
connectionInfo.getBSSID();

What has changed when targeting SDK 26 or more? what more should we do to get those values?

Erbium answered 11/3, 2018 at 20:31 Comment(2)
#21391895Ser
Location should be turned on to get SSID see: #21391895Ser
D
5

You need location permission starting with API 27

Deakin answered 11/3, 2018 at 20:38 Comment(2)
which location permission? there are two or threeErbium
ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION no matter which one or both if you wantDeakin
V
2

Google has changed their policy with WifiManager since you can track user's location tracking wifi location from SDK>=27. Therefore you need to access location permission in order to use getSSID() for your appliaction. In order to use this code follow below.

As @Samuel Eminet answer above, you need either ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION. I used ACCESS_FINE_LOCATION because ACCESS_COARSE_LOCATION request permission for only NETWORK_PROVIDE whereas ACCESS_FINE_LOCATION request permissions for NETWORK_PROVIDE AND GPS_PROVIDER.

Refer here for more information

In your Manifest file

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

and where your wifi manager is, add this code to request permissions.

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

        //Permission Not Granted
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_FINE_LOCATION);
    }



    @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {                                   
        switch (requestCode) {

        case PERMISSION_FINE_LOCATION:
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //permission granted keep going status
                Log.d(TAG, "PERMISSION GRANTED");
            }
            else {
                Log.d(TAG, "PERMISSION DENIED");
            }
    }
}
Valona answered 14/6, 2018 at 6:53 Comment(0)
S
0

One more thing to remember (I learned the hard way). If you are side-loading your app onto a device, don't forget to "allow" location permission in the app permissions (long press app, app info). It is off by default, thus you will get

Singh answered 1/5, 2018 at 1:17 Comment(0)
V
0

I met with same problem. This problem happens higher android versions than API 23. Solution: You need to add below to build.gradle file.

implementation 'com.google.android.gms:play-services-location:15.0.0'

And you permission below rules.

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

Below code provides automatically permission for location in application.

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        try {
            LocationRequest request = new LocationRequest()
                    .setFastestInterval(1500)
                    .setInterval(3000)
                    .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

            builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(request);

            try {
                Task<LocationSettingsResponse> result =
                        LocationServices.getSettingsClient(this.getApplicationContext()).checkLocationSettings(builder.build());
                result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
                    @Override
                    public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
                        try {
                            task.getResult(ApiException.class);
                        } catch (ApiException e) {
                            switch (e.getStatusCode()) {
                                case LocationSettingsStatusCodes
                                        .RESOLUTION_REQUIRED:
                                    ResolvableApiException resolveApiException = (ResolvableApiException) e;
                                    try {
                                        resolveApiException.startResolutionForResult(WifiConnectionActivity.this, REQUEST_CHECK_CODE);
                                    } catch (IntentSender.SendIntentException ex) {
                                        ex.printStackTrace();
                                    } catch (ClassCastException ex) {

                                    }
                                    break;

                                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                                    break;
                            }
                        }
                    }
                });
            } catch (Exception x) {
            }
            checkPermissions();
        } catch (Exception a) {
        }
    }


void checkPermissions() {
    if(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(WifiConnectionActivity.this, new String[]
                {
                        Manifest.permission.ACCESS_COARSE_LOCATION
                }, REQUEST_LOCATION);
    }
    else{
        //Toast.makeText(getActivity(), "GPS is already permissioned", Toast.LENGTH_SHORT).show();
    }
}
Venation answered 29/5, 2020 at 12:7 Comment(0)
K
-2

The below is the code .

public String getMac() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        //Permission Not Granted
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    }else{
        WifiManager wifimanage = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        WifiInfo wifiinfo = wifimanage.getConnectionInfo();
        macAddress = wifiinfo.getBSSID();//Get the mac address of the currently connected network;
    }
    return macAddress;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 1:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //permission granted keep going status
                Log.d("mac", "PERMISSION GRANTED");
                WifiManager wifimanage = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
                WifiInfo wifiinfo = wifimanage.getConnectionInfo();
                macAddress = wifiinfo.getBSSID();//Get the mac address of the currently connected network;
            } else {
                Log.d("mac", "PERMISSION DENIED");
            }
    }
}

Then call getMac() to solve the problem.

Kistler answered 17/9, 2018 at 7:7 Comment(1)
Welcome to SO. Could you improve the formatting of your code sample, please? It's also good to give some information in English. Thanks.Emboss

© 2022 - 2024 — McMap. All rights reserved.