优化连接过程
This commit is contained in:
@@ -158,20 +158,20 @@
|
||||
|
||||
- (void)udpHelperCommandExecutedSuccess:(IFishUDPHelperBackMsgModel *)backModel {
|
||||
self.ssid = backModel.senderMacAddress;
|
||||
|
||||
self.HUD.labelText = @"请切当前Ifish-xxxx到其他网络,以进行后续操作";
|
||||
[self.view resignFirstResponder];
|
||||
self.HUD.labelText = @"请稍后";
|
||||
self.trackString = [NSMutableString string];
|
||||
[self appendTrakContent:@"=================收到设备答复==================="];
|
||||
[self appendTrakContent:[NSString stringWithFormat:@"答复状态码:%@", backModel.result]];
|
||||
[self appendTrakContent:[NSString stringWithFormat:@"答复Mac地址:%@", backModel.senderMacAddress]];
|
||||
[IFishHotpotUDPHelper sharedInstance].delegate = nil;
|
||||
[[IFishHotpotUDPHelper sharedInstance] broadCastRestartCommand];
|
||||
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 (![[self currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) {
|
||||
if (![[strongSelf currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) {
|
||||
networkOk = YES;
|
||||
}
|
||||
} else if (status == AFNetworkReachabilityStatusReachableViaWWAN) {
|
||||
@@ -180,16 +180,19 @@
|
||||
if (!networkOk) {
|
||||
return;
|
||||
}
|
||||
if (self.isBindingDevice) {
|
||||
if (strongSelf.isBindingDevice) {
|
||||
return;
|
||||
}
|
||||
if (self.macAddress.length == 0) {
|
||||
if (strongSelf.macAddress.length == 0) {
|
||||
return;
|
||||
}
|
||||
self.isBindingDevice = YES;
|
||||
[self bindDeviceWithSsid:self.macAddress];
|
||||
strongSelf.isBindingDevice = YES;
|
||||
[strongSelf bindDeviceWithSsid:self.macAddress];
|
||||
}];
|
||||
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
|
||||
|
||||
[IFishHotpotUDPHelper sharedInstance].delegate = nil;
|
||||
[[IFishHotpotUDPHelper sharedInstance] broadCastRestartCommand];
|
||||
}
|
||||
|
||||
- (void)udpHelperMessage:(NSString *)msg {
|
||||
@@ -203,7 +206,7 @@
|
||||
NSLog(@"设备bssid_%@",bssid);
|
||||
[self appendTrakContent:@"=================进入绑定函数================="];
|
||||
NSString *userId = [dataContorl dataControlGetUserIdInfo];
|
||||
self.HUD.labelText = @"开始绑定设备";
|
||||
self.HUD.labelText = @"正在绑定设备";
|
||||
[self appendTrakContent:[NSString stringWithFormat:@"绑定参数 userID:%@", userId]];
|
||||
[self appendTrakContent:[NSString stringWithFormat:@"绑定参数 macaddress:%@", bssid]];
|
||||
[AFHttpTool bindDeviceWithUserId:userId macAddress:bssid success:^(id response) {
|
||||
|
||||
@@ -21,8 +21,6 @@
|
||||
NSString *_pwd;
|
||||
}
|
||||
|
||||
@property (nonatomic, assign) BOOL backMessageReceived;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -35,60 +33,55 @@
|
||||
dispatch_once(&onceToken, ^{
|
||||
sharedInstace = [[self alloc] init];
|
||||
});
|
||||
|
||||
return sharedInstace;
|
||||
}
|
||||
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
|
||||
|
||||
UInt16 bindPort = 9954;
|
||||
|
||||
NSError *error;
|
||||
[self.clientSocket enableBroadcast:YES error:&error];
|
||||
//本地接收端口
|
||||
[self.clientSocket bindToPort:bindPort error:&error];
|
||||
[self.clientSocket receiveWithTimeout:-1 tag:0];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)broadCastHotspotConnectCommandWith:(NSString *)ssidName pwd:(NSString *)pwd {
|
||||
_ssidName = ssidName;
|
||||
_pwd = pwd;
|
||||
[self postmsg:@"命令已发出"];
|
||||
self.backMessageReceived = NO;
|
||||
self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:K_ReSendTimeInterval target:self selector:@selector(sendConnectHotspotSoketdata) userInfo:nil repeats:YES];
|
||||
[self sendConnectHotspotSoketdata];
|
||||
}
|
||||
|
||||
- (void)broadCastRestCommand {
|
||||
[self postmsg:@"命令已发出"];
|
||||
self.backMessageReceived = NO;
|
||||
self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:K_ReSendTimeInterval target:self selector:@selector(sendRestSocketData) userInfo:nil repeats:YES];
|
||||
[self sendRestSocketData];
|
||||
}
|
||||
|
||||
- (void)sendRestSocketData {
|
||||
NSString* hostS = @"192.168.4.1";
|
||||
|
||||
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
|
||||
//120.55.190.56 服务器IP 指令中用
|
||||
ResetDeviceModel *model = [ResetDeviceModel new];
|
||||
NSString *request=[NSString stringWithFormat:@"%@", model.description];
|
||||
|
||||
NSData *data=[dataContorl stringToHexData:request];
|
||||
|
||||
UInt16 port = 333;
|
||||
UInt16 bindPort = 9954;
|
||||
|
||||
NSError *error;
|
||||
|
||||
[self.clientSocket enableBroadcast:YES error:&error];
|
||||
|
||||
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1];
|
||||
|
||||
//本地接收端口
|
||||
[self.clientSocket bindToPort:bindPort error:&error];
|
||||
|
||||
[self.clientSocket receiveWithTimeout:-1 tag:0];
|
||||
}
|
||||
|
||||
- (void)broadCastRestartCommand {
|
||||
[self postmsg:@"命令已发出"];
|
||||
self.backMessageReceived = NO;
|
||||
self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:K_ReSendTimeInterval target:self selector:@selector(sendRestartSocketData) userInfo:nil repeats:YES];
|
||||
|
||||
[self sendRestSocketData];
|
||||
}
|
||||
|
||||
- (void)sendRestartSocketData {
|
||||
NSString* hostS = @"192.168.4.1";
|
||||
|
||||
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
|
||||
//120.55.190.56 服务器IP 指令中用
|
||||
RestartDeviceModel *model = [RestartDeviceModel new];
|
||||
NSString *request=[NSString stringWithFormat:@"%@", model.description];
|
||||
@@ -96,24 +89,13 @@
|
||||
NSData *data=[dataContorl stringToHexData:request];
|
||||
|
||||
UInt16 port = 333;
|
||||
UInt16 bindPort = 9954;
|
||||
|
||||
NSError *error;
|
||||
|
||||
[self.clientSocket enableBroadcast:YES error:&error];
|
||||
|
||||
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1];
|
||||
|
||||
//本地接收端口
|
||||
[self.clientSocket bindToPort:bindPort error:&error];
|
||||
|
||||
[self.clientSocket receiveWithTimeout:-1 tag:0];
|
||||
}
|
||||
|
||||
-(void)sendConnectHotspotSoketdata{
|
||||
NSString* hostS = @"192.168.4.1";
|
||||
|
||||
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
|
||||
//120.55.190.56 服务器IP 指令中用
|
||||
ConnectHotspotModel *model = [ConnectHotspotModel new];
|
||||
model.ssidName = _ssidName;
|
||||
@@ -123,27 +105,14 @@
|
||||
NSData *data=[dataContorl stringToHexData:request];
|
||||
|
||||
UInt16 port = 333;
|
||||
UInt16 bindPort = 9954;
|
||||
|
||||
NSError *error;
|
||||
|
||||
[self.clientSocket enableBroadcast:YES error:&error];
|
||||
|
||||
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1];
|
||||
|
||||
//本地接收端口
|
||||
[self.clientSocket bindToPort:bindPort error:&error];
|
||||
|
||||
[self.clientSocket receiveWithTimeout:-1 tag:0];
|
||||
}
|
||||
|
||||
|
||||
-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
|
||||
{
|
||||
if (self.backMessageReceived) {
|
||||
return YES;
|
||||
}
|
||||
self.backMessageReceived = YES;
|
||||
[self.loopTimer invalidate];
|
||||
|
||||
NSString* result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
|
||||
IFishUDPHelperBackMsgModel *model = [[IFishUDPHelperBackMsgModel alloc] initWithData:data];
|
||||
if ([model.result isEqualToString:@"01"]) {
|
||||
@@ -154,15 +123,12 @@
|
||||
AppDelegate *app =(AppDelegate*)[UIApplication sharedApplication].delegate;
|
||||
[app.window makeToast:[NSString stringWithFormat:@"收到答复:%@", result]];
|
||||
}
|
||||
[self.clientSocket close];
|
||||
[self.loopTimer invalidate];
|
||||
return YES;
|
||||
|
||||
}
|
||||
|
||||
-(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{
|
||||
|
||||
[self.clientSocket close];
|
||||
[self.loopTimer invalidate];
|
||||
NSLog(@"关闭");
|
||||
}
|
||||
@@ -179,7 +145,6 @@
|
||||
NSLog(@"dueToError%@",error);
|
||||
[self postmsg:error.localizedDescription];
|
||||
|
||||
[self.clientSocket close];
|
||||
[self.loopTimer invalidate];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user