智能升降桌-UDP广播和TCP链接
其实整个项目最不好理解的就是app端和硬件的交互,因为客户端很少回去接触底层的东西,所以开始会觉得不知所措,接下跟大家分享一下具体流程。
首先,我们接入了一个第三方,叫ESPTouch,把客户端所连接的WIFI信息发送给硬件,需要把WIFI的SSID和SECURT发送给硬件,用到的朋友可以去官网下个demo。
// 获取WIFI信息
#import <SystemConfiguration/CaptiveNetwork.h>
- (NSDictionary *)WIFIDic{
NSArray *ifs = (id)CFBridgingRelease(CNCopySupportedInterfaces());
NSLog(@"%s: Supported interfaces: %@", __func__, ifs);
for (NSString *ifnam in ifs) {
_WIFIDic = (id)CFBridgingRelease(CNCopyCurrentNetworkInfo((CFStringRef)ifnam));
// NSLog(@"%s: %@ => %@", __func__, ifnam, _WIFIDic);
if (_WIFIDic && [_WIFIDic count]) {
break;
} }
NSLog(@"wifi信息 %@ ",_WIFIDic);
return _WIFIDic ; }
然后,在链接成功的方法中发送UDP广播,给大家推荐一个很好用的第三方pod "CocoaAsyncSocket";
.h文件中:
<GCDAsyncUdpSocketDelegate>
self.updSuccess = NO;
self.udpSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError * error = nil;
// [_udpSocket bindToPort:46000 error:&error]; // 绑定端口 若客户端只向服务端发送消息而不用接收到其他的udp消息就可以不用绑定端口
[self.udpSocket enableBroadcast:YES error:&error]; // 启用广播
if (error) {//监听错误打印错误信息
NSLog(@"error:%@",error);
}else {//监听成功则开始接收信息
[_udpSocket receiveOnce:&error];
}
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
if (!self.updTimers) {
self.updTimers = [NSTimer scheduledTimerWithTimeInterval:2 repeats:YES block:^(NSTimer * _Nonnull timer) {
if (self.updSuccess == YES) {
[self.updTimers invalidate];
self.updTimers = nil;
}else{
//data 需要发送的信息 host IP地址 port 端口号
[_udpSocket sendData:data toHost:host port:port withTimeout:10 tag:0];
} }]; }
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag{
NSLog(@"发送信息成功");
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error{
NSLog(@"发送信息失败 %@",error);
}
#pragma mark --------------------UDP广播的回调----------------------------
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext{
self.updSuccess = YES;
NSString * ip = [GCDAsyncUdpSocket hostFromAddress:address];
uint16_t port = [GCDAsyncUdpSocket portFromAddress:address];
// NSString * message = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
if (!data || [data length] == 0) {
return;
}
// 十六进制转字符串
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
[data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
unsigned char *dataBytes = (unsigned char*)bytes;
for (NSInteger i = 0; i < byteRange.length; i++) {
NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
if ([hexStr length] == 2) {
[string appendString:hexStr];
} else {
[string appendFormat:@"0%@", hexStr]; } }
}];
NSLog(@"接收到服务端的消息:ip:%@ port:%d message:%@",ip,port,string);
if ([self.delegate respondsToSelector:@selector(clientSocketDidReceiveMessage:andPort:withHost:)]) {
[self.delegate clientSocketDidReceiveMessage:string andPort:port withHost:ip];
}
[self.udpSocket receiveOnce:nil];
}
这就好了,UPD完成,,然后在回调方法中发送TC:
.h文件中:<GCDAsyncSocketDelegate>
//将GCDAsynSocket设置为全局变量
self.tcpSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error = nil;
[self.tcpSocket connectToHost:servesIP onPort:port error:&error];
[self.tcpSocket writeData:[message dataUsingEncoding:NSUTF8StringEncoding] withTimeout:5 tag:101];
[self.tcpSocket readDataWithTimeout:5 tag:0];
//withTimeout: 超时时间 -1 表示不超时
//tag: 一个标识
-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
NSLog(@"链接成功");
}
// 服务器返回数据
- (void)socket:(GCDAsyncSocket*)sock didReadData:(NSData *)data withTag:(long)tag {
self.tcpIsSuccess = YES;
// 十六进制转字符串
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
[data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
unsigned char *dataBytes = (unsigned char*)bytes;
for (NSInteger i = 0; i < byteRange.length; i++) {
NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
if ([hexStr length] == 2) {
[string appendString:hexStr];
} else {
[string appendFormat:@"0%@", hexStr]; } } }];
[self.delegate tcpSocketDicReceiveMessage:string];
[self.tcpSocket readDataWithTimeout:5 tag:0];
}
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err {
NSLog(@"已经断开连接!");
if (self.tcpIsSuccess == NO) {
[self.delegate tcpConnectError]; }
[self.tcpSocket readDataWithTimeout:5 tag:0]; }
如果,回调成功,恭喜你,app与硬件交互完成了。