首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

AsyncUdpSocket的简略应用和 fetchSSIDInfo 获取设备sid

2013-10-10 
AsyncUdpSocket的简单应用和 fetchSSIDInfo 获取设备sid- (id)fetchSSIDInfo{? ? NSArray *ifs (__bridg

AsyncUdpSocket的简单应用和 fetchSSIDInfo 获取设备sid

- (id)fetchSSIDInfo

{

? ? NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();

? ? NSLog(@"%s: Supported interfaces: %@", __func__, ifs);

? ? id info = nil;

? ? for (NSString *ifnam in ifs) {

? ? ? ? info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);

? ? ? ? if (info && [info count]) {

? ? ? ? ? ? break;

? ? ? ? }

? ? }

? ? return info ;

}

?

?

?

?

?

?

用socket添加 两个文件 AsyncUdpSocket.h ?和 AsyncUdpSocket.m?

SystemControm.framework ? ?CFNetwork.framework

?

#import "ViewController.h"

?

#import <SystemConfiguration/CaptiveNetwork.h>

?

@interfaceViewController ()

?

@property(nonatomic,strong) AsyncUdpSocket *socket;

?

@end

?

@implementation ViewController

?

- (void)viewDidLoad

{

? ? [superviewDidLoad];

? ? self.socket = [[AsyncUdpSocketalloc] initWithDelegate:self];

?? ?

? ? NSError *error = nil;

? ? [self.socket bindToPort:3000 error:&error];

? ? [self.socketenableBroadcast:YESerror:&error];

? ? [self.socketjoinMulticastGroup:@"239.255.255.250"error:&error];

?? ?

? ? if (error) {

? ? ? ? NSLog(@"error: %@",error);

? ? }

?? ?

? ? [self.socketreceiveWithTimeout:-1tag:1];

?? ?

? ? NSLog(@"start udp server");

?? ?

?? ?

? ? NSDictionary *ifs = [self fetchSSIDInfo]; //获取sid信息。

?? ?

? ? NSString *ssid = [[ifs objectForKey:@"SSID"] lowercaseString];

? ? NSLog(@"ssid:%@",ssid);

? ? _wifiName.text = [NSString stringWithFormat:@"Wifi name:\n%@",ssid];

}

?

- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{

? ? NSString *receiveData = [[NSStringalloc] initWithData:data encoding:NSUTF8StringEncoding];

? ? NSLog(@"received data: %@",receiveData);

?? ?

? ? _data.text = [NSString stringWithFormat:@"Received data:\n%@",receiveData];

?? ?

? ? [self.socket receiveWithTimeout:-1 tag:1];

? ? returnYES;

}

?

?

- (id)fetchSSIDInfo

{

? ? NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();

? ? NSLog(@"%s: Supported interfaces: %@", __func__, ifs);

? ? id info = nil;

? ? for (NSString *ifnam in ifs) {

? ? ? ? info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);

? ? ? ? if (info && [info count]) {

? ? ? ? ? ? break;

? ? ? ? }

? ? }

? ? return info ;

}

?

热点排行