This commit is contained in:
xuemh
2018-08-27 21:34:34 +08:00
parent eb993bbe01
commit ee03c2b4c8
4 changed files with 87 additions and 72 deletions
@@ -39,6 +39,10 @@
*/
@property (nonatomic, assign) BOOL isBindingDevice;
@property (nonatomic, strong) NSTimer *bindTimer;
@property (nonatomic, assign) NSInteger retryTimes;
@end
@implementation ConnectHotpotViewController
@@ -166,33 +170,37 @@
[self appendTrakContent:[NSString stringWithFormat:@"答复Mac地址:%@", backModel.senderMacAddress]];
self.macAddress = backModel.senderMacAddress;
__weak typeof(self) weakSelf = self;
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
__strong typeof(weakSelf) strongSelf = weakSelf;
BOOL networkOk = NO;
if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
if (![[strongSelf currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) {
networkOk = YES;
}
} else if (status == AFNetworkReachabilityStatusReachableViaWWAN) {
networkOk = YES;
}
if (!networkOk) {
return;
}
if (strongSelf.isBindingDevice) {
return;
}
if (strongSelf.macAddress.length == 0) {
return;
}
strongSelf.isBindingDevice = YES;
[strongSelf bindDeviceWithSsid:self.macAddress];
}];
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[IFishHotpotUDPHelper sharedInstance].delegate = nil;
[[IFishHotpotUDPHelper sharedInstance] broadCastRestartCommand];
self.bindTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startBinding) userInfo:nil repeats:YES];
}
- (void)startBinding {
BOOL networkOk = NO;
AFNetworkReachabilityStatus status = [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus;
if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
if (![[self currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) {
networkOk = YES;
}
} else if (status == AFNetworkReachabilityStatusReachableViaWWAN) {
networkOk = YES;
}
if (!networkOk) {
return;
}
if (self.isBindingDevice) {
return;
}
if (self.macAddress.length == 0) {
return;
}
self.isBindingDevice = YES;
if (self.bindTimer) {
[self.bindTimer invalidate];
self.bindTimer = nil;
}
self.retryTimes = 0;
[self bindDeviceWithSsid:self.macAddress];
}
- (void)udpHelperMessage:(NSString *)msg {
@@ -267,12 +275,16 @@
} failure:^(NSError *err) {
self.isBindingDevice = NO;
self.HUD.labelText = @"网络异常";
[self.HUD hide:YES afterDelay:2];
[self appendTrakContent:[NSString stringWithFormat:@"绑定网络请求错误:%@", err.localizedDescription]];
self.bakbutton.userInteractionEnabled=YES;
if (self.retryTimes < 3) {
[self bindDeviceWithSsid:self.macAddress];
self.retryTimes += 1;
} else {
self.isBindingDevice = NO;
self.HUD.labelText = @"请求异常";
[self.HUD hide:YES afterDelay:2];
[self appendTrakContent:[NSString stringWithFormat:@"绑定网络请求错误:%@", err.localizedDescription]];
self.bakbutton.userInteractionEnabled=YES;
}
}];
}
@@ -24,8 +24,6 @@
+ (IFishHotpotUDPHelper *)sharedInstance;
@property (nonatomic,strong) NSTimer *loopTimer;
@property (nonatomic, weak) id<IFishHotpotUDPHelperDelegate> delegate;
- (void)broadCastHotspotConnectCommandWith:(NSString *)ssidName pwd:(NSString *)pwd;
@@ -21,6 +21,7 @@
NSString *_pwd;
}
@end
@@ -43,7 +44,6 @@
UInt16 bindPort = 9954;
NSError *error;
[self.clientSocket enableBroadcast:YES error:&error];
//本地接收端口
[self.clientSocket bindToPort:bindPort error:&error];
[self.clientSocket receiveWithTimeout:-1 tag:0];
@@ -60,10 +60,10 @@
- (void)broadCastRestCommand {
[self postmsg:@"命令已发出"];
[self sendRestSocketData];
[self sendResetSocketData];
}
- (void)sendRestSocketData {
- (void)sendResetSocketData {
NSString* hostS = @"192.168.4.1";
ResetDeviceModel *model = [ResetDeviceModel new];
NSString *request=[NSString stringWithFormat:@"%@", model.description];
@@ -72,12 +72,12 @@
UInt16 port = 333;
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1];
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:0];
}
- (void)broadCastRestartCommand {
[self postmsg:@"命令已发出"];
[self sendRestSocketData];
[self sendRestartSocketData];
}
- (void)sendRestartSocketData {
@@ -90,7 +90,7 @@
UInt16 port = 333;
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1];
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:0];
}
-(void)sendConnectHotspotSoketdata{
@@ -105,14 +105,11 @@
NSData *data=[dataContorl stringToHexData:request];
UInt16 port = 333;
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1];
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:0];
}
-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
[self.loopTimer invalidate];
NSString* result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
IFishUDPHelperBackMsgModel *model = [[IFishUDPHelperBackMsgModel alloc] initWithData:data];
if ([model.result isEqualToString:@"01"]) {
@@ -128,9 +125,7 @@
}
-(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{
[self.loopTimer invalidate];
NSLog(@"关闭");
NSLog(@"关闭");
}
@@ -144,16 +139,12 @@
NSLog(@"dueToError%@",error);
[self postmsg:error.localizedDescription];
[self.loopTimer invalidate];
}
-(void)dealloc{
[self.clientSocket close];
[self.loopTimer invalidate];
}
- (void)postmsg:(NSString *)msg {