最近在做公司的一个蓝牙连接打印机的项目,正常的蓝牙连接打印机进行打印没问题,但是要发送一个16进制的指令来获取蓝牙设备信息时颇费了一番功夫,现做如下记录:
Byte byteArray[] = {0x1d ,0x67 ,0x61};
NSData *sendData = [NSData dataWithBytes:byteArray length:sizeof(byteArray)];
[self.peripheral writeValue:sendData forCharacteristic:self.characteristic type:CBCharacteristicWriteWithResponse];
补充:
将十六进制字符串转换成Byte
NSString *hexS = @"0x1D,0x67,0x39";
NSArray *hexSAray = [hexS componentsSeparatedByString:@","];
Byte byteArray[hexSAray.count];
for (int i = 0; i < hexSAray.count; i ++) {
const char *c = [hexSAray[i] UTF8String];
Byte byte = (Byte)strtol(c, NULL, 16);
byteArray[i] = byte;
}