1.获取SSID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
+ (
id
) fetchWifiSSIDInfo {
NSArray
*ifs = (
id
)CNCopySupportedInterfaces();
id
info =
nil
;
for
(
NSString
*ifnam in ifs) {
info = (
id
)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
if
(info && [info count]) {
break
;
}
[info release];
}
[ifs release];
return
info;
}
|
2.获取路由器的IP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
//获取路由器的IP
//ssdp协议,需要路由器开启upnp
-(
void
) getApIP {
ssdpSock = [[AsyncUdpSocket alloc] initWithDelegate:
self
];
[ssdpSock enableBroadcast:TRUE error:
nil
];
NSString
*str = @
"M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX:3\r\nST: urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n\r\n"
;
[ssdpSock bindToPort:0 error:
nil
];
[ssdpSock joinMulticastGroup:@
"239.255.255.250"
error:
nil
];
[ssdpSock sendData:[str dataUsingEncoding:
NSUTF8StringEncoding
]
toHost:@
"239.255.255.250"
port:1900
withTimeout:3
tag:1];
[ssdpSock receiveWithTimeout:-1 tag:2];
[
NSTimer
scheduledTimerWithTimeInterval:3 target:
self
selector
:
@selector
(completeSearch:)
userInfo:
self
repeats:
NO
];
}
#pragma mark -
#pragma mark AsyncUdpSocket delegate
-(
void
) completeSearch: (
NSTimer
*)t {
[ssdpSock close];
ssdpSock =
nil
;
[ssdpSock release];
}
- (
void
)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(
long
)tag dueToError:(
NSError
*)error {
}
- (
BOOL
)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(
NSData
*)data withTag:(
long
)tag fromHost:(
NSString
*)host port:(UInt16)port{
[[Global shareInstance] setApIP:host];
return
YES
;
}
- (
void
)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(
long
)tag dueToError:(
NSError
*)error {
}
|
参考:
http://www.cocoachina.com/bbs/read.php?tid=40872
http://www.cocoachina.com/bbs/simple/?t110456.html