简体   繁体   中英

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.} Error Domain=CBATTErrorDomain Code=3 "Writing is not permitted." UserInfo={NSLocalizedDescription=Writing is not permitted.}

Although same is working android.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM