现在的位置: 首页 > 综合 > 正文

ios扫描公共区域内wifi信息

2018年08月03日 ⁄ 综合 ⁄ 共 8107字 ⁄ 字号 评论关闭
  1. 在ios扫描公共区域内wifi信息中,写了实现wifi扫描的一种方法,但是那种方法扫描出来的wifi信息不全,下面是扫描全部wifi信息的实现方法:  
  2.    
  3. #import <CoreFoundation/CoreFoundation.h>   
  4. #import <Foundation/NSTimer.h>   
  5. #import <Foundation/Foundation.h>   
  6. #include <dlfcn.h>   
  7. #include <ifaddrs.h>   
  8. #include <arpa/inet.h>   
  9. @interface MSNetworksManager : NSObject {  
  10.    
  11.     NSMutableDictionary *networks;   
  12.     NSArray *types;   
  13.     int autoScanInterval;   
  14.     bool scanning;   
  15.     bool autoScanning;   
  16.     voidvoid *libHandle;   
  17.     voidvoid *airportHandle;   
  18.        
  19.     int (*open)(voidvoid *);   
  20.     int (*bind)(voidvoid *, NSString *);   
  21.     int (*close)(voidvoid *);   
  22.     int (*associate)(voidvoid *, NSDictionary*, NSString*);   
  23.     int (*scan)(voidvoid *, NSArray **, voidvoid *);   
  24.        
  25.     //int (*open)(void *);   
  26.     //int (*bind)(void *, NSString *);   
  27.     //int (*close)(void *);   
  28.     //int (*scan)(void *, NSArray **, void *);   
  29.     //int (*associate)(void*, NSDictionary *, NSString *);   
  30.     int (*getpower)(voidvoid *, charchar *);   
  31.     int (*setpower)(void*, char*);   
  32. }   
  33. + (MSNetworksManager *)sharedNetworksManager;   
  34. + (NSNumber *)numberFromBSSID:(NSString *) bssid;   
  35. - (NSMutableDictionary *)networks;   
  36. - (NSDictionary *)networks:(int) type;   
  37. - (NSDictionary *)network:(NSString *) aNetwork;   
  38. - (id)init;   
  39. - (void)dealloc;   
  40. - (int)numberOfNetworks;   
  41. - (int)numberOfNetworks:(int) type;   
  42. - (int)autoScanInterval;   
  43. - (void)scan;   
  44. - (void)removeNetwork:(NSString *)aNetwork;   
  45. - (void)removeAllNetworks;   
  46. - (void)removeAllNetworks:(int) type;   
  47. - (void)autoScan:(bool)scan;   
  48. - (bool)autoScan;   
  49. - (void)scanSelector:(id)param;   
  50. - (void)setAutoScanInterval:(int) scanInterval;   
  51. - (int)associateNetwork: (NSDictionary *)bss: (NSString *)password;   
  52. - (int)getPower: (charchar *)power;   
  53. - (int)setPower: (charchar *)power;   
  54. - (NSString *) localIPAddress;  
  55.    
  56. @end  
  57.    
  58.     
  59.    
  60.     
  61.    
  62.    
  63. .m文件:  
  64.    
  65. #import "MSNetworksManager.h"   
  66. static MSNetworksManager *NetworksManager;  
  67.    
  68. @implementation MSNetworksManager   
  69. + (MSNetworksManager *)sharedNetworksManager   
  70. {   
  71.     if (!NetworksManager)   
  72.         NetworksManager = [[MSNetworksManager alloc] init];   
  73.     return NetworksManager;   
  74. }  
  75.    
  76. + (NSNumber *)numberFromBSSID:(NSString *) bssid   
  77. {   
  78.     int x = 0;   
  79.     uint64_t longmac;   
  80.     int MAC_LEN = 6;   
  81.     short unsigned intint *bs_in = malloc(sizeof(short unsigned int) * MAC_LEN);   
  82.     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)   
  83.     {   
  84.         for (x = 0; x < MAC_LEN; x++)   
  85.             longmac |= (uint64_t) bs_in[x] << ((MAC_LEN – x – 1) * 8);   
  86.     } else {   
  87.         NSLog(@"WARN: invalid mac address! %@",self);   
  88.     }   
  89.     free(bs_in);   
  90.     return [NSNumber numberWithUnsignedLongLong:longmac];   
  91. }  
  92.    
  93. - (NSDictionary *)networks   
  94. {   
  95.     // TODO: Implement joining of network types   
  96.     return networks;   
  97. }   
  98. - (NSDictionary *)networks:(int) type   
  99. {   
  100.     // TODO: Implement selecting of network types   
  101.     if(type != 0)   
  102.         NSLog(@"WARN: Non 80211 networks are not supported. %@",self);   
  103.     return networks;   
  104. }  
  105.    
  106. - (NSDictionary *)network:(NSString *) aNetwork   
  107. {   
  108.     return [networks objectForKey: aNetwork];   
  109. }  
  110.    
  111. - (id)init   
  112. {   
  113.     self = [super init];   
  114.     NetworksManager = self;   
  115.     networks = [[NSMutableDictionary alloc] init];   
  116.     types = [NSArray arrayWithObjects:@"80211"@"Bluetooth"@"GSM", nil nil];   
  117.     [types retain];   
  118.     autoScanInterval = 5//seconds   
  119.     // For iPhone 2.0   
  120.     // libHandle = dlopen("/System/Library/PrivateFrameworks/Apple80211.framework/Apple80211", RTLD_LAZY);   
  121.     // For iPhone 3.0  
  122.    
  123.     libHandle = dlopen("/System/Library/SystemConfiguration/WiFiManager.bundle/WiFiManager", RTLD_LAZY);   
  124.     open = dlsym(libHandle, "Apple80211Open");   
  125.     bind = dlsym(libHandle, "Apple80211BindToInterface");   
  126.     close = dlsym(libHandle, "Apple80211Close");   
  127.     scan = dlsym(libHandle, "Apple80211Scan");   
  128.     associate = dlsym(libHandle, "Apple80211Associate");   
  129.     getpower = dlsym(libHandle, "Apple80211GetPower");   
  130.     setpower = dlsym(libHandle, "Apple80211SetPower");   
  131.        
  132.     open(&airportHandle);   
  133.     bind(airportHandle, @"en0");   
  134.        
  135.     return self;   
  136. }  
  137.    
  138. - (void)dealloc   
  139. {   
  140.     close(&airportHandle);   
  141.     [super dealloc];   
  142. }  
  143.    
  144. - (int)numberOfNetworks   
  145. {   
  146.     return [networks count];   
  147. }   
  148. - (int)numberOfNetworks:(int) type   
  149. {   
  150.     // TODO: Implement selecting of network types   
  151.     if(type != 0)   
  152.         NSLog(@"WARN: Non 80211 networks are not supported. %@",self);   
  153.     return [networks count];   
  154. }  
  155.    
  156. - (int)autoScanInterval   
  157. {   
  158.     return autoScanInterval;   
  159. }  
  160.    
  161. - (void)scan   
  162. {   
  163. //    NSLog(@"Scanning…");   
  164.     scanning = true;   
  165.     [[NSNotificationCenter defaultCenter] postNotificationName:@"startedScanning" object:self];   
  166.     NSArray *scan_networks;   
  167.     NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];   
  168.     [parameters setObject:@"YES" forKey:@"SCAN_MERGE"];   
  169.     scan(airportHandle, &scan_networks, parameters);   
  170.     int i;   
  171.     //bool changed;   
  172.     [networks removeAllObjects];   
  173.     for (i = 0; i < [scan_networks count]; i++) {   
  174.                 [networks setObject:[[scan_networks objectAtIndex: i] objectForKey:@"BSSID"] forKey:[[scan_networks objectAtIndex: i] objectForKey:@"RSSI"]];       
  175.     }   
  176.    NSLog(@"Scan Finished…");   
  177. }  
  178.    
  179. - (void)removeNetwork:(NSString *)aNetwork   
  180. {   
  181.     [networks removeObjectForKey:aNetwork];   
  182. }  
  183.    
  184. - (void)removeAllNetworks   
  185. {   
  186.     [networks removeAllObjects];   
  187. }  
  188.    
  189. - (void)removeAllNetworks:(int) type   
  190. {   
  191.     if(type != 0)   
  192.         NSLog(@"WARN: Non 80211 networks are not supported. %@",self);   
  193.     [networks removeAllObjects];   
  194. }  
  195.    
  196. - (void)autoScan:(bool) bScan   
  197. {   
  198.     autoScanning = bScan;   
  199.     if(bScan) {   
  200.         [self scan];   
  201.         [NSTimer scheduledTimerWithTimeInterval:autoScanInterval target:self selector:@selector (scanSelector:) userInfo:nil repeats:NO];   
  202.     }   
  203.     NSLog(@"WARN: Automatic scanning not fully supported yet. %@",self);   
  204. }  
  205.    
  206. - (bool)autoScan   
  207. {   
  208.     return autoScanning;   
  209. }  
  210.    
  211. - (void)scanSelector:(id)param {   
  212.     if(autoScanning) {   
  213.         [self scan];   
  214.         [NSTimer scheduledTimerWithTimeInterval:autoScanInterval target:self selector:@selector (scanSelector:) userInfo:nil repeats:NO];   
  215.     }   
  216. }  
  217.    
  218. - (void)setAutoScanInterval:(int) scanInterval   
  219. {   
  220.     autoScanInterval = scanInterval;   
  221. }  
  222.    
  223. - (int)associateNetwork:(NSDictionary *)bss: (NSString *)password   
  224. {   
  225.     if(bss!=nil) {   
  226.         NSLog(@"associateNetwork");   
  227.         int ret = associate(airportHandle, bss, password);   
  228.         return ret;   
  229.     }else   
  230.         return -1;   
  231. }  
  232.    
  233. - (int)getPower: (charchar *)power   
  234. {   
  235.     return getpower(airportHandle, power);   
  236. }  
  237.    
  238. - (int)setPower: (charchar *)power   
  239. {   
  240.     return setpower(airportHandle, power);   
  241. }  
  242.    
  243. - (NSString *) localIPAddress   
  244. {   
  245.     NSString *address = @"error";   
  246.     struct ifaddrs *interfaces = NULL;   
  247.     struct ifaddrs *temp_addr = NULL;   
  248.     int success = 0;   
  249.        
  250.     // retrieve the current interfaces – returns 0 on success   
  251.     success = getifaddrs(&interfaces);   
  252.     if (success == 0)   
  253.     {   
  254.         // Loop through linked list of interfaces   
  255.         temp_addr = interfaces;   
  256.         while(temp_addr != NULL)   
  257.         {   
  258.             if(temp_addr->ifa_addr->sa_family == AF_INET)   
  259.             {   
  260.                 // Check if interface is en0 which is the wifi connection on the iPhone   
  261.                 if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])   
  262.                 {   
  263.                     // Get NSString from C String   
  264.                     address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];   
  265.                 }   
  266.             }   
  267.                
  268.             temp_addr = temp_addr->ifa_next;   
  269.         }   
  270.     }   
  271.        
  272.     // Free memory   
  273.     freeifaddrs(interfaces);   
  274.     return address;   
  275. }  
  276.    
  277. @end  
  278.    
  279.     
  280.    
  281. 添加到项目中即可。  

抱歉!评论已关闭.