Skip to content

Commit ecfaee2

Browse files
Links2004igrr
authored andcommitted
add function to get the MAC / BSSID as String
1 parent 448f09b commit ecfaee2

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,32 @@ uint8_t* ESP8266WiFiClass::macAddress(uint8_t* mac)
203203
return mac;
204204
}
205205

206+
String ESP8266WiFiClass::macAddress(void)
207+
{
208+
uint8_t mac[6];
209+
char macStr[18] = {0};
210+
wifi_get_macaddr(STATION_IF, mac);
211+
212+
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
213+
return String(macStr);
214+
}
215+
206216
uint8_t* ESP8266WiFiClass::softAPmacAddress(uint8_t* mac)
207217
{
208218
wifi_get_macaddr(SOFTAP_IF, mac);
209219
return mac;
210220
}
211221

222+
String ESP8266WiFiClass::softAPmacAddress(void)
223+
{
224+
uint8_t mac[6];
225+
char macStr[18] = {0};
226+
wifi_get_macaddr(SOFTAP_IF, mac);
227+
228+
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
229+
return String(macStr);
230+
}
231+
212232
IPAddress ESP8266WiFiClass::localIP()
213233
{
214234
struct ip_info ip;
@@ -251,6 +271,16 @@ uint8_t* ESP8266WiFiClass::BSSID(void)
251271
return reinterpret_cast<uint8_t*>(conf.bssid);
252272
}
253273

274+
String ESP8266WiFiClass::BSSIDstr(void)
275+
{
276+
static struct station_config conf;
277+
char mac[18] = {0};
278+
wifi_station_get_config(&conf);
279+
sprintf(mac,"%02X:%02X:%02X:%02X:%02X:%02X", conf.bssid[0], conf.bssid[1], conf.bssid[2], conf.bssid[3], conf.bssid[4], conf.bssid[5]);
280+
return String(mac);
281+
}
282+
283+
254284
int32_t ESP8266WiFiClass::channel(void) {
255285
return wifi_get_channel();
256286
}
@@ -353,6 +383,17 @@ uint8_t * ESP8266WiFiClass::BSSID(uint8_t i)
353383
return it->bssid;
354384
}
355385

386+
String ESP8266WiFiClass::BSSIDstr(uint8_t i)
387+
{
388+
char mac[18] = {0};
389+
struct bss_info* it = reinterpret_cast<struct bss_info*>(_getScanInfoByIndex(i));
390+
if (!it)
391+
return String("");
392+
393+
sprintf(mac,"%02X:%02X:%02X:%02X:%02X:%02X", it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]);
394+
return String(mac);
395+
}
396+
356397
int32_t ESP8266WiFiClass::channel(uint8_t i)
357398
{
358399
struct bss_info* it = reinterpret_cast<struct bss_info*>(_getScanInfoByIndex(i));

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,19 @@ class ESP8266WiFiClass
103103
* Get the station interface MAC address.
104104
*
105105
* return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
106+
* return: String
106107
*/
107108
uint8_t* macAddress(uint8_t* mac);
109+
String macAddress(void);
108110

109111
/*
110112
* Get the softAP interface MAC address.
111113
*
112114
* return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
115+
* return: String
113116
*/
114117
uint8_t* softAPmacAddress(uint8_t* mac);
115-
118+
String softAPmacAddress(void);
116119
/*
117120
* Get the station interface IP address.
118121
*
@@ -151,10 +154,17 @@ class ESP8266WiFiClass
151154
/*
152155
* Return the current bssid / mac associated with the network if configured
153156
*
154-
* return: bssid string
157+
* return: bssid uint8_t *
155158
*/
156159
uint8_t * BSSID(void);
157160

161+
/*
162+
* Return the current bssid / mac associated with the network if configured
163+
*
164+
* return: bssid string
165+
*/
166+
String BSSIDstr(void);
167+
158168
/*
159169
* Return the current channel associated with the network
160170
*
@@ -209,10 +219,17 @@ class ESP8266WiFiClass
209219
/**
210220
* return MAC / BSSID of scanned wifi
211221
* @param networkItem specify from which network item want to get the information
212-
* @return uint8_t * to MAC / BSSID of scanned wifi
222+
* @return uint8_t * MAC / BSSID of scanned wifi
213223
*/
214224
uint8_t * BSSID(uint8_t networkItem);
215225

226+
/**
227+
* return MAC / BSSID of scanned wifi
228+
* @param networkItem specify from which network item want to get the information
229+
* @return String MAC / BSSID of scanned wifi
230+
*/
231+
String BSSIDstr(uint8_t networkItem);
232+
216233
/**
217234
* return channel of scanned wifi
218235
* @param networkItem specify from which network item want to get the information

0 commit comments

Comments
 (0)