添加IFishHotpotUDPHelper

This commit is contained in:
xuemh 2018-08-06 17:58:03 +08:00
parent 65446cebfc
commit fc4062fbe8
4 changed files with 157 additions and 30 deletions

View File

@ -2483,6 +2483,7 @@
E5F1B03A21183E9700A879A1 /* setting-restart@3x.png in Resources */ = {isa = PBXBuildFile; fileRef = E5F1B03721183E9600A879A1 /* setting-restart@3x.png */; };
E5F1B03B21183E9700A879A1 /* setting-restart.png in Resources */ = {isa = PBXBuildFile; fileRef = E5F1B03821183E9600A879A1 /* setting-restart.png */; };
E5F1B03C21183E9700A879A1 /* setting-restart@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = E5F1B03921183E9700A879A1 /* setting-restart@2x.png */; };
E5F1B03F21184F9400A879A1 /* IFishHotpotUDPHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = E5F1B03D21184F9300A879A1 /* IFishHotpotUDPHelper.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -6285,6 +6286,8 @@
E5F1B03721183E9600A879A1 /* setting-restart@3x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "setting-restart@3x.png"; sourceTree = "<group>"; };
E5F1B03821183E9600A879A1 /* setting-restart.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "setting-restart.png"; sourceTree = "<group>"; };
E5F1B03921183E9700A879A1 /* setting-restart@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "setting-restart@2x.png"; sourceTree = "<group>"; };
E5F1B03D21184F9300A879A1 /* IFishHotpotUDPHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IFishHotpotUDPHelper.m; sourceTree = "<group>"; };
E5F1B03E21184F9400A879A1 /* IFishHotpotUDPHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IFishHotpotUDPHelper.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -9638,6 +9641,8 @@
883E781E1D48A5E50030E075 /* SelectDeviceViewController.m */,
883E781F1D48A5E50030E075 /* SetViewController.h */,
883E78201D48A5E50030E075 /* SetViewController.m */,
E5F1B03E21184F9400A879A1 /* IFishHotpotUDPHelper.h */,
E5F1B03D21184F9300A879A1 /* IFishHotpotUDPHelper.m */,
E56A745F20FCDBFC000129C9 /* ConnectHotpotViewController.h */,
E56A746020FCDBFC000129C9 /* ConnectHotpotViewController.m */,
E56A746120FCDBFC000129C9 /* ConnectHotpotViewController.xib */,
@ -16325,6 +16330,7 @@
88779EAF1E28BD4200209ABA /* KanHuFuWuViewController.m in Sources */,
88602F661E12002600F3DB5D /* NTESSnapchatAttachment.m in Sources */,
88742C2A1D63F4EA0074F1F8 /* ShopCertificationViewController.m in Sources */,
E5F1B03F21184F9400A879A1 /* IFishHotpotUDPHelper.m in Sources */,
88602DBB1E10D97E00F3DB5D /* UIImage+MultiFormat.m in Sources */,
881671DF1EA5DE2B00BEBF23 /* UMComUserTableViewCell.m in Sources */,
881671EE1EA5DE2B00BEBF23 /* UMComGridView.m in Sources */,

View File

