文档章节

在ios上获取wifi的相关信息

S
 SimonXun
发布于 2017/02/05 13:48
字数 761
阅读 31
收藏 0
在ios扫描公共区域内wifi信息中,写了实现wifi扫描的一种方法,但是那种方法扫描出来的wifi信息不全,下面是扫描全部wifi信息的实现方法:
  
#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/NSTimer.h>
#import <Foundation/Foundation.h>
#include <dlfcn.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
@interface MSNetworksManager : NSObject {
  
    NSMutableDictionary *networks;
    NSArray *types;
    int autoScanInterval;
    bool scanning;
    bool autoScanning;
    void *libHandle;
    void *airportHandle;
      
    int (*open)(void *);
    int (*bind)(void *, NSString *);
    int (*close)(void *);
    int (*associate)(void *, NSDictionary*, NSString*);
    int (*scan)(void *, NSArray **, void *);
      
    //int (*open)(void *);
    //int (*bind)(void *, NSString *);
    //int (*close)(void *);
    //int (*scan)(void *, NSArray **, void *);
    //int (*associate)(void*, NSDictionary *, NSString *);
    int (*getpower)(void *, char *);
    int (*setpower)(void*, char*);
}
+ (MSNetworksManager *)sharedNetworksManager;
+ (NSNumber *)numberFromBSSID:(NSString *) bssid;
- (NSMutableDictionary *)networks;
- (NSDictionary *)networks:(int) type;
- (NSDictionary *)network:(NSString *) aNetwork;
- (id)init;
- (void)dealloc;
- (int)numberOfNetworks;
- (int)numberOfNetworks:(int) type;
- (int)autoScanInterval;
- (void)scan;
- (void)removeNetwork:(NSString *)aNetwork;
- (void)removeAllNetworks;
- (void)removeAllNetworks:(int) type;
- (void)autoScan:(bool)scan;
- (bool)autoScan;
- (void)scanSelector:(id)param;
- (void)setAutoScanInterval:(int) scanInterval;
- (int)associateNetwork: (NSDictionary *)bss: (NSString *)password;
- (int)getPower: (char *)power;
- (int)setPower: (char *)power;
- (NSString *) localIPAddress;
  
@end
  
   
  
   
  
  
.m文件:
  
#import "MSNetworksManager.h"
static MSNetworksManager *NetworksManager;
  
@implementation MSNetworksManager
+ (MSNetworksManager *)sharedNetworksManager
{
    if (!NetworksManager)
        NetworksManager = [[MSNetworksManager alloc] init];
    return NetworksManager;
}
  
+ (NSNumber *)numberFromBSSID:(NSString *) bssid
{
    int x = 0;
    uint64_t longmac;
    int MAC_LEN = 6;
    short unsigned int *bs_in = malloc(sizeof(short unsigned int) * MAC_LEN);
    if (sscanf([bssid cStringUsingEncoding: [NSString defaultCStringEncoding]],"%hX:%hX:%hX:%hX:%hX:%hX",&bs_in[0], &bs_in[1], &bs_in[2], &bs_in[3], &bs_in[4], &bs_in[5]) == MAC_LEN)
    {
        for (x = 0; x < MAC_LEN; x++)
            longmac |= (uint64_t) bs_in[x] << ((MAC_LEN – x – 1) * 8);
    } else {
        NSLog(@"WARN: invalid mac address! %@",self);
    }
    free(bs_in);
    return [NSNumber numberWithUnsignedLongLong:longmac];
}
  
- (NSDictionary *)networks
{
    // TODO: Implement joining of network types
    return networks;
}
- (NSDictionary *)networks:(int) type
{
    // TODO: Implement selecting of network types
    if(type != 0)
        NSLog(@"WARN: Non 80211 networks are not supported. %@",self);
    return networks;
}
  
- (NSDictionary *)network:(NSString *) aNetwork
{
    return [networks objectForKey: aNetwork];
}
  
- (id)init
{
    self = [super init];
    NetworksManager = self;
    networks = [[NSMutableDictionary alloc] init];
    types = [NSArray arrayWithObjects:@"80211", @"Bluetooth", @"GSM", nil];
    [types retain];
    autoScanInterval = 5; //seconds
    // For iPhone 2.0
    // libHandle = dlopen("/System/Library/PrivateFrameworks/Apple80211.framework/Apple80211", RTLD_LAZY);
    // For iPhone 3.0
  
    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");
    associate = dlsym(libHandle, "Apple80211Associate");
    getpower = dlsym(libHandle, "Apple80211GetPower");
    setpower = dlsym(libHandle, "Apple80211SetPower");
      
    open(&airportHandle);
    bind(airportHandle, @"en0");
      
    return self;
}
  
- (void)dealloc
{
    close(&airportHandle);
    [super dealloc];
}
  
- (int)numberOfNetworks
{
    return [networks count];
}
- (int)numberOfNetworks:(int) type
{
    // TODO: Implement selecting of network types
    if(type != 0)
        NSLog(@"WARN: Non 80211 networks are not supported. %@",self);
    return [networks count];
}
  
- (int)autoScanInterval
{
    return autoScanInterval;
}
  
- (void)scan
{
//    NSLog(@"Scanning…");
    scanning = true;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"startedScanning" object:self];
    NSArray *scan_networks;
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
    [parameters setObject:@"YES" forKey:@"SCAN_MERGE"];
    scan(airportHandle, &scan_networks, parameters);
    int i;
    //bool changed;
    [networks removeAllObjects];
    for (i = 0; i < [scan_networks count]; i++) {
                [networks setObject:[[scan_networks objectAtIndex: i] objectForKey:@"BSSID"] forKey:[[scan_networks objectAtIndex: i] objectForKey:@"RSSI"]];    
    }
   NSLog(@"Scan Finished…");
}
  
