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.