@ -10,17 +10,15 @@
#import "ConnectHotspotTipViewController.h"
#import "dataContorl.h"
#import<SystemConfiguration/CaptiveNetwork.h>
#import "GCDAsyncUdpSocket.h"
#import "IFishHotpotUDPHelper.h"
@interface ConnectHotpotViewController ()<UITextFieldDelegate, GCDAsyncUdpSocketDelegate>
@interface ConnectHotpotViewController ()<UITextFieldDelegate, IFishHotpotUDPHelperDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITextField *nameLbl;
@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *dashView;
@property (weak, nonatomic) IBOutlet UITextField *pwdTf;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (nonatomic, strong) GCDAsyncUdpSocket *socket;
@end
@implementation ConnectHotpotViewController
@ -124,35 +122,14 @@
#pragma mark - UDP related
- (void)sendToDeviceWithSSIDName:(NSString *)name andSSIDPWD:(NSString *)pwd {
self.socket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *error;
[self.socket bindToPort:9090 error:&error];
[self.socket enableBroadcast:YES error:&error];
if (error) {
[self.view makeToast:error.localizedDescription];
} else {
[self.socket beginReceiving:&error];
}
[self.socket sendData:[self updmessageData:name ssidPwd:pwd] toHost:@"192.168.4.1" port:333 withTimeout:-1 tag:0];
[IFishHotpotUDPHelper sharedInstance].delegate = self;
[[IFishHotpotUDPHelper sharedInstance] broadCaseUDPWith:name pwd:pwd];
}
- (NSData *)updmessageData:(NSString *)ssidName ssidPwd:(NSString *)ssidPwd {
return [dataContorl stringToHexData:[NSString stringWithFormat:@"%@\0%@\0", ssidPwd, ssidPwd]];
}
#pragma mark - IFishHotpotUDPHelperDelegate
#pragma mark - GCDAsyncUdpSocketDelegate
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag {
[self.view makeToast:@"UDP指令发送成功"];
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error {
[self.view makeToast:[NSString stringWithFormat:@"UDP指令发送失败%@", [error localizedDescription]]];
}
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext {
[self.view makeToast:[NSString stringWithFormat:@"接收到设备返回UDP数据:%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]]];
- (void)udpHelperMessage:(NSString *)msg {
[self.view makeToast:msg];
}
@end

View File

@ -0,0 +1,30 @@
//
// IfishUDPBroadCastHelper.h
// Ifish
//
// Created by imac on 16/9/12.
// Copyright © 2016年 lianxiang. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "AsyncUdpSocket.h"
@protocol IFishHotpotUDPHelperDelegate<NSObject>
- (void)udpHelperMessage:(NSString *)msg;
@end
@interface IFishHotpotUDPHelper : NSObject <AsyncUdpSocketDelegate>
@property (nonatomic, strong) AsyncUdpSocket *clientSocket;
+ (IFishHotpotUDPHelper *)sharedInstance;
@property (nonatomic,strong) NSTimer *loopTimer;
@property (nonatomic, weak) id<IFishHotpotUDPHelperDelegate> delegate;
-(void)broadCaseUDPWith:(NSString *)ssidName pwd:(NSString *)pwd;
@end

View File

@ -0,0 +1,114 @@
//
// IfishUDPBroadCastHelper.m
// Ifish
//
// Created by imac on 16/9/12.
// Copyright © 2016 lianxiang. All rights reserved.
//
#import "IFishHotpotUDPHelper.h"
#import "LxGetCurrentIp.h"
#import "AppDelegate.h"
@interface IFishHotpotUDPHelper ()
{
NSString * _ssidName;
NSString *_pwd;
}
@end
@implementation IFishHotpotUDPHelper
+ (IFishHotpotUDPHelper *)sharedInstance{
static IFishHotpotUDPHelper *sharedInstace = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstace = [[self alloc] init];
});
return sharedInstace;
}
- (void)broadCaseUDPWith:(NSString *)ssidName pwd:(NSString *)pwd {
_ssidName = ssidName;
_pwd = pwd;
self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sendLoopSoketdata) userInfo:nil repeats:YES];
}
-(void)sendLoopSoketdata{
NSString* hostS = @"192.168.4.1";
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
//120.55.190.56 IP
NSString *request=@"0007000000000000000000000000273132302e35352e3139302e353600000000000000bbbb0000";
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
{
NSString* result;
result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"didReceiveData%@",result);
NSLog(@"port%hu",port);
AppDelegate *app =(AppDelegate*)[UIApplication sharedApplication].delegate;
[app.window makeToast:@"切换成功"];
[self.loopTimer invalidate];
return YES;
}
-(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{
[self.loopTimer invalidate];
NSLog(@"关闭");
}
-(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:error.localizedDescription];
[self.loopTimer invalidate];
}
-(void)dealloc{
[self.loopTimer invalidate];
}
- (void)postmsg:(NSString *)msg {
if ([self.delegate respondsToSelector:@selector(udpHelperMessage:)]) {
[self.delegate udpHelperMessage:msg];
}
}
@end