iOS ble 写入不改变值

iOS ble write not change value

通过iOS (writeValue: forCharacteristic: type :) 方法发送数据工作正常没有错误,但值没有改变。你知道为什么吗?

> 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];
}

当我尝试写入时,只打印了相同的日志。

尝试:

[self.discoveredPeripheral writeValue: valData
           forCharacteristic:self.usedCharacteristic
                        type:CBCharacteristicWriteWithoutResponse];

使用类型:CBCharacteristicWriteWithoutResponse

同时检查您是否设置了通知程序

  [self.discoveredPeripheral setNotifyValue:YES forCharacteristic:self.usedCharacteristic];

在递交代表之前。

在您的 iOS 中央,didWriteValueForCharacteristic 中的特征 value 并未反映您刚刚编写的更改。

您需要发出显式读取以获取新值。

这是设计使然,因为无法保证您刚刚写入的值就是特征的当前值;只有外围设备可以为您提供当前值。