zoukankan      html  css  js  c++  java
  • IOS零碎技术整理(3)-获取wifi列表

    1.   该功能实现基于MobileApple80211框架来进行开发,而目前该框架成为了私有框架,其中的API均为私有API。

    如果使用这些API可能导致应用不能上app store或者ios版本升级过程中,可能对私有api不兼容,导致程序莫名的挂掉或数据获取失败

    2.   终端必须越狱,且必须把程序部署到终端的/Applications目录下取得超级用户权限才能获得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/IPConfiguration.bundle/IPConfiguration", RTLD_LAZY);

        char *error;

        if (libHandle == NULL && (error = dlerror()) != NULL)  {

           NSLog(@">>>  error %s",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(@"===-scan_networks-======%@",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: "];

        for (id key in networks){

           [result appendString:[NSString stringWithFormat:@"%@ (MAC: %@), RSSI: %@, Channel: %@ ",

                                [[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

  • 相关阅读:
    运维自动化工具 Kickstart
    Ansible 运维自动化 ( 配置管理工具 )
    Redis 主从同步配置
    MONGO db INSTALLATION
    Innobackupex MySQL 全备、增备及恢复
    strace 使用
    LCD硬件原理
    多点触摸_电容屏驱动程序_实践_tiny4412
    单点触摸屏与多点触摸屏的异同
    多点触摸屏——电容屏驱动程序
  • 原文地址:https://www.cnblogs.com/v-jing/p/3302887.html
Copyright © 2011-2022 走看看