- (void)removeNetwork:(NSString *)aNetwork
{
    [networks removeObjectForKey:aNetwork];
}
  
- (void)removeAllNetworks
{
    [networks removeAllObjects];
}
  
- (void)removeAllNetworks:(int) type
{
    if(type != 0)
        NSLog(@"WARN: Non 80211 networks are not supported. %@",self);
    [networks removeAllObjects];
}
  
- (void)autoScan:(bool) bScan
{
    autoScanning = bScan;
    if(bScan) {
        [self scan];
        [NSTimer scheduledTimerWithTimeInterval:autoScanInterval target:self selector:@selector (scanSelector:) userInfo:nil repeats:NO];
    }
    NSLog(@"WARN: Automatic scanning not fully supported yet. %@",self);
}
  
- (bool)autoScan
{
    return autoScanning;
}
  
- (void)scanSelector:(id)param {
    if(autoScanning) {
        [self scan];
        [NSTimer scheduledTimerWithTimeInterval:autoScanInterval target:self selector:@selector (scanSelector:) userInfo:nil repeats:NO];
    }
}
  
- (void)setAutoScanInterval:(int) scanInterval
{
    autoScanInterval = scanInterval;
}
  
- (int)associateNetwork:(NSDictionary *)bss: (NSString *)password
{
    if(bss!=nil) {
        NSLog(@"associateNetwork");
        int ret = associate(airportHandle, bss, password);
        return ret;
    }else
        return -1;
}
  
- (int)getPower: (char *)power
{
    return getpower(airportHandle, power);
}
  
- (int)setPower: (char *)power
{
    return setpower(airportHandle, power);
}
  
- (NSString *) localIPAddress
{
    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
      
    // retrieve the current interfaces – returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0)
    {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL)
        {
            if(temp_addr->ifa_addr->sa_family == AF_INET)
            {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
                {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
              
            temp_addr = temp_addr->ifa_next;
        }
    }
      
    // Free memory
    freeifaddrs(interfaces);
    return address;
}
  
@end
  
   
  
添加到项目中即可。

© 著作权归作者所有

S
粉丝 5
博文 68
码字总数 54236
作品 0
深圳
程序员
私信 提问
疯狂ios讲义之使用CoreLocation定位(1)

9.1 iOS的定位支持 iOS设备支持的定位方式有多种,最早期的iPhone只支持通过运营商的基站信号进行定位,后来iOS系统开始引入WiFi支持,从而允许iOS设备通过周围的WiFi信号进行定位。 从iPhon...

博文视点
2014/04/02
1K
1
苹果向开发者发布 iOS 8 Beta 3,第三个测试版

苹果今天向开发者发布了 iOS 8 Beta 3,距离 Beta 2发布相隔三周。iOS 8 测试版在今年 WWDC 结束后发布,距离今天一个多月。iOS 8 Beta 3的编译号为12A4318c,iOS 8测试版用户可以通过 OTA ...

oschina
2014/07/08
1K
9
微信小程序连接WiFi(以及connectWifi在IOS的坑)

前言 最近做了一个小程序连接WiFi的需求,里面也遇到了一些坑。微信小程序提供了一组与WiFi相关的API,其中包括获取已连接的WiFi信息,连接某个WiFi,以及监听WiFi连接等功能。 使用wx.conn...

ITgecko
03/28
0
0
iOS 获取手机的IP地址(内网与外网)

联系人:石虎 QQ:1224614774 昵称:嗡嘛呢叭咪哄 QQ群:807236138 群称:iOS 技术交流学习群 一、概念 1.先是获取内网IP的方法: #import ifaddrs.h #import arpa/inet.h #import net/if.h #defi...

石虎132
2018/09/13
0
0
ZipperDown漏洞来了!微博、陌陌、快手等常用 iOS 应用恐要中招

雷锋网(公众号:雷锋网)消息,5月15日,盘古实验室对外宣布,他们在针对不同客户的 iOS 应用安全审计过程中,发现了一类通用的安全漏洞---ZipperDown漏洞。 10%的iOS应用可能受ZipperDow...

郭佳
2018/05/15
0
0

没有更多内容

加载失败,请刷新页面

加载更多

让《强化学习(第2版)》架起一座通往强化学习经典知识宝库的桥梁

上交大计算科学与工程系俞凯教授,5分钟口述讲解,带你快速认识了解年度重磅图书《强化学习(第二版)》! 在 AlphaGo战胜李世石之后,AlphaZero以其完全凭借自我学习超越人类在各种棋类游戏...

博文视点Bv
21分钟前
6
0
TLA7-EVM开发板的处理器、NOR FLASH、DDR3

TLA7-EVM开发板是一款由广州创龙基于Xilinx Artix-7系列FPGA自主研发的核心板+底板方式的开发板,可快速评估FPGA性能。核心板尺寸仅70mm*50mm,底板采用沉金无铅工艺的6层板设计,专业的PCB...

Tronlong创龙
30分钟前
4
0
UUID的变种-有序

为了解决UUID无序的问题,NHibernate在其主键生成方式中提供了Comb算法(combined guid/timestamp)。保留GUID的10个字节,用另6个字节表示GUID生成的时间(DateTime)。 /// <summary> //...

Canaan_
31分钟前
4
0
Netty学习(6)——通道间数据传输

1. FileChannel实现通道间的数据传输 在Java NIO中,如果两个通道中有一个是FileChannel,那你可以直接将数据从一个channel传输到另外一个channel。 transferFrom() FileChannel的transferF...

江左煤郎
34分钟前
4
0
AngularDOM操作

gtandsn
35分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部