我需要为我的iphone应用程序获取wifi的bssid,以确定它是否已连接。我该怎么做?
需要一些如何做的指导。
答案 0 :(得分:1)
以下答案将从此answer复制。
在iOS 4.1+上,你可以这样做:
#import <SystemConfiguration/CaptiveNetwork.h>
- (id)fetchSSIDInfo
{
NSArray *ifs = (id)CNCopySupportedInterfaces();
NSLog(@"%s: Supported interfaces: %@", __func__, ifs);
id info = nil;
for (NSString *ifnam in ifs) {
info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
NSLog(@"%s: %@ => %@", __func__, ifnam, info);
if (info && [info count]) {
break;
}
[info release];
}
[ifs release];
return [info autorelease];
}
示例输出:
2011-03-04 15:32:00.669 ShowSSID[4857:307] -[ShowSSIDAppDelegate fetchSSIDInfo]: Supported interfaces: (
en0
)
2011-03-04 15:32:00.693 ShowSSID[4857:307] -[ShowSSIDAppDelegate fetchSSIDInfo]: en0 => {
BSSID = "ca:fe:ca:fe:ca:fe";
SSID = XXXX;
SSIDDATA = <01234567 01234567 01234567>;
}
请注意,模拟器上不支持ifs。在您的设备上进行测试。
答案 1 :(得分:1)
1.添加SystemConfiguration.framework
2.import&lt; SystemConfiguration / CaptiveNetwork.h&GT;
3.使用以下方法
+(NSString *)currentWifiBSSID {
NSString *bssid = nil;
NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
for (NSString *ifnam in ifs) {
NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
NSLog(@"info:%@",info);
if (info[@"BSSID"]) {
bssid = info[@"BSSID"];
}
}
return bssid;
}
希望这有帮助
答案 2 :(得分:0)
我是这样做的:
NSArray* interfaces = (NSArray*) CNCopySupportedInterfaces();
for (NSString* interface in interfaces)
{
CFDictionaryRef networkDetails = CNCopyCurrentNetworkInfo((CFStringRef) interface);
if (networkDetails)
{
NSLog(@"all details: %@", (NSDictionary *)networkDetails);
NSLog(@"BSSID: %@", (NSString *)CFDictionaryGetValue (networkDetails, kCNNetworkInfoKeyBSSID));
BSSID1 = (NSString *)CFDictionaryGetValue (networkDetails, kCNNetworkInfoKeyBSSID);
BSSID = [[BSSID1 stringByReplacingOccurrencesOfString:@":"
withString:@""] uppercaseString];
NSLog(@"%@",BSSID);
CFRelease(networkDetails);
}
}