In the Bluetooth project, a single chip microcomputer needs to call OC’s sending and receiving method, sending is no problem, but when reading the data returned by Bluetooth, the data has not been received. How can I make him wait for the data to be received before running?
This is a method of reading data broadcast by Bluetooth
- (void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
bracket
if (error)
bracket
NSLog (@ "ErrorReceiving Notification for Characteristic Percent @: Percent @", characteristic, error);
return;
bracket
Else
bracket
[self.lock lock];
NSLog(@"\n Ready to View Received Data ");
//Accepted Data
NSData*receiveData= [NSData dataWithData:characteristic.value];
//Save the received data to an array
[self.array addObject:receiveData];
//For those stored in array inside
[self.lock unlock];
bracket
bracket
/**
* Read data
*/
-(void)receiveData
bracket
NSLog(@ "View data received");
//Take out the saved data from the array inside and put it into the attribute inside
ble.readData = [self.array firstObject];
NSString * str = [self hexStringFromData:ble.readData];
[self.array removeObjectAtIndex:0];
NSLog(@"\n~~~~~~~~~~~~~\n% of data received @ \ n ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ \ n ",STR);
bracket
//This is a function called by the lower computer.
int readData(unsigned char* buf, int maxlen)
bracket
//here ble.readData is not worth it yet. I want him to execute the return or something below after he is worth it. please advise how to do it.
buf = (unsigned char *)[ble.readData bytes];
int len = [ble.readData length];
return len;
bracket
Modify and add succeeded block: (void () (intlen)) getdatablock
- (void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic successedBlock:(void (^)(int len))getDataBlock error:(NSError *)error bracket if (error) bracket NSLog (@ "ErrorReceiving Notification for Characteristic Percent @: Percent @", characteristic, error); return; bracket Else bracket [self.lock lock]; NSLog(@"\n Ready to View Received Data "); //Accepted Data NSData*receiveData= [NSData dataWithData:characteristic.value]; //Process Data int length = ....... if(getDataBlock) { getDataBlock(length); bracket //For those stored in array inside [self.lock unlock]; bracket bracket