我自己得到了解决方案
首先是将数据转换为字节的函数
- (NSData *)dataWithString:(NSString *)hexstring
{
NSMutableData* data = [NSMutableData data];
int idx;
for (idx = 0; idx+2 <= hexstring.length; idx+=2) {
NSRange range = NSMakeRange(idx, 2);
NSString* hexStr = [hexstring substringWithRange:range];
NSScanner* scanner = [NSScanner scannerWithString:hexStr];
unsigned int intValue;
[scanner scanHexInt:&intValue];
[data appendBytes:&intValue length:1];
}
return data;
}
并通过在将发送到 BLE 的命令中提供参数来调用该函数
NSData * data = [self dataWithString: @"401A"];
[peripheral writeValue:data forCharacteristic:characteristic
type:CBCharacteristicWriteWithResponse];
通过此代码,任何人都可以将字符串发送到 BLE 设备。谢谢和干杯