ios公共区域中搜索wifi信号信息总结

转载:http://www.cocoachina.com/bbs/simple/?t97108.html

ios公共区域中搜索wifi信号信息总结

通过这几天对wifi扫描的学习,总结ios自动搜索wifi热点并获取其信息(比如MAC,SSID,RSSI,CHANNEL)的方法,该功能实现都要基于MobileApple80211框架来进行开发,而目前该框架成为了私有框架,其中的API均为私有API。如果使用这些API可能导致应用不能上app store或者ios版本升级过程中,可能对私有api不兼容,导致程序莫名的挂掉。
其中扫描wifi信息的参考资料: http://code.google.com/p/uwecaugmentedrealityproject/ http://code.google.com/p/iphone-wireless/
源代码参考: http://easymorse-iphone.googlecode.com/svn/trunk/iphone.wifiii/

代码实现如下:
第一个类(SOLStumbler)文件
#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

第二个文件 界面设计
#import <UIKit/UIKit.h>

@class iphone_wifiiiViewController;

@interface iphone_wifiiiAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    iphone_wifiiiViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet iphone_wifiiiViewController *viewController;
@end


#import "iphone_wifiiiAppDelegate.h"
#import "iphone_wifiiiViewController.h"

@implementation iphone_wifiiiAppDelegate
@synthesize window;
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    // Override point for customization after application launch.
    //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");

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}
- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
}

#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}
- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}
@end

第三个类文件 视图控制
#import <UIKit/UIKit.h>
@interface iphone_wifiiiViewController : UIViewController {
}

#import "iphone_wifiiiViewController.h"
#import "SOLStumbler.h"
@implementation iphone_wifiiiViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"--------");
    SOLStumbler *networksManager = [[SOLStumbler alloc] init];
    [networksManager scanNetworks];
    NSLog(@"-----wifi description----------\n%@",[networksManager description]);
    NSLog(@"----wifi size ------\n%d",[networksManager numberOfNetworks]);
    NSLog(@"====");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}
- (void)dealloc {
    [super dealloc];
}

@end

@end


在iOS5上必须修改路径为System/Library/SystemConfiguration/IPConfiguration.bundle/IPConfiguration。而且你的终端必须越狱,且必须把程序部署到终端的/Applications目录下取得超级用户权限才能获得wifi的访问权限,


借助91助手直接把app文件部署到/Applications下即可获得这个权限的,或者把app做成deb文件修改安装目录也可以安装到/applications下,前提是你的手机必须已经越狱成功哦


这个API仅仅是扫描AP信号强度哦,即使不是私有的API 也不能提供定位的,真正的定位需要你自己做服务器做定位算法,wifi定位原理和方法有很多的,你可以在网上搜下,常见的就是网格法,即指纹法

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值