Difference between revisions of "MobileWiFi.framework"

From iPhone Development Wiki
Jump to: navigation, search
(Fix example code...)
Line 54: Line 54:
  
 
<source lang="c">
 
<source lang="c">
 +
 
#include <MobileWiFi.h>
 
#include <MobileWiFi.h>
+
 
WiFiManagerRef _manager;
+
static WiFiManagerRef _manager;
WiFiDeviceClientRef _client;
+
static void scan_callback(WiFiDeviceClientRef device, CFArrayRef results, CFErrorRef error, void *token);
  
 
int main()
 
int main()
 
{
 
{
    _manager = WiFiManagerClientCreate(kCFAllocatorDefault, 0);
+
_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);
  
    CFArrayRef devices = WiFiManagerClientCopyDevices(_manager);
+
WiFiManagerClientScheduleWithRunLoop(_manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    _client = (WiFiDeviceClientRef)CFArrayGetValueAtIndex(devices, 0);
+
WiFiDeviceClientScanAsync(client, (CFDictionaryRef)[NSDictionary dictionary], scan_callback, 0);
  
    WiFiManagerClientScheduleWithRunLoop(_manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
+
CFRelease(devices);
    WiFiDeviceClientScanAsync(_client, (CFDictionaryRef)[NSDictionary dictionary], scanCallback, 0);
 
  
    CFRelease(devices);
+
CFRunLoopRun();
 
}
 
}
  
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);
+
WiFiManagerClientUnscheduleFromRunLoop(_manager);
    CFRelease(_manager);
+
CFRelease(_manager);
 +
 
 +
CFRunLoopStop(CFRunLoopGetCurrent());
 
}
 
}
 
 
</source>
 
</source>
  

Revision as of 19:43, 25 July 2013

MobileWiFi.framework
Private Framework
Availabile 3.0 – present


MobileWiFi is the framework that manages WiFi functionality on iOS. It replaces the obsolete Apple80211 framework.

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

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);
CFNumberRef RSSI = (CFNumberRef)WiFiDeviceClientCopyProperty(client, kWiFiScaledRSSIKey);

float strength;
CFNumberGetValue(RSSI, kCFNumberFloatType, &strength);

strength = strength * 100;

// Round to the nearest integer.
strength = ceilf(strength);

// Convert to a negative number (RSSI is negative).
strength = strength * -1;

NSLog(@"WiFi signal strength: %f dBm", strength);

CFRelease(RSSI);
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()
{
	_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();
}

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

	WiFiManagerClientUnscheduleFromRunLoop(_manager);
	CFRelease(_manager);

	CFRunLoopStop(CFRunLoopGetCurrent());
}

References