博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iphone中扫描wifi热点
阅读量:4690 次
发布时间:2019-06-09

本文共 10742 字,大约阅读时间需要 35 分钟。

最近项目中需要扫描环境周围中的wifi信息,查了一些资料,扫描wifi信息的api是苹果公司的私有api,使用这些api可能导致应用不能上app store或者ios版本升级过程中,可能对私有api不兼容。

关于扫描wifi信息的参考资料:和
下面是我在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 = 
; 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 =
; 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 
#import
#include
@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(@"====");

源代码:

转载于:https://www.cnblogs.com/ligun123/archive/2013/03/05/2944189.html

你可能感兴趣的文章
标记页面控件尺寸
查看>>
批处理文件中的路径问题
查看>>
appium+python 环境搭建
查看>>
WampServer下修改和重置MySQL密码
查看>>
hibernate出现No row with the given identifier exists问题
查看>>
为什么wait()和notify()属于Object类
查看>>
Part2_lesson3---ARM寄存器详解
查看>>
PHP 在5.1.* 和5.2.*之间 PDO数据库操作中的不同!
查看>>
导入properties时的坑
查看>>
python——网络编程
查看>>
Spark的39个机器学习库
查看>>
Electron学习笔记(一)
查看>>
Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
查看>>
配置NRPE的通讯
查看>>
VS2005编译VTK5.10.1
查看>>
shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言
查看>>
总结上海永辉云商高级前端职位面试题集
查看>>
匹配两个空格之间的字符。。。
查看>>
CSS 文字溢出 变成省略号 ...
查看>>
Spring事务
查看>>