AccessPoint 配置上网
通过app发送wifi账号密码给硬件设备,让硬件设备连接上网.
.h文件
#import@interface AccessPoint : NSObject
@property(nonatomic ,copy)void(^Conneted)(BOOL isConneted);
+(AccessPoint*)shareInstance;
- (void)initConnectedAccessPointWithWebWifiID:(NSString *)webWifiID andWebWifiPassword:(NSString *)webWifiPassword andApiIP:(NSString *)apiIP andApiProt:(int)apiProt;
@end
.m文件
#import "AccessPoint.h"@interface AccessPoint(){
NSInputStream *inputStream;
NSOutputStream *outputStream;
NSString *_webWifiID;
NSString *_WifiPassword;
NSString *_apiIP;
int _apiProt;
NSInteger _sentCount;
}
@end
@implementation AccessPoint
+(AccessPoint*)shareInstance
{
static AccessPoint *instance = nil;
static dispatch_once_t oneceToken;
dispatch_once(&oneceToken, ^{
instance = [[AccessPoint alloc]init];
});
return instance;
}
- (void)initConnectedAccessPointWithWebWifiID:(NSString *)webWifiID andWebWifiPassword:(NSString *)webWifiPassword andApiIP:(NSString *)apiIP andApiProt:(int)apiProt
{
_sentCount = 0;
_webWifiID = webWifiID;
_WifiPassword = webWifiPassword;
_apiIP = apiIP;
_apiProt = apiProt;
[self initNetworkCommunication];
}
//192.168.4.1 5566 为硬件特定的ip 端口
-(void)initNetworkCommunication {
uint portNo = 5566;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)@"192.168.4.1", portNo, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
uint8_t buffer[1024];
NSInteger len;
switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(@"成功连接建立,形成输入输出流的传输通道");
break;
case NSStreamEventHasBytesAvailable:
NSLog(@"有数据可读");
if (theStream == inputStream)
{
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0)
{
if (_Conneted) {
_Conneted(YES);
}
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output) {
NSLog(@"服务器发回来: %@", output);
}
}
}
}
else
{
if (_Conneted) {
_Conneted(NO);
}
NSLog(@"it is NOT theStream == inputStream");
}
break;
case NSStreamEventHasSpaceAvailable:{
NSLog(@"可以发送数据");
_sentCount++;
if (_sentCount==1) {
[self MacRegister:_webWifiID and:_WifiPassword and:_apiIP and:_apiProt];
}
}
break;
case NSStreamEventErrorOccurred:
NSLog(@"有错误发生,连接失败");
_sentCount = 0;
break;
case NSStreamEventEndEncountered:
NSLog(@"正常的断开连接");
_sentCount = 0;
[inputStream close];
[outputStream close];
[inputStream removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
break;
default:
NSLog(@"Unknown event %lu", (unsigned long)streamEvent);
}
}
//发送特定指令到硬件设备
-(void)MacRegister:(NSString *) webWifiID and:(NSString *)webWifiPassword and:(NSString *) apiIP and:(int)apiProt
{
NSInteger len = [self jsLength:webWifiID]+ [ self jsLength: webWifiPassword]+ [self jsLength: apiIP]+7;
int index = 0 ;
Byte cmd[len + 8];
NSLog(@"%@",[self convertStringToHexStr:@"$"]);
cmd[index++] = strtoul([[self convertStringToHexStr:@"$"] UTF8String], 0, 16);
NSLog(@"$:%x",cmd[0]);
cmd[index++] = strtoul([[self convertStringToHexStr:@"@"] UTF8String], 0, 16);
NSLog(@"@:%x",cmd[1]);
cmd[index++] = strtoul([[self convertStringToHexStr:@"i"] UTF8String], 0, 16);
NSLog(@"i:%x",cmd[2]);
cmd[index++] = strtoul([[self convertStringToHexStr:@"y"] UTF8String], 0, 16); NSLog(@"y:%x",cmd[3]);
//数据包长度
cmd[index++] = strtoul([[self ToHex:len] UTF8String], 0, 16);
NSLog(@"数据包长度:%x",cmd[4]);
cmd[index++] = 0x00;
NSLog(@"这是:%x",cmd[5]);
cmd[index++] = 0x00;
NSLog(@"这是:%x",cmd[6]);
//ip
cmd[index++] = strtoul([[self ToHex:[self jsLength:apiIP]] UTF8String], 0, 16);
NSLog(@"apiIP长度:%x",cmd[7]);
NSString *ai = nil;
for(int i =0; i < [apiIP length]; i++)
{
ai = [apiIP substringWithRange:NSMakeRange(i, 1)];
if ([self isChinese:ai]) {
NSData *myD = [ai dataUsingEncoding:NSUTF8StringEncoding];
Byte *bytes = (Byte *)[myD bytes];
for(int i=0;i<[myD length];i++)
{
cmd[index++] = bytes[i]&0xff;
}
}
else{
cmd[index++] = strtoul([[self convertStringToHexStr:ai] UTF8String], 0, 16);
NSLog(@"apiIP:%x",cmd[index-1]);
}
}
NSString *oooo = @"0000";
NSString *str = [self ToHex:apiProt];
NSString *result = [NSString stringWithFormat:@"%@%@",oooo,str];
NSString *port = [result substringFromIndex:[result length]-4];
NSString *prot1 = [port substringWithRange:NSMakeRange(0, 2)];//端口
NSString *prot2 = [port substringFromIndex:2];//端口
cmd[index++] = strtoul([prot1 UTF8String], 0, 16);
NSLog(@"端口1:%x",cmd[index-1]);
cmd[index++] = strtoul([prot2 UTF8String], 0, 16);
NSLog(@"端口2:%x",cmd[index-1]);
cmd[index++] = strtoul([[self ToHex:[self jsLength: webWifiID]] UTF8String], 0, 16);
NSLog(@"SSID长度:%x",cmd[index-1]);
NSString *wifi = nil;
for(int i =0; i < [webWifiID length]; i++)
{
wifi = [webWifiID substringWithRange:NSMakeRange(i, 1)];
if ([self isChinese:wifi]) {
NSData *myD = [wifi dataUsingEncoding:NSUTF8StringEncoding];
Byte *bytes = (Byte *)[myD bytes];
for(int i=0;i<[myD length];i++)
{
cmd[index++] = bytes[i]&0xff;
}
}
else{
cmd[index++] = strtoul([[self convertStringToHexStr:wifi] UTF8String], 0, 16);
NSLog(@"webWifiID:%x",cmd[index-1]);
}
}
cmd[index++] =strtoul([[self ToHex:[self jsLength: webWifiPassword]] UTF8String], 0, 16); //WIFI,密码长度
NSLog(@"wifi长度:%x",cmd[index-1]);
NSString *pass = nil;
for(int i =0; i < [webWifiPassword length]; i++)
{
pass = [webWifiPassword substringWithRange:NSMakeRange(i, 1)];
if ([self isChinese:pass]) {
NSData *myD = [pass dataUsingEncoding:NSUTF8StringEncoding];
Byte *bytes = (Byte *)[myD bytes];
for(int i=0;i<[myD length];i++)
{
cmd[index++] = bytes[i]&0xff;
}
}
else{
cmd[index++] = strtoul([[self convertStringToHexStr:pass] UTF8String], 0, 16);
NSLog(@"webWifiPassword:%x",cmd[index-1]);
}
}
int a = 0;
a = cmd[2];
for (int i = 3; i < index; i++)
{
a = a ^cmd[i] ;
}
cmd[index++] = (Byte)a;
NSLog(@"异或校验2:%x",cmd[index-1]);
cmd[index++] = 0x0d;
NSLog(@"0x0d:%x",cmd[index-1]);
cmd[index++] = 0x0a;
NSLog(@"0x0a:%x",cmd[index-1]);
NSString *hexStr = @"";
for (int i = 0; i < index ; i++)
{
NSString *newHexStr = [NSString stringWithFormat:@"%x",cmd[i]];///16进制数
if([newHexStr length]==1)
{
hexStr = [NSString stringWithFormat:@"%@0%@ ",hexStr,newHexStr];
}
else{
hexStr = [NSString stringWithFormat:@"%@%@ ",hexStr,newHexStr];
}
}
NSLog(@"指令:%@",hexStr);
[outputStream write:cmd maxLength:sizeof(cmd)];
}
#pragma mark---判断中文
- (BOOL)isChinese:(NSString *)str
{
NSString *match = @"(^[\u4e00-\u9fa5]+$)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
return [predicate evaluateWithObject:str];
}
#pragma mark---将NSString转换成十六进制的字符串则可使用如下方式:
-(NSString *)convertStringToHexStr:(NSString *)str {
if (!str || [str length] == 0) {
return @"";
}
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
[data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
unsigned char *dataBytes = (unsigned char*)bytes;
for (NSInteger i = 0; i < byteRange.length; i++) {
NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
if ([hexStr length] == 2) {
[string appendString:hexStr];
} else {
[string appendFormat:@"0%@", hexStr];
}
}
}];
return string;
}
#pragma mark - 十进制转成十六进制
-(NSString *)ToHex:(long long int)tmpid
{
NSString *nLetterValue;
NSString *str =@"";
long long int ttmpig;
for (int i = 0; i<9; i++) {
ttmpig=tmpid%16;
tmpid=tmpid/16;
switch (ttmpig)
{
case 10:
nLetterValue =@"A";break;
case 11:
nLetterValue =@"B";break;
case 12:
nLetterValue =@"C";break;
case 13:
nLetterValue =@"D";break;
case 14:
nLetterValue =@"E";break;
case 15:
nLetterValue =@"F";break;
default:nLetterValue=[[NSString alloc]initWithFormat:@"%lli",ttmpig];
}
str = [nLetterValue stringByAppendingString:str];
if (tmpid == 0) {
break;
}
}
return str;
}
#pragma mark--- 计算utf8
-(NSInteger)jsLength:(NSString *)str
{
NSData *myD = [str dataUsingEncoding:NSUTF8StringEncoding];
return myD.length;
}
网友评论