Search code examples
iosobjective-cbluetooth-lowenergycore-bluetoothibeacon

Issue in writing CBCharacteristic


I am currently working on an BLE app which scans my company beacons , i need to update the values for CBCharacteristic .

I am not getting any error after my write function , but the change does not reflect on BLE device.

I have scanned all CBCharacteristics and then traversing to get particular and calling write function

for (CBCharacteristic* characteristic in beaconCharacteristics)
{
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF5"]]) {
        [characteristic.service.peripheral writeValue:[@"BeaconE" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
    }
}

even i get completion delegate method called after writing without an error

-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
}

Am i missing anything ? Is it due to hex value ?

Please guide me

Thank you !


Solution

  • It could be that the Characteristic is read only. You can find out by looking at the CBCharacteristicProperties object, which you can get to using characteristic.properties

    //Check if the Characteristic is writable
    if ((characteristic.properties & CBCharacteristicPropertyWrite) ||
        (characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse))
    {
        //Do your Write here
    }
    

    Below is a simple method that takes a CBCharacteristicProperties object and logs the properties. . .helps with debugging.

    -(void)logCharacteristicProperties:(CBCharacteristicProperties)properties {
    
        if (properties & CBCharacteristicPropertyBroadcast) {
            NSLog(@"CBCharacteristicPropertyBroadcast");
        }
        if (properties & CBCharacteristicPropertyRead) {
            NSLog(@"CBCharacteristicPropertyRead");
        }
        if (properties & CBCharacteristicPropertyWriteWithoutResponse) {
            NSLog(@"CBCharacteristicPropertyWriteWithoutResponse");
        }
        if (properties & CBCharacteristicPropertyWrite) {
            NSLog(@"CBCharacteristicPropertyWrite");
        }
        if (properties & CBCharacteristicPropertyNotify) {
            NSLog(@"CBCharacteristicPropertyNotify");
        }
        if (properties & CBCharacteristicPropertyIndicate) {
            NSLog(@"CBCharacteristicPropertyIndicate");
        }
        if (properties & CBCharacteristicPropertyAuthenticatedSignedWrites) {
            NSLog(@"CBCharacteristicPropertyAuthenticatedSignedWrites");
        }
        if (properties & CBCharacteristicPropertyExtendedProperties) {
            NSLog(@"CBCharacteristicPropertyExtendedProperties");
        }
        if (properties & CBCharacteristicPropertyNotifyEncryptionRequired) {
            NSLog(@"CBCharacteristicPropertyNotifyEncryptionRequired");
        }
        if (properties & CBCharacteristicPropertyIndicateEncryptionRequired) {
            NSLog(@"CBCharacteristicPropertyIndicateEncryptionRequired");
        }
    }
    

    Also, you should be checking for Error to see if CoreBluetooth is throwing an error.

    //CBPeripheral Delegate
    -(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
    
        if (error) {
            NSLog(@"<error> didWriteValueForCharacteristic %@",[error description]);
            //
            // Add Error handling for failed Writes
            //
        }
    
        //Add Handling for successful writes
    
    }