nihaogirl 发表于 2015-9-30 12:39:48

ios扫描公共区域内wifi信息

  最近项目中需要扫描环境周围中的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 Scanning WiFi Channels…
2010-12-26 12:32:59.695 iphone.wifiii ===–======
(
      {
      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 Scanning WiFi Channels Finished.
2010-12-26 12:32:59.729 iphone.wifiii —–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 —-wifi size ——
8
2010-12-26 12:32:59.739 iphone.wifiii ====
  正好和我当时的环境的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 = ;
    networks = [ 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 ;
}
  - (NSDictionary *)networks
{
    return networks;
}
  - (void)scanNetworks
{
    NSLog(@"Scanning WiFi Channels…");
    NSDictionary *parameters = [ 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 < ; i++) {
       forKey:[ objectForKey:@"BSSID"]];
    }
    NSLog(@"Scanning WiFi Channels Finished.");   
}
  - (int)numberOfNetworks
{
    return ;
}
  - ( NSString * ) description {
    NSMutableString *result = [ initWithString:@"Networks State: \n"];
    for (id key in networks){
      [result appendString:[NSString stringWithFormat:@"%@ (MAC: %@), RSSI: %@, Channel: %@ \n",
                              [ objectForKey:@"SSID_STR"], //Station Name
                              key, //Station BBSID (MAC Address)
                              [ objectForKey:@"RSSI"], //Signal Strength
                              [ objectForKey:@"CHANNEL"]//Operating Channel
                              ]];
    }
    return ;
}
  - (void) dealloc {
    apple80211Close(airportHandle);
    ;
}
@end
  类的调用:
  NSLog(@"——–");
SOLStumbler *networksManager = [ init];
;
NSLog(@"—–wifi description———-\n%@",);
NSLog(@"—-wifi size ——\n%d",);
NSLog(@"====");
  源代码:http://easymorse-iphone.googlecode.com/svn/trunk/iphone.wifiii/
页: [1]
查看完整版本: ios扫描公共区域内wifi信息