【发布时间】:2013-02-18 04:41:38
【问题描述】:
我需要为我的 iphone 应用程序获取 wifi 的 bssid 以确定它是否已连接。我该怎么做?
需要一些指导。
【问题讨论】:
-
想要什么..?请提供更多详细信息,然后有人可以帮助您..
标签: iphone ios reachability bssid
我需要为我的 iphone 应用程序获取 wifi 的 bssid 以确定它是否已连接。我该怎么做?
需要一些指导。
【问题讨论】:
标签: iphone ios reachability bssid
下面的答案是从这个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>;
}
请注意,模拟器不支持 if。在您的设备上进行测试。
【讨论】:
我是这样做的:
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);
}
}
【讨论】:
1.添加SystemConfiguration.framework
2.import
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;
}
希望对你有帮助
【讨论】: