我有以下简单的方法:

const QString& UeNetworkManager::ueAccessPointMACAddress(const QString& interfaceName) const
{
    static QString ueMACAddress=QString();
    iwreq wrq;
    int result=0;
    int kernelSocket=iw_sockets_open();

    ueMACAddress.clear();

    result=iw_get_ext(kernelSocket,
                      interfaceName.toLocal8Bit().constData(),
                      SIOCGIWAP,
                      &wrq);

    ueMACAddress=result>=0?QString(wrq.u.addr.sa_data):"Invalid MAC address";

    iw_sockets_close(kernelSocket);

    return ueMACAddress;
}   // ueAccessPointMACAddress

现在,我想用这个方法从wrq结构-它的u.addr.sa_data成员中提取访问点MAC地址,这个成员(我想是的,不确定)包含访问点的MAC地址。以下调试屏幕截图显示u.addr.sa_data成员的值:
linux - iwlib尝试获取访问点名称-u.addr.sa_data包含可疑字符,导致空QString-LMLPHP
为什么我在代码行之后变空了?

最佳答案

我好像用错了命令,下面是工作方法:

const QStringList& UeNetworkManager::ueAccessPointMACAddresses(const QString& interfaceName)
{
    static QStringList ueMACAddresses=QStringList();
    iwrange rangeScan;
    wireless_scan_head scanResultHead;
    wireless_scan* scanResult=Q_NULLPTR;
    int kernelSocket=iw_sockets_open();

    ueMACAddresses.clear();

    if(iw_get_range_info(kernelSocket,
                         interfaceName.toLocal8Bit().constData(),
                         &rangeScan)>=0)
    {
        if(iw_scan(kernelSocket,
                   interfaceName.toLocal8Bit().data(),
                   rangeScan.we_version_compiled,
                   &scanResultHead)>=0)
        {
            scanResult=scanResultHead.result;

            while(scanResult!=Q_NULLPTR)
            {
                ueMACAddresses.append(QByteArray(scanResult->ap_addr.sa_data).toHex());

                scanResult=scanResult->next;
            }   // while
        }
        else
        {
            ueMACAddresses.append(tr("MAC scan failed."));
        }   // if
    }
    else
    {
            ueMACAddresses.append(interfaceName+
                                  " "+
                                  tr("range scan failed."));
    }   // if

    iw_sockets_close(kernelSocket);

    return ueMACAddresses;
}   // ueAccessPointMACAddress

07-27 18:36