2017

iOS 蓝牙指令写入

2017-02-07  本文已影响42人  彦子凡
- (void)writeData:(NSString *)string{

NSMutableData *hexdata = [[NSMutableData alloc] initWithCapacity:8];

NSRange ranger;

if ([string length] %2 == 0) {

ranger = NSMakeRange(0,2);

} else {

ranger = NSMakeRange(0,1);

}

for (NSInteger i = ranger.location; i < [string length]; i += 2) {

unsigned int anint;

NSString *hexStr = [string substringWithRange:ranger];

NSScanner *scan = [[NSScanner alloc] initWithString:hexStr];

[scan scanHexInt:&anint];

NSData *enty = [[NSData alloc]initWithBytes:&anint length:1];

[hexdata appendData:enty];

ranger.location += ranger.length;

ranger.length = 2;

}

//此处为写入数据的方法

[self.peripheral writeValue:hexdata forCharacteristic:self.controCharacteristic type:CBCharacteristicWriteWithResponse];

}
上一篇下一篇

猜你喜欢

热点阅读