Skip to content

Commit 6450ecd

Browse files
committed
Use m2m_wifi_get_connection_info to get BSSID/remote MAC
1 parent 410583e commit 6450ecd

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

examples/AP_SimpleWebServer/AP_SimpleWebServer.ino

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,26 @@ void loop() {
7676
status = WiFi.status();
7777

7878
if (status == WL_AP_CONNECTED) {
79+
byte remoteMac[6];
80+
7981
// a device has connected to the AP
80-
Serial.println("Device connected to AP");
82+
Serial.print("Device connected to AP, MAC address: ");
83+
WiFi.BSSID(remoteMac);
84+
Serial.print(remoteMac[5], HEX);
85+
Serial.print(":");
86+
Serial.print(remoteMac[4], HEX);
87+
Serial.print(":");
88+
Serial.print(remoteMac[3], HEX);
89+
Serial.print(":");
90+
Serial.print(remoteMac[2], HEX);
91+
Serial.print(":");
92+
Serial.print(remoteMac[1], HEX);
93+
Serial.print(":");
94+
Serial.println(remoteMac[0], HEX);
8195
} else {
8296
// a device has disconnected from the AP, and we are back in listening mode
8397
Serial.println("Device disconnected from AP");
84-
}
98+
}
8599
}
86100

87101
WiFiClient client = server.available(); // listen for incoming clients
@@ -154,4 +168,5 @@ void printWifiStatus() {
154168
// print where to go in a browser:
155169
Serial.print("To see this page in action, open a browser to http://");
156170
Serial.println(ip);
171+
157172
}

src/WiFi.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,26 @@ static void wifi_cb(uint8_t u8MsgType, void *pvMsg)
140140
if (scan_ssid_len) {
141141
memcpy(WiFi._scan_ssid, (const char *)pstrScanResult->au8SSID, scan_ssid_len);
142142
}
143-
if (WiFi._bssid) {
144-
memcpy(WiFi._bssid, (const char *)pstrScanResult->au8BSSID, 6);
145-
}
146143
WiFi._resolve = pstrScanResult->s8rssi;
147144
WiFi._scan_auth = pstrScanResult->u8AuthType;
148145
WiFi._status = WL_SCAN_COMPLETED;
149146
}
150147
break;
151148

149+
case M2M_WIFI_RESP_CONN_INFO:
150+
{
151+
tstrM2MConnInfo *pstrConnInfo = (tstrM2MConnInfo*)pvMsg;
152+
153+
if (WiFi._bssid) {
154+
// reverse copy the remote MAC
155+
for(int i = 0; i < 6; i++) {
156+
WiFi._bssid[i] = pstrConnInfo->au8MACAddress[5-i];
157+
}
158+
WiFi._bssid = 0;
159+
}
160+
}
161+
break;
162+
152163
default:
153164
break;
154165
}
@@ -591,17 +602,17 @@ char* WiFiClass::SSID()
591602

592603
uint8_t* WiFiClass::BSSID(uint8_t* bssid)
593604
{
594-
int8_t net = scanNetworks();
595-
596605
_bssid = bssid;
597606
memset(bssid, 0, 6);
598-
for (uint8_t i = 0; i < net; ++i) {
599-
SSID(i);
600-
if (strcmp(_scan_ssid, _ssid) == 0) {
601-
break;
602-
}
607+
608+
m2m_wifi_get_connection_info();
609+
610+
// Wait for connection or timeout:
611+
unsigned long start = millis();
612+
while (_bssid != 0 && millis() - start < 1000) {
613+
m2m_wifi_handle_events(NULL);
603614
}
604-
615+
605616
_bssid = 0;
606617
return bssid;
607618
}

0 commit comments

Comments
 (0)