gpt4 book ai didi

IOS BLE通讯写值;

转载 作者:行者123 更新时间:2023-11-29 02:44:22 24 4
gpt4 key购买 nike

我正在编写我的第一个 IOS 应用程序,用于与 Bluegiga BLE 芯片通信。我需要读取和写入值。我遇到的问题是,我无法在第一次运行程序时写入值。我没有收到任何错误。它显示写入当前值的最后一个值。当我重新运行程序时,正确的值已正确更新。它延迟了我的项目。任何建议都会有很大帮助。我正在使用以下代码片段。

CBPeripheral        *servicePeripheral;
NSData *readIndex;
CBCharacteristic *readIdxCharacteristic;

[servicePeripheral writeValue:readIndex forCharacteristic:readIdxCharacteristic type:CBCharacteristicWriteWithResponse];

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

if ([characteristic isEqual:readIdxCharacteristic] ){
[peripheral readValueForCharacteristic:readIdxCharacteristic];
NSLog(@"Read Index Characteristic Written Check%@",characteristic.value);
}
}

最佳答案

您的问题是,您在调用readValueForCharacteristic后立即访问characteristic.value,但Core-Bluetooth需要一些时间才能联系外设,请求特征值和外设回复的值。

如果您引用 documentation for this method你会看到 -

Discussion

When you call this method to read the value of a characteristic, the peripheral calls the peripheral:didUpdateValueForCharacteristic:error: method of its delegate object. If the value of the characteristic is successfully retrieved, you can access it through the characteristic’s value property.

因此,您需要在 didUpdateValueForCharacteristic: CBPeripheral 委托(delegate)方法中访问 characteristic.value

关于IOS BLE通讯写值;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25331458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com