我正在尝试使用 CoreBluetooth 写入特定的已知特征。我觉得这应该是可能的,因为我使用了德州仪器 BLE 实用程序,您可以在连接的外围设备上选择“写入值”操作,只需输入特征 UUID 和您要写入的值,它执行没有问题。
据我了解,为了做到这一点,我必须打电话给
[peripheral writeValue:data forCharacteristic:...];
对象配置为具有CBCharacteristic
正确的 UUID。
我尝试CBMutableCharacteristic
使用正确的 UUID,甚至对我所知道的是此外围设备配置文件中的现有特征的正确权限,尝试在外围设备上执行具有该特征的任何读/写操作时出现以下崩溃:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CBMutableCharacteristic peripheral]: unrecognized selector sent to instance 0x1ed57690'
这是我用来设置和写入特征的代码:
NSString *characteristicUUIDstring = @"fff1";
CBUUID *characteristicUUID = [CBUUID UUIDWithString:characteristicUUIDstring];
char dataByte = 0x10;
NSData *data = [NSData dataWithBytes:&dataByte length:1];
CBMutableCharacteristic *testCharacteristic = [[CBMutableCharacteristic alloc] initWithType:characteristicUUID properties:CBCharacteristicPropertyRead|CBCharacteristicPropertyWrite value:data permissions:CBAttributePermissionsReadable|CBAttributePermissionsWriteable];
[peripheral writeValue:data forCharacteristic:testCharacteristic type:CBCharacteristicWriteWithResponse];