简体   繁体   English

iOS BLE通讯写入值;

[英]IOS BLE communication write value;

I am writing my first IOS App, to communicate with the Bluegiga BLE chip. 我正在编写我的第一个IOS应用程序,以便与Bluegiga BLE芯片进行通信。 I need to read and write values. 我需要读取和写入值。 The issue I am having is that, I am not able to write the value the first time I run the program. 我遇到的问题是,我第一次运行程序时无法编写该值。 I do not get any error. 我没有任何错误。 It shows the last value which was written as the current value. 它显示了最后一个被写入为当前值的值。 When I re-run the program the correct value is updated correctly. 当我重新运行程序时,正确的值会正确更新。 Its delaying my project. 它延迟了我的项目。 Any suggestion will be of great help. 任何建议都会有很大帮助。 I am using the following code snippet. 我正在使用以下代码段。

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

Your problem is that you are accessing the characteristic.value immediately after the call to readValueForCharacteristic , but it takes some time for Core-Bluetooth to contact the peripheral, request a value for the characteristic and for the peripheral to reply. 你的问题是,你正在访问characteristic.value通话后立即readValueForCharacteristic ,但需要一段时间的核心蓝牙联系外围,要求值的特性和外设回复。

If you refer to the documentation for this method you will see - 如果您参考此方法文档,将会看到-

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. 当您调用此方法以读取特征值时,外围设备将调用其委托对象的外围设备:didUpdateValueForCharacteristic:error:方法。 If the value of the characteristic is successfully retrieved, you can access it through the characteristic's value property. 如果成功检索到特征的值,则可以通过特征的value属性访问它。

So, you need to access characteristic.value in your didUpdateValueForCharacteristic: CBPeripheral delegate method 所以,你需要访问characteristic.valuedidUpdateValueForCharacteristic: CBPeripheral委托方法

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

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