Skip to content

Commit c415ebe

Browse files
committed
add function to get the MAC / BSSID as String
1 parent 883278a commit c415ebe

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFi.cpp

+41
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
}
@@ -358,6 +388,17 @@ uint8_t * ESP8266WiFiClass::BSSID(uint8_t i)
358388
return it->bssid;
359389
}
360390

391+
String ESP8266WiFiClass::BSSIDstr(uint8_t i)
392+
{
393+
char mac[18] = {0};
394+
struct bss_info* it = reinterpret_cast<struct bss_info*>(_getScanInfoByIndex(i));
395+
if (!it)
396+
return String("");
397+
398+
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]);
399+
return String(mac);
400+
}
401+
361402
int32_t ESP8266WiFiClass::channel(uint8_t i)
362403
{
363404
struct bss_info* it = reinterpret_cast<struct bss_info*>(_getScanInfoByIndex(i));

libraries/ESP8266WiFi/src/ESP8266WiFi.h

+20-3
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
*
@@ -208,10 +218,17 @@ class ESP8266WiFiClass
208218
/**
209219
* return MAC / BSSID of scanned wifi
210220
* @param networkItem specify from which network item want to get the information
211-
* @return uint8_t * to MAC / BSSID of scanned wifi
221+
* @return uint8_t * MAC / BSSID of scanned wifi
212222
*/
213223
uint8_t * BSSID(uint8_t networkItem);
214224

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

0 commit comments

Comments
 (0)