Difference between revisions of "MobileWiFi.framework"

From iPhone Development Wiki
Jump to: navigation, search
(Scanning)
(More documentation!)
 
(9 intermediate revisions by 2 users not shown)
Line 4: Line 4:
 
}}
 
}}
  
'''MobileWiFi''' is the framework that manages WiFi functionality on iOS. It replaces the obsolete '''Apple80211''' framework. MobileWiFi.framework has a C API, and a unfinished reverse-engineered header is available [https://gist.github.com/Cykey/5007313 here].
+
'''MobileWiFi''' is the framework that manages WiFi functionality on iOS. It powers [[WiFiPicker.servicebundle]], the Wi-Fi settings page and more. Its main purpose is to be a front-end to [[wifid]], which acquires its data directly from the kernel drivers. It replaces the obsolete '''Apple80211''' framework.
  
'''Note''': Your program needs the ''com.apple.wifi.manager-access'' entitlement to use '''any''' of the WiFiManager functions.
+
For examples on how to use this framework, see the [[#Example Code | Example Code]] section of this page.
 +
 
 +
* '''Note''': Your program needs the ''com.apple.wifi.manager-access'' entitlement to use '''any''' of the WiFiManager functions.
 +
 
 +
= Example Code =
 +
 
 +
This section includes example code that uses this framework. For more in-depth examples, see the following open-source projects:
 +
* [https://github.com/Cykey/wifi WiFi]
 +
* [https://github.com/Cykey/airscan Airscan]
  
 
== Retrieving a list of known networks ==
 
== Retrieving a list of known networks ==
Line 12: Line 20:
 
#include <MobileWiFi.h>
 
#include <MobileWiFi.h>
  
WiFiManagerRef manager = WiFiManagerClientCreate();
+
WiFiManagerRef manager = WiFiManagerClientCreate(kCFAllocatorDefault, 0);
  
 
CFArrayRef networks = WiFiManagerClientCopyNetworks(manager);
 
CFArrayRef networks = WiFiManagerClientCopyNetworks(manager);
Line 21: Line 29:
 
CFRelease(networks);
 
CFRelease(networks);
 
</source>
 
</source>
 +
 
== Getting the WiFi signal strength ==
 
== Getting the WiFi signal strength ==
 
<source lang="c">
 
<source lang="c">
Line 26: Line 35:
 
#include <MobileWiFi.h>
 
#include <MobileWiFi.h>
  
// We're not using the manager here but you need to create one for this code to work.
+
WiFiManagerRef manager = WiFiManagerClientCreate(kCFAllocatorDefault, 0);
WiFiManagerRef manager = WiFiManagerClientCreate();
+
CFArrayRef devices = WiFiManagerClientCopyDevices(manager);
CFArrayRef devices = WiFiManagerClientCopyDevices();
 
  
 
WiFiDeviceClientRef client = (WiFiDeviceClientRef)CFArrayGetValueAtIndex(devices, 0);
 
WiFiDeviceClientRef client = (WiFiDeviceClientRef)CFArrayGetValueAtIndex(devices, 0);
CFNumberRef RSSI = (CFNumberRef)WiFiDeviceClientCopyProperty(client, kWiFiScaledRSSIKey);
+
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;
 
float strength;
CFNumberGetValue(RSSI, kCFNumberFloatType, &strength);
+
CFNumberGetValue(scaled, kCFNumberFloatType, &strength);
 +
CFRelease(scaled);
  
strength = strength * 100;
+
strength *= -1;
  
// Round to the nearest integer.
+
// Apple uses -3.0.
strength = ceilf(strength);
+
int bars = (int)ceilf(strength * -3.0f);
 +
bars = MAX(1, MIN(bars, 3));
  
// Convert to a negative number.
 
strength = strength * -1;
 
  
NSLog(@"WiFi signal strength: %f dBm", strength);
+
printf("WiFi signal strength: %d dBm\n\t Bars: %d\n", raw,  bars);
  
CFRelease(RSSI);
+
CFRelease(data);
 +
CFRelease(scaled);
 
CFRelease(devices);
 
CFRelease(devices);
 
CFRelease(manager);
 
CFRelease(manager);
Line 55: Line 70:
  
 
<source lang="c">
 
<source lang="c">
 +
 
#include <MobileWiFi.h>
 
#include <MobileWiFi.h>
 
WiFiManagerRef _manager;
 
WiFiDeviceClientRef _client;
 
  
int main()
+
static WiFiManagerRef _manager;
 +
static void scan_callback(WiFiDeviceClientRef device, CFArrayRef results, CFErrorRef error, void *token);
 +
 
 +
int main(int argc, char **argv)
 
{
 
{
    _manager = WiFiManagerClientCreate(kCFAllocatorDefault, 0);
+
_manager = WiFiManagerClientCreate(kCFAllocatorDefault, 0);
  
    CFArrayRef devices = WiFiManagerClientCopyDevices(_manager);
+
CFArrayRef devices = WiFiManagerClientCopyDevices(_manager);
    _client = (WiFiDeviceClientRef)CFArrayGetValueAtIndex(devices, 0);
+
if (!devices) {
 +
fprintf(stderr, "Couldn't get WiFi devices. Bailing.\n");
 +
exit(EXIT_FAILURE);
 +
}
  
    WiFiManagerClientScheduleWithRunLoop(_manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
+
WiFiDeviceClientRef client = (WiFiDeviceClientRef)CFArrayGetValueAtIndex(devices, 0);
    WiFiDeviceClientScanAsync(_client, (CFDictionaryRef)[NSDictionary dictionary], scanCallback, 0);
 
  
    CFRelease(devices);
+
WiFiManagerClientScheduleWithRunLoop(_manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
 +
WiFiDeviceClientScanAsync(client, (CFDictionaryRef)[NSDictionary dictionary], scan_callback, 0);
 +
 
 +
CFRelease(devices);
 +
 
 +
CFRunLoopRun();
 +
 
 +
        return 0;
 
}
 
}
  
void scanCallback(WiFiDeviceClientRef device, CFArrayRef results, WiFiErrorRef error, void *token)
+
static void scan_callback(WiFiDeviceClientRef device, CFArrayRef results, CFErrorRef error, void *token)
 
{
 
{
    NSLog(@"Networks: %@", results);
+
NSLog(@"Finished scanning! networks: %@", results);
    WiFiManagerClientUnscheduleFromRunLoop(_manager);
+
 
    CFRelease(_manager);
+
WiFiManagerClientUnscheduleFromRunLoop(_manager);
 +
CFRelease(_manager);
 +
 
 +
CFRunLoopStop(CFRunLoopGetCurrent());
 
}
 
}
 +
</source>
  
</source>
+
== References ==
 +
* Header: https://github.com/Cykey/ios-reversed-headers/blob/master/MobileWiFi/MobileWiFi.h
  
 
{{Navbox Frameworks}}
 
{{Navbox Frameworks}}

Latest revision as of 05:25, 13 July 2014

MobileWiFi.framework
Private Framework
Availabile 3.0 – present


MobileWiFi is the framework that manages WiFi functionality on iOS. It powers WiFiPicker.servicebundle, the Wi-Fi settings page and more. Its main purpose is to be a front-end to wifid, which acquires its data directly from the kernel drivers. It replaces the obsolete Apple80211 framework.

For examples on how to use this framework, see the Example Code section of this page.

  • Note: Your program needs the com.apple.wifi.manager-access entitlement to use any of the WiFiManager functions.

Example Code

This section includes example code that uses this framework. For more in-depth examples, see the following open-source projects:

Retrieving a list of known networks

#include <MobileWiFi.h>

WiFiManagerRef manager = WiFiManagerClientCreate(kCFAllocatorDefault, 0);

CFArrayRef networks = WiFiManagerClientCopyNetworks(manager);

NSLog(@"networks: %@", networks);

CFRelease(manager);
CFRelease(networks);

Getting the WiFi signal strength

#include <math.h>
#include <MobileWiFi.h>

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);

Scanning for nearby networks

#include <MobileWiFi.h>

static WiFiManagerRef _manager;
static void scan_callback(WiFiDeviceClientRef device, CFArrayRef results, CFErrorRef error, void *token);

int main(int argc, char **argv)
{
	_manager = WiFiManagerClientCreate(kCFAllocatorDefault, 0);

	CFArrayRef devices = WiFiManagerClientCopyDevices(_manager);
	if (!devices) {
		fprintf(stderr, "Couldn't get WiFi devices. Bailing.\n");
		exit(EXIT_FAILURE);
	}

	WiFiDeviceClientRef client = (WiFiDeviceClientRef)CFArrayGetValueAtIndex(devices, 0);

	WiFiManagerClientScheduleWithRunLoop(_manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
	WiFiDeviceClientScanAsync(client, (CFDictionaryRef)[NSDictionary dictionary], scan_callback, 0);

	CFRelease(devices);

	CFRunLoopRun();

        return 0;
}

static void scan_callback(WiFiDeviceClientRef device, CFArrayRef results, CFErrorRef error, void *token)
{
	NSLog(@"Finished scanning! networks: %@", results);

	WiFiManagerClientUnscheduleFromRunLoop(_manager);
	CFRelease(_manager);

	CFRunLoopStop(CFRunLoopGetCurrent());
}

References