c++ - ESP8266:如何在station模式下获取客户端的MAC地址(STA_MODE)?

标签 c++ esp8266 arduino-esp8266

我可以在接入点模式(AP_MODE)下获得客户端mac-address,请参见下面的suGetClientMacAddress()函数。可以在互联网上找到此代码。我尝试找出WiFi.mode(STA_MODE);中客户端的MAC地址,但是找不到关于此的任何有用信息,不可能吗?当看起来不可能的时候,为什么不可能呢?还是还有一个“技巧”来使它起作用?

在下面可以找到AP_MODE中的工作功能/版本

我使用的库是默认的ESP(v。2.4.0)库和Async Web / sockets库:
https://github.com/me-no-dev/ESPAsyncWebServer

#define SU_WCFG_SERVER_ASYNC


 ........
 ........

 #ifdef SU_WCFG_SERVER_ASYNC
  #define SUWebSocketServer       AsyncWebSocket
  #define SUWebServerRequest      AsyncWebServerRequest
 #else
  ......
 #endif

 extern "C" {
    #include "user_interface.h"   // Required for some low level wifi functions
 }

............
............


String suIpAddressToString( IPAddress ipAddress )
{ 
  return String( ipAddress[0] )+"."+
         String( ipAddress[1] )+"."+
         String( ipAddress[2] )+"."+
         String( ipAddress[3] );
}

IPAddress suStringToIpAddress( String ipAddress )
{ 
  IPAddress ip = IPAddress( 0, 0, 0, 0 );
  char* pStr   = (char*)ipAddress.c_str();
  uint8_t i    = strlen( pStr );
  uint8_t iPos = 4;

   // Test before start 
  if( i > 6 && pStr[i-1] != '.' )
  {
    while( i-- && iPos )    
    {
      if( pStr[i] == '.' || i == 0 )
      {
        ip[--iPos] = String( (char*)&pStr[i+(i>0)] ).toInt();
        pStr[i] = 0; // set null termination to truncate
      }
    }
  }

  return ip;
}

String suGetClientIp( SUWebServerRequest* request )
{ 
  if( request )
   { return suIpAddressToString( IPAddress( request->client()->remoteIP() )); }

  return "";  
}

函数:
String suGetClientMacAddress( SUWebServerRequest* request )
{ 
  if( request )
   {  
     struct ip_addr*       IPaddress;
     IPAddress             ip;
     struct station_info*  stat_info = wifi_softap_get_station_info();
     String                sClientIp = suGetClientIp( request );

     Serial.print( "Client ip: " );
     Serial.println( sClientIp );

     if( sClientIp.length() > 0 )
     {
      while( stat_info ) 
      {
        IPaddress = &stat_info->ip;
        ip = IPaddress->addr;

        if( suIpAddressToString( ip ) == sClientIp )
        {
           // Kind of rude and ugly to exit a loop and function this way
           // however it works with less 'check' overhead. OK to me.
          return String( stat_info->bssid[0], HEX )+":"+
                 String( stat_info->bssid[1], HEX )+":"+
                 String( stat_info->bssid[2], HEX )+":"+
                 String( stat_info->bssid[3], HEX )+":"+
                 String( stat_info->bssid[4], HEX )+":"+
                 String( stat_info->bssid[5], HEX );
        } 

        stat_info = STAILQ_NEXT(stat_info, next);
      }
     } 
   }

  return "";  
}

有任何提示使其在STA_MODE下工作吗?

最佳答案

暗示:

- stations (clients) connect to an AP (Access Point - server)
- an AP generally hosts RF (radio) mechanisms for the connections of stations

ESP8266可以是Station或AP,两者都不是。

除非另有特殊规定,WiFi站(作为客户端)通常不直接相互通信,例如在mesh / Ad Hoc网络或物理(电缆;)连接中。

关于c++ - ESP8266:如何在station模式下获取客户端的MAC地址(STA_MODE)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48894445/

相关文章:

c++ - Win-builds 与 MinGW-builds 之间的区别

c++ - “嵌套”模板 `>>` 问题已解决。 `<::` 呢?

nodemcu esp8266 中的 CORS 问题

linux - 如何使用 Linux CLI 知道某些网站的 SSL 指纹?

https - 无法使用 WifiClientSecure 将 https 协议(protocol)与 ESP8266 连接

c++ - 为什么 sizeof() 为这个类返回 4 和 2 个字节

c++ - istream 运算符跳过空格字符

c++ - 静态 map 初始化

c++ - 为什么我的 C++ Arduino 程序在写入数组时会中断?

arduino - 使 ESP32 WiFi/蓝牙协同工作