当前位置:编程学习 > wap >>

怎样在ios上获取wifi的相关信息?

rt  怎样获取设备的wifi信息,如信号强弱,热点名称等 --------------------编程问答-------------------- 求出一般式,然后解方程。


C/C++ code
int
Calc_Two_Line_Point( double a1, double b1, double c1, double a2, double b2, double c2, OUT double *x, OUT double *y )
{
double xx, yy;
double e;

e ……


--------------------编程问答-------------------- 除 --------------------编程问答--------------------

在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

 

添加到项目中即可。
--------------------编程问答-------------------- LS已经做了不少工作:)顶一个 --------------------编程问答-------------------- 好牛。。。 --------------------编程问答-------------------- Marking --------------------编程问答-------------------- 想学,不知有什么书可读? --------------------编程问答-------------------- 楼主能把工程罚我一下吗?我把代码拷贝到工程中的时候编译不通过,谢谢啊。327965728@qq.com --------------------编程问答-------------------- 楼主可以发一份项目文件给我吗? 谢谢   jerry2801@gmail.com --------------------编程问答-------------------- 编译不通过啊,请问你们能够将工程发过来吗??? --------------------编程问答-------------------- 求楼主来一份能编译的工程代码~~ 不甚感激 934350746@qq.com --------------------编程问答-------------------- --------------------编程问答--------------------  项目要上线的,不越越能行吗,我没有越狱机器,我要做的也不是越狱项目 --------------------编程问答-------------------- 顶一个哈。。。很不错 --------------------编程问答-------------------- .m  文件的 
28- 29 行 那边

for (x = 0; x < MAC_LEN; x++)
            longmac |= (uint64_t) bs_in[x] << ((MAC_LEN - x – 1) * 8);

这个 报错了  Expected ')'

请问怎弄弄啊     --------------------编程问答--------------------     MSNetworksManager *manager = [MSNetworksManager sharedNetworksManager];
    NSString *add = [manager localIPAddress];
    NSLog(@"add:%@", add);


直接 Crash了 么。。。
error: address doesn't contain a section that points to a section in a object file

补充:移动开发 ,  iPhone
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,