转载了两篇文章,做个标记,有时间再研究下:
第一篇:
最近项目中需要扫描环境周围中的wifi信息,查了一些资料,扫描wifi信息的api是苹果公司的私有api,使用这些api可能导致应用不能上app store或者ios版本升级过程中,可能对私有api不兼容。
关于扫描wifi信息的参考资料:http://code.google.com/p/uwecaugmentedrealityproj<wbr>ect/和http://code.google.com/p/iphone-wireless/
下面是我在3gs ios 4.0上测试的结果:
2010-12-26 12:32:58.819 iphone.wifiii[538:307] Scanning WiFi Channels…
2010-12-26 12:32:59.695 iphone.wifiii[538:307] ===–======
(
{
AGE = 0;
"AP_MODE" = 2;
"BEACON_INT" = 10;
BSSID = "0:21:29:9d:79:d6";
CAPABILITIES = 1041;
CHANNEL = 6;
"CHANNEL_FLAGS" = 8;
IE = <30140100 000fac04 0100000f ac040100 000fac02 0000>;
NOISE = 0;
RATES = (
1,
2,
5,
11,
18,
24,
36,
54,
6,
9,
12,
48
);
"RSN_IE" = {
"IE_KEY_RSN_AUTHSELS" = (
2
);
"IE_KEY_RSN_MCIPHER" = 4;
"IE_KEY_RSN_UCIPHERS" = (
4
);
"IE_KEY_RSN_VERSION" = 1;
};
RSSI = "-91";
SSID = <6c696e6b 737973>;
"SSID_STR" = linksys;
},
{
AGE = 0;
"AP_MODE" = 2;
"BEACON_INT" = 10;
BSSID = "0:23:cd:2f:90:fa";
CAPABILITIES = 1073;
CHANNEL = 6;
"CHANNEL_FLAGS" = 8;
NOISE = 0;
RATES = (
1,
2,
5,
11,
6,
12,
24,
36,
9,
18,
48,
54
);
RSSI = "-88";
SSID = <62656964 6f756172 74>;
"SSID_STR" = beidouart;
WEP = 1;
},
{
AGE = 0;
"AP_MODE" = 2;
"BEACON_INT" = 10;
BSSID = "1c:bd:b9:d0:8b:f";
CAPABILITIES = 1041;
CHANNEL = 1;
"CHANNEL_FLAGS" = 8;
IE = <dd180050 f2010100 0050f202 01000050 f2020100 0050f202 0c00>;
NOISE = 0;
RATES = (
1,
2,
5,
11,
18,
24,
36,
54,
6,
9,
12,
48
);
RSSI = "-88";
SSID = <4368696e 614e6574 2d526d66 5a>;
"SSID_STR" = "ChinaNet-RmfZ";
"WPA_IE" = {
"IE_KEY_WPA_AUTHSELS" = (
2
);
"IE_KEY_WPA_MCIPHER" = 2;
"IE_KEY_WPA_UCIPHERS" = (
2
);
"IE_KEY_WPA_VERSION" = 1;
};
},
{
AGE = 0;
"AP_MODE" = 2;
"BEACON_INT" = 10;
BSSID = "94:c:6d:66:8:84";
CAPABILITIES = 1073;
CHANNEL = 7;
"CHANNEL_FLAGS" = 8;
IE = <30140100 000fac04 0100000f ac040100 000fac02 0100dd16 0050f201 01000050 f2040100 0050f204 01000050 f202>;
NOISE = 0;
RATES = (
1,
2,
5,
11,
6,
12,
24,
36,
9,
18,
48,
54
);
"RSN_IE" = {
"IE_KEY_RSN_AUTHSELS" = (
2
);
"IE_KEY_RSN_CAPS" = {
"GTKSA_REPLAY_COUNTERS" = 1;
"NO_PAIRWISE" = 0;
"PRE_AUTH" = 1;
"PTKSA_REPLAY_COUNTERS" = 1;
};
"IE_KEY_RSN_MCIPHER" = 4;
"IE_KEY_RSN_UCIPHERS" = (
4
);
"IE_KEY_RSN_VERSION" = 1;
};
RSSI = "-73";
SSID = <4850542d 54502d4c 494e4b5f 36363038 3834>;
"SSID_STR" = "HPT-TP-LINK_660884";
"WPA_IE" = {
"IE_KEY_WPA_AUTHSELS" = (
2
);
"IE_KEY_WPA_MCIPHER" = 4;
"IE_KEY_WPA_UCIPHERS" = (
4
);
"IE_KEY_WPA_VERSION" = 1;
};
},
{
AGE = 0;
"AP_MODE" = 2;
"BEACON_INT" = 10;
BSSID = "0:22:b0:b:a4:8f";
CAPABILITIES = 1041;
CHANNEL = 11;
"CHANNEL_FLAGS" = 8;
IE = <dd180050 f2010100 0050f202 01000050 f2020100 0050f202 0000>;
NOISE = 0;
RATES = (
1,
2,
5,
11,
18,
24,
36,
54,
6,
9,
12,
48
);
RSSI = "-73";
SSID = <4368696e 614e6574 2d565350 57454233>;
"SSID_STR" = "ChinaNet-VSPWEB3";
"WPA_IE" = {
"IE_KEY_WPA_AUTHSELS" = (
2
);
"IE_KEY_WPA_MCIPHER" = 2;
"IE_KEY_WPA_UCIPHERS" = (
2
);
"IE_KEY_WPA_VERSION" = 1;
};
},
{
AGE = 0;
"AP_MODE" = 2;
"BEACON_INT" = 10;
BSSID = "8:10:74:62:7f:4";
CAPABILITIES = 1041;
CHANNEL = 3;
"CHANNEL_FLAGS" = 8;
NOISE = 0;
RATES = (
1,
2,
5,
11,
6,
9,
12,
18,
24,
36,
48,
54
);
RSSI = "-63";
SSID = <56535020 57454220 33>;
"SSID_STR" = "VSP WEB 3";
WEP = 1;
},
{
AGE = 0;
"AP_MODE" = 2;
"BEACON_INT" = 10;
BSSID = "0:24:1:d0:23:f2";
CAPABILITIES = 1073;
CHANNEL = 1;
"CHANNEL_FLAGS" = 8;
NOISE = 0;
RATES = (
1,
2,
5,
11,
6,
9,
12,
18,
24,
36,
48,
54
);
RSSI = "-60";
SSID = <56535020 57454220 32>;
"SSID_STR" = "VSP WEB 2";
WEP = 1;
},
{
AGE = 0;
"AP_MODE" = 2;
"BEACON_INT" = 10;
BSSID = "0:19:e0:ae:2b:80";
CAPABILITIES = 1073;
CHANNEL = 6;
"CHANNEL_FLAGS" = 8;
NOISE = 0;
RATES = (
1,
2,
5,
11,
6,
12,
24,
36,
9,
18,
48,
54
);
RSSI = "-57";
SSID = <56535020 574542>;
"SSID_STR" = "VSP WEB";
WEP = 1;
}
)
2010-12-26 12:32:59.724 iphone.wifiii[538:307] Scanning WiFi Channels Finished.
2010-12-26 12:32:59.729 iphone.wifiii[538:307] —–wifi description———-
Networks State:
VSP WEB 3 (MAC: 8:10:74:62:7f:4), RSSI: -63, Channel: 3
VSP WEB 2 (MAC: 0:24:1:d0:23:f2), RSSI: -60, Channel: 1
ChinaNet-RmfZ (MAC: 1c:bd:b9:d0:8b:f), RSSI: -88, Channel: 1
beidouart (MAC: 0:23:cd:2f:90:fa), RSSI: -88, Channel: 6
ChinaNet-VSPWEB3 (MAC: 0:22:b0:b:a4:8f), RSSI: -73, Channel: 11
linksys (MAC: 0:21:29:9d:79:d6), RSSI: -91, Channel: 6
HPT-TP-LINK_660884 (MAC: 94:c:6d:66:8:84), RSSI: -73, Channel: 7
VSP WEB (MAC: 0:19:e0:ae:2b:80), RSSI: -57, Channel: 6
2010-12-26 12:32:59.735 iphone.wifiii[538:307] —-wifi size ——
8
2010-12-26 12:32:59.739 iphone.wifiii[538:307] ====
正好和我当时的环境的wifi信息一致。
下面是代码实现:
#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#include <dlfcn.h>
@interface SOLStumbler : NSObject {
NSMutableDictionary *networks; //Key: MAC Address (BSSID)
void *libHandle;
void *airportHandle;
int (*apple80211Open)(void *);
int (*apple80211Bind)(void *, NSString *);
int (*apple80211Close)(void *);
int (*associate)(void *, NSDictionary*, NSString*);
int (*apple80211Scan)(void *, NSArray **, void *);
}
- (NSDictionary *)networks; //returns all 802.11 scanned network(s)
- (NSDictionary *)network:(NSString *) BSSID; //return specific 802.11 network by BSSID (MAC Address)
- (void)scanNetworks;
- (int)numberOfNetworks;
@end
方法具体实现:
#import "SOLStumbler.h"
@implementation SOLStumbler
- (id)init
{
self = [super init];
networks = [[NSMutableDictionary alloc] init];
libHandle = dlopen("/System/Library/SystemConfiguration/WiFiManager.bundle/WiFiManager", RTLD_LAZY);
char *error;
if (libHandle == NULL && (error = dlerror()) != NULL) {
NSLog(@"%c",error);
exit(1);
}
apple80211Open = dlsym(libHandle, "Apple80211Open");
apple80211Bind = dlsym(libHandle, "Apple80211BindToInterface");
apple80211Close = dlsym(libHandle, "Apple80211Close");
apple80211Scan = dlsym(libHandle, "Apple80211Scan");
apple80211Open(&airportHandle);
apple80211Bind(airportHandle, @"en0");
return self;
}
- (NSDictionary *)network:(NSString *) BSSID
{
return [networks objectForKey:@"BSSID"];
}
- (NSDictionary *)networks
{
return networks;
}
- (void)scanNetworks
{
NSLog(@"Scanning WiFi Channels…");
NSDictionary *parameters = [[NSDictionary alloc] init];
NSArray *scan_networks; //is a CFArrayRef of CFDictionaryRef(s) containing key/value data on each discovered network
apple80211Scan(airportHandle, &scan_networks, parameters);
NSLog(@"===–======\n%@",scan_networks);
for (int i = 0; i < [scan_networks count]; i++) {
[networks setObject:[scan_networks objectAtIndex: i] forKey:[[scan_networks objectAtIndex: i] objectForKey:@"BSSID"]];
}
NSLog(@"Scanning WiFi Channels Finished.");
}
- (int)numberOfNetworks
{
return [networks count];
}
- ( NSString * ) description {
NSMutableString *result = [[NSMutableString alloc] initWithString:@"Networks State: \n"];
for (id key in networks){
[result appendString:[NSString stringWithFormat:@"%@ (MAC: %@), RSSI: %@, Channel: %@ \n",
[[networks objectForKey: key] objectForKey:@"SSID_STR"], //Station Name
key, //Station BBSID (MAC Address)
[[networks objectForKey: key] objectForKey:@"RSSI"], //Signal Strength
[[networks objectForKey: key] objectForKey:@"CHANNEL"] //Operating Channel
]];
}
return [NSString stringWithString:result];
}
- (void) dealloc {
apple80211Close(airportHandle);
[super dealloc];
}
@end
类的调用:
NSLog(@"——–");
SOLStumbler *networksManager = [[SOLStumbler alloc] init];
[networksManager scanNetworks];
NSLog(@"—–wifi description———-\n%@",[networksManager description]);
NSLog(@"—-wifi size ——\n%d",[networksManager numberOfNetworks]);
NSLog(@"====");
第二篇:
在ios上获取wifi的相关信息:
iOS中用来查询当前连接的网络信息的API即CNCopyCurrentNetworkInfo
这个API位于SystemConfiguration.framework里面,使用时需要增加.h和包含库文件
使用时可以直接包含
#import <SystemConfiguration/CaptiveNetwork.h>
代码如下:
+ (NSString *)getWifiName
{
NSString *wifiName = nil;
CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
if (!wifiInterfaces) {
return nil;
}
NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
for (NSString *interfaceName in interfaces) {
CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
if (dictRef) {
NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
NSLog(@"network info -> %@", networkInfo);
wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
CFRelease(dictRef);
}
}
CFRelease(wifiInterfaces);
return wifiName;
}
在ios扫描公共区域内wifi信息中,写了实现wifi扫描的一种方法,但是那种方法扫描出来的wifi信息不全,下面是扫描全部wifi信息的实现方法:
#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/NSTimer.h>
#import <Foundation/Foundation.h>
#include <dlfcn.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
@interface MSNetworksManager : NSObject {
NSMutableDictionary *networks;
NSArray *types;
int autoScanInterval;
bool scanning;
bool autoScanning;
void *libHandle;
void *airportHandle;
int (*open)(void *);
int (*bind)(void *, NSString *);
int (*close)(void *);
int (*associate)(void *, NSDictionary*, NSString*);
int (*scan)(void *, NSArray **, void *);
//int (*open)(void *);
//int (*bind)(void *, NSString *);
//int (*close)(void *);
//int (*scan)(void *, NSArray **, void *);
//int (*associate)(void*, NSDictionary *, NSString *);
int (*getpower)(void *, char *);
int (*setpower)(void*, char*);
}
+ (MSNetworksManager *)sharedNetworksManager;
+ (NSNumber *)numberFromBSSID:(NSString *) bssid;
- (NSMutableDictionary *)networks;
- (NSDictionary *)networks:(int) type;
- (NSDictionary *)network:(NSString *) aNetwork;
- (id)init;
- (void)dealloc;
- (int)numberOfNetworks;
- (int)numberOfNetworks:(int) type;
- (int)autoScanInterval;
- (void)scan;
- (void)removeNetwork:(NSString *)aNetwork;
- (void)removeAllNetworks;
- (void)removeAllNetworks:(int) type;
- (void)autoScan:(bool)scan;
- (bool)autoScan;
- (void)scanSelector:(id)param;
- (void)setAutoScanInterval:(int) scanInterval;
- (int)associateNetwork: (NSDictionary *)bss: (NSString *)password;
- (int)getPower: (char *)power;
- (int)setPower: (char *)power;
- (NSString *) localIPAddress;
@end
.m文件:
#import "MSNetworksManager.h"
static MSNetworksManager *NetworksManager;
@implementation MSNetworksManager
+ (MSNetworksManager *)sharedNetworksManager
{
if (!NetworksManager)
NetworksManager = [[MSNetworksManager alloc] init];
return NetworksManager;
}
+ (NSNumber *)numberFromBSSID:(NSString *) bssid
{
int x = 0;
uint64_t longmac;
int MAC_LEN = 6;
short unsigned int *bs_in = malloc(sizeof(short unsigned int) * MAC_LEN);
if (sscanf([bssid cStringUsingEncoding: [NSString defaultCStringEncoding]],"%hX:%hX:%hX:%hX:%hX:%hX",&bs_in[0], &bs_in[1], &bs_in[2], &bs_in[3], &bs_in[4], &bs_in[5]) == MAC_LEN)
{
for (x = 0; x < MAC_LEN; x++)
longmac |= (uint64_t) bs_in[x] << ((MAC_LEN – x – 1) * 8);
} else {
NSLog(@"WARN: invalid mac address! %@",self);
}
free(bs_in);
return [NSNumber numberWithUnsignedLongLong:longmac];
}
- (NSDictionary *)networks
{
// TODO: Implement joining of network types
return networks;
}
- (NSDictionary *)networks:(int) type
{
// TODO: Implement selecting of network types
if(type != 0)
NSLog(@"WARN: Non 80211 networks are not supported. %@",self);
return networks;
}
- (NSDictionary *)network:(NSString *) aNetwork
{
return [networks objectForKey: aNetwork];
}
- (id)init
{
self = [super init];
NetworksManager = self;
networks = [[NSMutableDictionary alloc] init];
types = [NSArray arrayWithObjects:@"80211", @"Bluetooth", @"GSM", nil];
[types retain];
autoScanInterval = 5; //seconds
// For iPhone 2.0
// libHandle = dlopen("/System/Library/PrivateFrameworks/Apple80211.framework/Apple80211", RTLD_LAZY);
// For iPhone 3.0
libHandle = dlopen("/System/Library/SystemConfiguration/WiFiManager.bundle/WiFiManager", RTLD_LAZY);
open = dlsym(libHandle, "Apple80211Open");
bind = dlsym(libHandle, "Apple80211BindToInterface");
close = dlsym(libHandle, "Apple80211Close");
scan = dlsym(libHandle, "Apple80211Scan");
associate = dlsym(libHandle, "Apple80211Associate");
getpower = dlsym(libHandle, "Apple80211GetPower");
setpower = dlsym(libHandle, "Apple80211SetPower");
open(&airportHandle);
bind(airportHandle, @"en0");
return self;
}
- (void)dealloc
{
close(&airportHandle);
[super dealloc];
}
- (int)numberOfNetworks
{
return [networks count];
}
- (int)numberOfNetworks:(int) type
{
// TODO: Implement selecting of network types
if(type != 0)
NSLog(@"WARN: Non 80211 networks are not supported. %@",self);
return [networks count];
}
- (int)autoScanInterval
{
return autoScanInterval;
}
- (void)scan
{
// NSLog(@"Scanning…");
scanning = true;
[[NSNotificationCenter defaultCenter] postNotificationName:@"startedScanning" object:self];
NSArray *scan_networks;
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:@"YES" forKey:@"SCAN_MERGE"];
scan(airportHandle, &scan_networks, parameters);
int i;
//bool changed;
[networks removeAllObjects];
for (i = 0; i < [scan_networks count]; i++) {
[networks setObject:[[scan_networks objectAtIndex: i] objectForKey:@"BSSID"] forKey:[[scan_networks objectAtIndex: i] objectForKey:@"RSSI"]];
}
NSLog(@"Scan Finished…");
}
- (void)removeNetwork:(NSString *)aNetwork
{
[networks removeObjectForKey:aNetwork];
}
- (void)removeAllNetworks
{
[networks removeAllObjects];
}
- (void)removeAllNetworks:(int) type
{
if(type != 0)
NSLog(@"WARN: Non 80211 networks are not supported. %@",self);
[networks removeAllObjects];
}
- (void)autoScan:(bool) bScan
{
autoScanning = bScan;
if(bScan) {
[self scan];
[NSTimer scheduledTimerWithTimeInterval:autoScanInterval target:self selector:@selector (scanSelector:) userInfo:nil repeats:NO];
}
NSLog(@"WARN: Automatic scanning not fully supported yet. %@",self);
}
- (bool)autoScan
{
return autoScanning;
}
- (void)scanSelector:(id)param {
if(autoScanning) {
[self scan];
[NSTimer scheduledTimerWithTimeInterval:autoScanInterval target:self selector:@selector (scanSelector:) userInfo:nil repeats:NO];
}
}
- (void)setAutoScanInterval:(int) scanInterval
{
autoScanInterval = scanInterval;
}
- (int)associateNetwork:(NSDictionary *)bss: (NSString *)password
{
if(bss!=nil) {
NSLog(@"associateNetwork");
int ret = associate(airportHandle, bss, password);
return ret;
}else
return -1;
}
- (int)getPower: (char *)power
{
return getpower(airportHandle, power);
}
- (int)setPower: (char *)power
{
return setpower(airportHandle, power);
}
- (NSString *) localIPAddress
{
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces – returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
@end
添加到项目中即可
网友评论