Reputation:
I am trying to write data using code snippet.
- (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];
}
}
}
But getting error in
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error
{
}
As:
Error Domain=CBATTErrorDomain Code=3 "Writing is not permitted." UserInfo={NSLocalizedDescription=Writing is not permitted.}
Although same is working android.
Upvotes: 0
Views: 543
Reputation: 4367
You get that error if you use the wrong write type for the characteristic (given the characteristic is writable after all). There are two types of writing data to a characteristic:
CBCharacteristicWriteWithResponse
: In this case you will get an acknowledge packet by the peripheral. You can think of this as a TCP packet.
CBCharacteristicWriteWithoutResponse
: This is a "fire and forget" kind of write. You can think of this as a UDP packet.
Therefore, try to use CBCharacteristicWriteWithoutResponse
instead of CBCharacteristicWriteWithResponse
. If that does not work as well you might have to check if your characteristic is writable after all.
Upvotes: 1