iOS 蓝牙指令写入

- (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];

}

你可能感兴趣的:(iOS 蓝牙指令写入)