如何获取WLAN MAC地址
文章信息
- 开发伙伴平台:
S60 3rd Edition, S60 3rd Edition FP1
- 详细描述
下列代码演示了如何列出所有的网络接口,并获得一个WLAN接口的硬件(MAC)地址:
//------------------------------------------------------------------------------
#include <in_sock.h> // link against insock.lib, esock.lib
// Connect to the socket server
RSocketServ socketServ;
User::LeaveIfError(socketServ.Connect());
CleanupClosePushL(socketServ);
// Open a socket
RSocket socket;
User::LeaveIfError(socket.Open(socketServ,
KAfInet,
KSockStream,
KProtocolInetTcp));
CleanupClosePushL(socket);
// Start enumerating the interfaces
TPckgBuf<TSoInetInterfaceInfo> info;
socket.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl);
TBuf<32> macAddr;
while(socket.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info) == KErrNone)
{
if(info().iName.FindF(_L("Wlan")) == KErrNotFound)
{
continue;
}
macAddr.Zero();
for( TUint i = sizeof(SSockAddr) ; i < sizeof(SSockAddr) + 6 ; i++ )
{
if(i < (TUint)info().iHwAddr.Length())
{
macAddr.AppendFormat(_L("%02X:"), info().iHwAddr[i]);
}
}
if(macAddr.Length()) // remove trailing ':'
{
macAddr.Delete(macAddr.Length()-1, 1);
}
}
CleanupStack::PopAndDestroy(2); // socket, socketServ
// macAddr now contains the WLAN MAC address,
// (empty if no WLAN interface is found)
//------------------------------------------------------------------------------


(no comments yet)