Search code examples
iosobjective-cbluetoothbluetooth-lowenergycore-bluetooth

iOS ble write not change value


Sending data through the iOS (writeValue: forCharacteristic: type :) method works fine without error, but the value does not change. Do you know why?

> 2017-06-27 14:53:54.846963+0900 BluetoothSample[12662:5477661] Did
> write  characteristic <CBCharacteristic: 0x1700ad500, UUID =
> 2A588021-4FB2-40F5- 8204-85315DEF11C5, properties = 0xA, value =
> <1357>, notifying = NO>


uint8_t val = 123;
NSData * valData = [NSData dataWithBytes:(void*)&val length:sizeof(val)];


 [self.discoveredPeripheral writeValue:valData forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];


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

    if (error) {
         NSLog(@"didWriteValueForCharacteristic With error: %@", [error localizedDescription]);

        self.receivePaketDataTextView.text = [[NSString alloc] initWithFormat:@"%@",[error localizedDescription]];
        return ;
    }

    self.receivePaketDataTextView.text = [[NSString alloc] initWithFormat:@"%@",characteristic.value];
}

When I try to write, only the same logs are printed.


Solution

  • On your iOS central the value of the characteristic in didWriteValueForCharacteristic does not reflect the change you just wrote.

    You need to issue an explicit read to get the new value.

    This is by design, since there is no guarantee that the value you have just written is the current value of the characteristic; only the peripheral can give you the current value.