WiFi通訊是指手機經過WiFi與內部設備樹立銜接,並與內部設備停止交互、通訊。手機與內部設備的WiFi通訊通常是運用Socket來完成的,在這裡先引見一個第三方Socket庫(CocoaAsyncSocket)來完成WiFi通訊。
CocoaAsyncSocket支持TCP和UDP,其中:
AsyncSocket類是支持TCP的;
AsyncUdpSocket類是支持UDP的。
本文是樹立在硬件經過UDP播送包播送本身信息,手機與硬件之間經過TCP銜接傳輸數據。
WiFi銜接的樹立首先,經過手動銜接手機WiFi至內部設備,此時可以獲取到內部WiFi的一些信息:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
typedef void (^udpSocketBlock)(NSDictionary* dic,NSError* err);// block用於硬件前往信息的回調
@property (nonatomic,copy) udpSocketBlock udpSocketBlock;
- (void)sendUdpBoardcast:(udpSocketBlock)block;
@end
#import "ViewController.h"
#import <AsyncSocket.h>
#import <AsyncUdpSocket.h>
@interface ViewController ()<AsyncSocketDelegate,AsyncUdpSocketDelegate>
@property (nonatomic,strong) AsyncUdpSocket *udpSocket;
@property (nonatomic,strong) AsyncSocket *socket;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)sendUdpBoardcast:(udpSocketBlock)block{
self.udpSocketBlock = block;
if(!_udpSocket)_udpSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
NSData *data = [NSData data];// 此處data是依據硬件要求傳參數
UInt16 port = 34343;// 此處詳細指需訊問硬件工程師
[self.udpSocket enableBroadcast:YES error:NULL];
[_udpSocket sendData:data toHost:@"255.255.255.255" port:port withTimeout:-1 tag:0];// 由於不知道詳細的ip地址,所以host采用受限播送地址
}
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withtag:(long)tag fromHost:(NSString *)host port:(UInt16)port{
// data 接納到的內部設備前往的數據
id result = [self unpackageMessage:data]; // 對數據停止處置,此處調用的 - (id)unpackageMessage:(NSData *)data ;是依據與硬件方面協商的數據格式停止的數據處置
if ([[result valueForJSONKey:@"typeid"] isEqualToString:@"xxxx"]) {
self.udpSocketBlock([result valueForJSONKey:@"data"],nil);
} // 判別的到的數據能否為我們需求的數據
return YES; // 發現設備後,則封閉發現通道
return NO; // 不封閉發現通道,不斷處於發現形態
}
#pragma mark - udpSocket
-(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithtag:(long)tag{
}
經過調用該辦法,可以失掉內部設備返還的WiFi信息:
[self sendUdpBoardcast:^(NSDictionary *dic, NSError *err) {
// dic為硬件前往的參數
}];
獲取硬件參數之後,需求確認手機能否已於硬件銜接,直接調用辦法
- (BOOL)isConnected;
若未銜接,則需樹立手機和硬件之間的socket銜接:
- (BOOL)connectToHost:(NSString*)hostname onPort:(UInt16)port error:(NSError **)errPtr;
// hostname、port均為硬件前往的
數據的寫入和讀取
CocoaAsyncSocket提供了寫入數據和讀取數據的辦法:
// 數據的寫入
- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
// 數據的讀取
- (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag;
數據寫入詳細格式需求依據硬件要求來決議,這裡提供幾種常用的數據類型轉換辦法以供參考:
十六進制字符串轉NSData-(NSData *)converHexStrToData:(NSString *)hexString {
NSMutableData *data = [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
if (hexString.length%2) {
//避免喪失半個byte
hexString = [@"0" stringByAppendingString:hexString];
}
int i;
for (i = 0; i < [hexString length]/2; i++) {
byte_chars[0] = [hexString characterAtIndex:i * 2];
byte_chars[1] = [hexString characterAtIndex:i * 2 + 1];
whole_byte = strtol(byte_chars, NULL, 16);
[data appendBytes:&whole_byte length:1];
}
return data;
}
NSData轉十六進制字符串
-(NSString *) converDataToHexString:(NSData *)data
{
if (data == nil) {
return nil;
}
NSMutableString* hexString = [NSMutableString string];
const unsigned char *p = [data bytes];
for (int i=0; i < [data length]; i++) {
[hexString appendFormat:@"%02x", *p++];
}
return hexString;
}
十六進制字符串轉普通字符串
-(NSString *)stringFromHexString:(NSString *)hexString {
char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);
bzero(myBuffer, [hexString length] / 2 + 1);
for (int i = 0; i < [hexString length] - 1; i += 2) {
unsigned int anInt;
NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
NSScanner * scanner = [[NSScanner alloc] initWithString:hexCharStr];
[scanner scanHexInt:&anInt];
myBuffer[i / 2] = (char)anInt;
}
NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
return unicodeString;
}
【iOS中的WiFi與硬件通訊】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!