简体   繁体   English

iOS无法写入不更改值

[英]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. 通过iOS发送数据(writeValue:forCharacteristic:type :)方法可以正常工作,不会出现错误,但该值不会更改。 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. 当我尝试写时,仅打印相同的日志。

Try: 尝试:

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

Use type: CBCharacteristicWriteWithoutResponse 使用类型:CBCharacteristicWriteWithoutResponse

Also check if you have set the notifier 还要检查是否设置了通知程序

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

Before hand in your delegates. 在派代表前。

On your iOS central the value of the characteristic in didWriteValueForCharacteristic does not reflect the change you just wrote. 在iOS中心上, didWriteValueForCharacteristic中特征的value不反映您刚刚编写的更改。

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. 只有外设可以给您当前值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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