3

I need to find the MacAddress of the AP I'm connected to. Did some research, finally to do this I used a snippet from the following thread: Wifi getSSID() returns null

The code looks like this:

String mac = "02:00:00:00:00:00";
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
       while(mac.equals("02:00:00:00:00:00")) {
             WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
             mac = wifiInfo.getBSSID();
             Log.d("Wifi", "WifiInfo : " + wifiInfo.toString());
          }
Log.d("Wifi", "YAY!" + mac);

(I wrote it like this so that if for some reason it returns null, it will redo it until it returns something else, because according to some answers it's not always consistent)

unfortunately, thie output is always: D/Wifi: WifiInfo: SSID: , BSSID: 02:00:00:00:00:00, MAC: 02:00:00:00:00:00, Supplicant state: COMPLETED, RSSI: -74, Link speed: 58Mbps, Tx Link speed: 58Mbps, Rx Link speed: -1Mbps, Frequency: 2412MHz, Net ID: -1, Metered hint: false, GigaAp: false, VenueName: null, WifiMode: 4, HotspotLiveAp: false, score: 55

The manifest has the necessary permissions (according to a couple of threads, may be missing something):

<uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" android:required="true" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" android:required="true" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:required="true" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:required="true" />
<uses-permission android:name="android.permission.INTERNET" android:required="true" />

To see if maybe the problem is external, I downloaded a WifiAnalyzer to my phone, but it correctly shows the mac of the AP's near me as well as the one I'm connected to when running my app, so the problem cant be in the AP settings or anything.

Multiple threads discussed similar if not identical problems: Wifi getSSID() returns null Why WifiConfiguration.BSSID is always null? How to get BSSID of all wifi access points?

But they either aren't answered, focus on a different aspect of the problem, or the answer doesn't work, plus they are all rather old.

TL;DR I want the mac address of the AP my phone is connected to, but getBSSID() always returns null.

Fro-Yo
  • 63
  • 4

1 Answers1

0

Well, turns out the answer isn't in the code. Regardless of your permissions, in android 10 you need to go into settings and allow your app to access location, otherwise getBSSID() will return "02:00:00:00:00:00", so if you want this to be done automatically, through the app and not manually in the sittings, you need to add the permission popup that will change the devices settings. As for how to do that I'm sure there are tutorials, in the meantime my problem was solved.

Fro-Yo
  • 63
  • 4