添加udp发送功能

This commit is contained in:
xuemh 2018-07-25 21:42:39 +08:00
parent 54f7d72850
commit f760907af3
2 changed files with 72 additions and 3 deletions

View File

@ -8,13 +8,19 @@
#import "ConnectHotpotViewController.h"
#import "ConnectHotspotTipViewController.h"
#import "dataContorl.h"
#import<SystemConfiguration/CaptiveNetwork.h>
#import "GCDAsyncUdpSocket.h"
@interface ConnectHotpotViewController ()<UITextFieldDelegate>
@interface ConnectHotpotViewController ()<UITextFieldDelegate, GCDAsyncUdpSocketDelegate>
@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
@ -80,5 +86,64 @@
[textField resignFirstResponder];
return YES;
}
- (IBAction)setLinkBtnClicked:(id)sender {
if (self.nameLbl.text.length == 0) {
[self.view makeToast:@"请输入正确的路由器名称"];
return;
}
BOOL isRight =[dataContorl isIncludeSpecialCharact:self.pwdTf.text];
if (!isRight) {
[self.view makeToast:@"路由器密码不能包含特殊字符,只能为数字字母下划线"];
return;
}
if (![[self currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) {
[self.view makeToast:@"请先将手机连接到wifi:ifish-xxxx"];
return;
}
NSString *ssidName = self.nameLbl.text;
NSString *ssidPwd = self.pwdTf.text;
[self sendToDeviceWithSSIDName:ssidName andSSIDPWD:ssidPwd];
}
- (NSString *)currentWifiSSID {
NSString *ssid = @"Not Found";
CFArrayRef myArray = CNCopySupportedInterfaces();
if (myArray != nil) {
CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
if (myDict != nil) {
NSDictionary *dict = (NSDictionary *)CFBridgingRelease(myDict);
ssid = [dict valueForKey:@"SSID"];
}
}
return ssid;
}
#pragma mark - UDP related
- (void)sendToDeviceWithSSIDName:(NSString *)name andSSIDPWD:(NSString *)pwd {
self.socket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[self.socket sendData:[self updmessageData:name ssidPwd:pwd] toHost:@"192.168.4.1" port:333 withTimeout:-1 tag:0];
}
- (NSData *)updmessageData:(NSString *)ssidName ssidPwd:(NSString *)ssidPwd {
return [dataContorl stringToHexData:[NSString stringWithFormat:@"%@\0%@\0", ssidPwd, ssidPwd]];
}
#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]]];
}
@end

View File

@ -12,6 +12,7 @@
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="ConnectHotpotViewController">
<connections>
<outlet property="containerView" destination="1Km-pH-09L" id="XAO-g5-CDw"/>
<outlet property="nameLbl" destination="APB-5y-OkX" id="mJ6-4x-fap"/>
<outlet property="pwdTf" destination="kiH-bp-24Q" id="PIU-eW-NJW"/>
<outlet property="scrollView" destination="rVr-h5-uWd" id="xVI-ZH-ZBs"/>
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
@ -62,8 +63,8 @@
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dDg-6q-BXw">
<rect key="frame" x="193" y="78.5" width="36" height="17"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="ifish-xxxx" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dDg-6q-BXw">
<rect key="frame" x="193" y="78.5" width="62" height="17"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
@ -167,6 +168,9 @@
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="boolean" keyPath="layer.masksToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
<connections>
<action selector="setLinkBtnClicked:" destination="-1" eventType="touchUpInside" id="cOx-dO-PUg"/>
</connections>
</button>
</subviews>
<constraints>