Reputation: 35
I am writing my first IOS App, to communicate with the Bluegiga BLE chip. 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);
}
}
Upvotes: 0
Views: 1857
Reputation: 115002
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.
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. If the value of the characteristic is successfully retrieved, you can access it through the characteristic’s value property.
So, you need to access characteristic.value
in your didUpdateValueForCharacteristic:
CBPeripheral
delegate method
Upvotes: 1