iOS WiFi与硬件通讯

WiFi通讯是指手机经过WiFi与外部设备创建链接,并与外部设备进行交互、通讯。手机与外部设备的WiFi通讯一般是使用Socket来实现的,在这里先介绍一个第三方Socket库(CocoaAsyncSocket)来实现WiFi通讯。app

CocoaAsyncSocket支持TCP和UDP,其中:AsyncSocket类是支持TCP的;AsyncUdpSocket类是支持UDP的。本文是创建在硬件经过UDP广播包广播自身信息,手机与硬件之间经过TCP链接传输数据。socket

WiFi链接的创建

首先,经过手动链接手机WiFi至外部设备,此时能够获取到外部WiFi的一些信息:atom

#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信息:spa

[self sendUdpBoardcast:^(NSDictionary *dic, NSError *err) {
     // dic为硬件返回的参数
}];

获取硬件参数以后,须要确认手机是否已于硬件链接,直接调用方法code

- (BOOL)isConnected;

若未链接,则需创建手机和硬件之间的socket链接:orm

- (BOOL)connectToHost:(NSString*)hostname onPort:(UInt16)port error:(NSError **)errPtr;
// hostname、port均为硬件返回的

数据的写入和读取

CocoaAsyncSocket提供了写入数据和读取数据的方法:ip

// 数据的写入
- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
// 数据的读取
- (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag;

数据写入具体格式须要根据硬件要求来决定,这里提供几种经常使用的数据类型转换方法以供参考:unicode

  • 十六进制字符串转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;
    }
相关文章
相关标签/搜索