airkis配置
This commit is contained in:
parent
189897a4f3
commit
6871fe6938
|
|
@ -11,10 +11,18 @@
|
|||
#import "GCDAsyncUdpSocket.h"
|
||||
#import "ESPTouchTaskParameter.h"
|
||||
#import "ESP_NetUtil.h"
|
||||
#include <ifaddrs.h>
|
||||
#import <arpa/inet.h>
|
||||
#include <net/if.h>
|
||||
|
||||
#define kAirKiss_Port 10000
|
||||
#define kAirKiss_Host @"255.255.255.255"
|
||||
#define kAirKiss_Limit_Return_Random_Num 20
|
||||
#define kAirKiss_Limit_Return_Random_Num 3
|
||||
#define IOS_CELLULAR @"pdp_ip0"
|
||||
#define IOS_WIFI @"en0"
|
||||
#define IOS_VPN @"utun0"
|
||||
#define IP_ADDR_IPv4 @"ipv4"
|
||||
#define IP_ADDR_IPv6 @"ipv6"
|
||||
|
||||
@interface JMAirKissConnection()<GCDAsyncUdpSocketDelegate>
|
||||
{
|
||||
|
|
@ -28,6 +36,7 @@
|
|||
int _returnRandomNum;
|
||||
|
||||
BOOL _connectionDone;
|
||||
NSString* _localIP;
|
||||
}
|
||||
@property (nonatomic,strong) ESPTaskParameter *_parameter;
|
||||
@end
|
||||
|
|
@ -74,7 +83,7 @@
|
|||
_tag = 0;
|
||||
_returnRandomNum = 0;
|
||||
_connectionDone = false;
|
||||
|
||||
_localIP = [self.class getIPAddress:YES];
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
_timer = [NSTimer scheduledTimerWithTimeInterval:60
|
||||
|
|
@ -192,14 +201,19 @@ withFilterContext:(id)filterContext
|
|||
if (_connectionDone) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSString* serIp = [GCDAsyncUdpSocket hostFromAddress:address];
|
||||
serIp= [serIp stringByReplacingOccurrencesOfString:@"::ffff:" withString:@""];
|
||||
uint16_t serPort = (int)[GCDAsyncUdpSocket portFromAddress:address];
|
||||
if ([serIp isEqualToString:_localIP]) {
|
||||
return;//是本机的数据直接返回
|
||||
}
|
||||
// 设备连接WIFI成功后会像10000端口发送至少20个UDP广播包所附带的随机数
|
||||
if (data != nil) {
|
||||
UInt8 *bytes = (UInt8 *) [data bytes];
|
||||
if (bytes[0] == _airKissEncoder.randomChar) {
|
||||
_returnRandomNum ++;
|
||||
|
||||
if (_returnRandomNum >= 2) {
|
||||
if (_returnRandomNum >= kAirKiss_Limit_Return_Random_Num) {
|
||||
// 成功
|
||||
[_timer invalidate];
|
||||
_timer = nil;
|
||||
|
|
@ -217,6 +231,64 @@ withFilterContext:(id)filterContext
|
|||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 获取设备IP地址
|
||||
*/
|
||||
+ (NSString *)getIPAddress:(BOOL)preferIPv4
|
||||
{
|
||||
NSArray *searchArray = preferIPv4 ?
|
||||
@[ IOS_VPN @"/" IP_ADDR_IPv4, /*IOS_VPN @"/" IP_ADDR_IPv6, */IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :
|
||||
@[ IOS_VPN @"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ];
|
||||
|
||||
NSDictionary *addresses = [self getIPAddresses];
|
||||
NSLog(@"addresses: %@", addresses);
|
||||
|
||||
__block NSString *address;
|
||||
[searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
|
||||
{
|
||||
address = addresses[key];
|
||||
if(address) *stop = YES;
|
||||
} ];
|
||||
return address ? address : @"0.0.0.0";
|
||||
}
|
||||
+ (NSDictionary *)getIPAddresses
|
||||
{
|
||||
NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
|
||||
// retrieve the current interfaces - returns 0 on success
|
||||
struct ifaddrs *interfaces;
|
||||
if(!getifaddrs(&interfaces)) {
|
||||
// Loop through linked list of interfaces
|
||||
struct ifaddrs *interface;
|
||||
for(interface=interfaces; interface; interface=interface->ifa_next) {
|
||||
if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
|
||||
continue; // deeply nested code harder to read
|
||||
}
|
||||
const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
|
||||
char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
|
||||
if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
|
||||
NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
|
||||
NSString *type;
|
||||
if(addr->sin_family == AF_INET) {
|
||||
if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
|
||||
type = IP_ADDR_IPv4;
|
||||
}
|
||||
} else {
|
||||
const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
|
||||
if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
|
||||
type = IP_ADDR_IPv6;
|
||||
}
|
||||
}
|
||||
if(type) {
|
||||
NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
|
||||
addresses[key] = [NSString stringWithUTF8String:addrBuf];
|
||||
}
|
||||
}
|
||||
}
|
||||
// Free memory
|
||||
freeifaddrs(interfaces);
|
||||
}
|
||||
return [addresses count] ? addresses : nil;
|
||||
}
|
||||
// NSdata进制转十六进制字符串
|
||||
+(NSString *) dataToHexString:(NSData*)data
|
||||
{
|
||||
|
|
|
|||
|
|
@ -178,7 +178,7 @@ extern BOOL isfromCameraView;
|
|||
{
|
||||
|
||||
self.hidesBottomBarWhenPushed = YES;
|
||||
ConAquarMethodVC *methodVC = InitObject(ConAquarMethodVC);
|
||||
ConfigWifiViewController *methodVC = [[ConfigWifiViewController alloc]init];
|
||||
methodVC.deviceType=DEVICEHEATING;
|
||||
methodVC.titleString=@"连接加热器";
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue