热点连接优化

This commit is contained in:
kai60
2022-03-27 22:08:21 +08:00
parent 9a5fe717a2
commit 90bcb3e43a
6 changed files with 62 additions and 26 deletions
@@ -23,6 +23,7 @@ Strong ESPTouchTask* epTask;
Strong NSMutableString *trackString;
Strong MBProgressHUD *HUD;
Strong NSTimer *timer;
Strong UITapGestureRecognizer*tap;
/**
设备返回的mac地址
*/
@@ -44,7 +45,10 @@ Copy NSString *deviceId;
[self addTitleViewWithTitle:@"尝试与设备建立连接"];
[self.view addSubview:self.progressView];
UITapGestureRecognizer*tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tryBindAgain:)];
[self.progressView addGestureRecognizer:tap];
self.tap=tap;
self.tap.enabled=NO;
[self.view addSubview:self.titleL];
[self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
@@ -194,8 +198,8 @@ Copy NSString *deviceId;
} failure:^(NSError *err) {
if (self.retryTimes < 3) {
[self bindDeviceWithSsid:self.macAddress];
if (self.retryTimes < 10) {
[self bindDeviceWithDeviceId:self.deviceId];
self.retryTimes += 1;
} else {
[self endProgress];
@@ -296,11 +300,7 @@ Copy NSString *deviceId;
if (self.macAddress.length == 0) {
return;
}
self.isBindingDevice = YES;
if (self.bindTimer) {
[self.bindTimer invalidate];
self.bindTimer = nil;
}
self.retryTimes = 0;
if (self.deviceType==DEVICEPETS)
{
@@ -324,9 +324,9 @@ Copy NSString *deviceId;
self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.HUD.mode = MBProgressHUDModeIndeterminate;
self.HUD.labelText = msg;
if ([msg isEqualToString:@"Socket连接失败"])
if ([msg containsString:@"Socket连接失败"])
{
[self.HUD hide:YES afterDelay:3];
[self.HUD hide:YES afterDelay:5];
[self endProgress];
[self.progressView endProgressWithString:@"连接失败"];
}
@@ -392,8 +392,24 @@ Copy NSString *deviceId;
}
#pragma mark - bind
-(void)tryBindAgain:(UITapGestureRecognizer*)tap
{
[self bindDeviceWithSsid:self.macAddress];
}
-(void)bindDeviceWithSsid:(NSString*)bssid{
NSLog(@"设备bssid_%@",bssid);
if (self.isBindingDevice) {
return;
}
else
{
self.isBindingDevice = YES;
if (self.bindTimer) {
[self.bindTimer invalidate];
self.bindTimer = nil;
}
}
[self appendTrakContent:@"=================进入绑定函数================="];
NSString *userId = [dataContorl dataControlGetUserIdInfo];
@@ -489,13 +505,21 @@ Copy NSString *deviceId;
} failure:^(NSError *err) {
if (self.retryTimes < 6) {
[self bindDeviceWithSsid:self.macAddress];
self.retryTimes += 1;
self.isBindingDevice=NO;
if (self.retryTimes < 10) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.HUD.labelText = @"自动重连...";
[self bindDeviceWithSsid:self.macAddress];
self.retryTimes += 1;
});
} else {
[self endProgress];
[self endProgress];
[self.progressView endProgressWithString:@"点击重试"];
self.tap.enabled=YES;
self.isBindingDevice = NO;
self.HUD.labelText = @"请求异常";
self.HUD.labelText = err.localizedDescription;
[self.HUD hide:YES afterDelay:2];
[self appendTrakContent:[NSString stringWithFormat:@"绑定网络请求错误:%@", err.localizedDescription]];
self.bakbutton.userInteractionEnabled=YES;
@@ -507,6 +531,7 @@ Copy NSString *deviceId;
#pragma mark - Private
- (void)appendTrakContent:(NSString *)msg {
NSLog(@"msg=%@",msg);
[self.trackString appendString:msg];
[self.trackString appendString:@"\n"];
}
@@ -25,7 +25,7 @@
// CFShow((__bridge CFTypeRef)(infoDic));
NSString *app_Version=[infoDic objectForKey:@"CFBundleShortVersionString"];
NSLog(@"app_Version%@",app_Version);
app_Version=@"4.7.20";
app_Version=@"4.7.21";
NSString *versionStr = [NSString stringWithFormat:@"v%@",app_Version];
NSString *buildVersion = [infoDic objectForKey:@"CFBundleVersion"];
if (buildVersion.length > 0) {
@@ -49,6 +49,9 @@
NSError *error;
//本地接收端口
[self.clientSocket bindToPort:bindPort error:&error];
if (error) {
[self postmsg: error.localizedDescription];
}
}
return self;
}
@@ -130,7 +133,11 @@
self.tag = 0;
[self.clientSocket receiveWithTimeout:-1 tag:self.tag];
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:self.tag];
BOOL send= [self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:self.tag];
if (!send)
{
[self postmsg:@"发送失败"];
}
}
-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
@@ -153,26 +160,30 @@
}
-(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{
NSLog(@"关闭");
[self postmsg:@"连接关闭"];
}
-(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag{
NSLog(@"didSendData");
[self postmsg:@"指令发送"];
}
-(void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error{
NSLog(@"dueToError%@",error);
[self postmsg:@"Socket连接失败"];
[self postmsg:[NSString stringWithFormat:@"Socket连接失败:%@",error.localizedDescription]];
}
-(void)dealloc{
[self postmsg:@"dealloc"];
[self.clientSocket close];
}
- (void)postmsg:(NSString *)msg {