我正在尝试使用代码段来编写数据。
- (void)peripheral:(CBPeripheral *)peripheral1 didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
// Again, we loop through the array, just in case.
for (CBCharacteristic *characteristic in service.characteristics) {
// And check if it's the right one
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
NSString *payloadMessage = @"3N";
NSData *payload = [payloadMessage dataUsingEncoding:NSUTF8StringEncoding];
[_discoveredPeripheral discoverDescriptorsForCharacteristic:characteristic];
[_discoveredPeripheral writeValue:payload forCharacteristic:characteristic
type:CBCharacteristicWriteWithResponse];
[_discoveredPeripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
}
但在
中收到错误 - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error
{
}
如:
的 Error Domain=CBATTErrorDomain Code=3 "Writing is not permitted." UserInfo={NSLocalizedDescription=Writing is not permitted.}
虽然同样适用于android。
答案 0 :(得分:1)
如果对特征使用了错误的写入类型(假设特性是可写的),则会出现该错误。将特征写入特征有两种类型:
CBCharacteristicWriteWithResponse
:在这种情况下,您将获得外围设备的确认包。您可以将其视为TCP数据包。
CBCharacteristicWriteWithoutResponse
:这是一种“一劳永逸”的写作。您可以将其视为UDP数据包。
因此,请尝试使用CBCharacteristicWriteWithoutResponse
代替CBCharacteristicWriteWithResponse
。如果这不起作用,你可能必须检查你的特征是否可写。