我需要您的帮助才能将值写入BLE设备。
我已经检查了很多像这样的话题: BLE writeValue to peripheral
我知道如何写一个值,我必须使用该代码
NSData *data = [NSData alloc];
data = [@"0s000000000000000000" dataUsingEncoding:NSUTF8StringEncoding];
[peripheral writeValue:data
forCharacteristic:characteristic
type:CBCharacteristicWriteWithResponse];
这种格式@“ 0s000000000000000000”对我来说可以
我的问题是,我需要编写以下命令:
x 0x50 0x00 0x1B 0x64 0x10 x 0x00 0x01 0x3C 0x02 0x10 0x00 x x 0x01
我不明白该怎么写。 你能帮我吗?
编辑:2019-05-14
@Larme:
示例:这段代码好吗?
uint8_t theData[] = { 0x0, 0x50, 0x00, 0x1B, 0x64, 0x10, 0x0, 0x00, 0x01, 0x3C, 0x02, 0x10, 0x00, 0x0, 0x0, 0x01 };
NSData *data = [NSData dataWithBytes:&theData length:sizeof(theData)];
[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
编辑:2019-05-15
我找到了这个主题: https://www.codeproject.com/Questions/884078/I-need-to-send-a-write-command-to-Bluetooth-low-en
那代码好吗?
NSMutableData *concatenate = [NSMutableData data];
uint8_t d_a = 0x0;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_a length:sizeof(d_a)]];
uint8_t d_b = 0x50;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_b length:sizeof(d_b)]];
uint8_t d_c = 0x00;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_c length:sizeof(d_c)]];
uint8_t d_d = 0x1B;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_d length:sizeof(d_d)]];
uint8_t d_e = 0x64;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_e length:sizeof(d_e)]];
uint8_t d_f = 0x10;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_f length:sizeof(d_f)]];
uint8_t d_g = 0x0;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_g length:sizeof(d_g)]];
uint8_t d_h = 0x00;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_h length:sizeof(d_h)]];
uint8_t d_i = 0x01;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_i length:sizeof(d_i)]];
uint8_t d_j = 0x3C;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_j length:sizeof(d_j)]];
uint8_t d_k = 0x02;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_k length:sizeof(d_k)]];
uint8_t d_l = 0x10;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_l length:sizeof(d_l)]];
uint8_t d_m = 0x00;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_m length:sizeof(d_m)]];
uint8_t d_n = 0x0;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_n length:sizeof(d_n)]];
uint8_t d_o = 0x0;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_o length:sizeof(d_o)]];
uint8_t d_p = 0x01;
[concatenate appendData:[NSData dataWithBytes:(void*)&d_p length:sizeof(d_p)]];
[peripheral writeValue:concatenate forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
在Swift上,我认为(我不确定)代码是:
let bytes : [UInt8] = [ 0x0, 0x50, 0x00, 0x1B, 0x64, 0x10, 0x0, 0x00, 0x01, 0x3C, 0x02, 0x10, 0x00, 0x0, 0x0, 0x01 ]
let data = NSData(bytes: bytes, length: bytes.count)
来源: Sending Hex String using WriteValue in Swift
我不知道这是否很好(+我需要目标C代码)