设为首页 收藏本站
查看: 905|回复: 0

[经验分享] ios扫描公共区域内wifi信息

[复制链接]

尚未签到

发表于 2015-9-30 12:39:48 | 显示全部楼层 |阅读模式
  最近项目中需要扫描环境周围中的wifi信息,查了一些资料,扫描wifi信息的api是苹果公司的私有api,使用这些api可能导致应用不能上app store或者ios版本升级过程中,可能对私有api不兼容。
  关于扫描wifi信息的参考资料:http://code.google.com/p/uwecaugmentedrealityproject/和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(@"====");
  源代码:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.wifiii/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-120941-1-1.html 上篇帖子: GPRS、EDGE、WIFI、CDMA、蓝牙、红外等各种接口传输速率的对比 下篇帖子: G1利用手机wifi连接笔记本wifi共享宽带上网
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表