如何以编程方式在iOS中获得以dBm为单位的WIFI信号强度

时间:2017-02-06 06:08:53

标签: ios objective-c iphone wifi

如何以编程方式在iOS中以 dBm 获取WIFI信号强度。

我也在示例应用程序中实现了MobileWiFi.framework代码。

但是我收到以下错误因为MobileWiFi.framework错过了Project,而我无法从任何示例应用程序中获取。

WiFiManagerClientCopyDevices ,引自:

ViewController中的

[ViewController getWifiSignalStregth:]

以下是我使用的示例代码。

-(IBAction)getWifiSignalStregth:(id)sender
{
    NSLog(@"getWifiSignalStregth Called");


    WiFiManagerRef manager = WiFiManagerClientCreate(kCFAllocatorDefault, 0);
    CFArrayRef devices = WiFiManagerClientCopyDevices(manager);

    WiFiDeviceClientRef client = (WiFiDeviceClientRef)CFArrayGetValueAtIndex(devices, 0);
    CFDictionaryRef data = (CFDictionaryRef)WiFiDeviceClientCopyProperty(client, CFSTR("RSSI"));
    CFNumberRef scaled = (CFNumberRef)WiFiDeviceClientCopyProperty(client, kWiFiScaledRSSIKey);

    CFNumberRef RSSI = (CFNumberRef)CFDictionaryGetValue(data, CFSTR("RSSI_CTL_AGR"));

    int raw;
    CFNumberGetValue(RSSI, kCFNumberIntType, &raw);

    float strength;
    CFNumberGetValue(scaled, kCFNumberFloatType, &strength);
    CFRelease(scaled);

    strength *= -1;

    // Apple uses -3.0.
    int bars = (int)ceilf(strength * -3.0f);
    bars = MAX(1, MIN(bars, 3));


    printf("WiFi signal strength: %d dBm\n\t Bars: %d\n", raw,  bars);

    CFRelease(data);
    CFRelease(scaled);
    CFRelease(devices);
    CFRelease(manager);



}

0 个答案:

没有答案