user6799144
user6799144

Reputation:

Not able to send data to BLE Peripheral (ZL-RC04A) device

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

Answers (1)

Jens Meder
Jens Meder

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:

  1. CBCharacteristicWriteWithResponse: In this case you will get an acknowledge packet by the peripheral. You can think of this as a TCP packet.

  2. 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

Related Questions