CBPeripheral didUpdateValueForCharacteristic, maid synchrone

I have a BLE peripheral over a known (for me) serial peripheral. For this serial peripheral, I have a whole house maid lib that implements its serial protocol. The protocol follows the rule: send a message and immediately receive a response.

Right I restarted the connection via Bluetooth using CoreBluetooth.framework, and this does not work so bad. My main problem was to wait until the didUpdateValueForharacteristic function was called before considering the answer. (Thank you guys stackoverflow, I found here the answer number that saved my life)

But now, when I talk to my device, I should do something like:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { dispatch_async(m_ProcessingQueue, ^{ bool ok = [MyPeripheral ExcecuteCommand]; }); } 

if i do this:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { bool ok = [MyPeripheral ExcecuteCommand]; } 

didUpdateValueForCharacteristic is not called. Run the time delayed command and return false. THEN didUpdateValueForCharacteristic is called in the end, with good data in it.

I refused that didUpdateValueForCharacteristic was called in the main thread:

 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { NSLog(@"didUpdateValueForCharacteristic %@", [NSThread isMainThread] ?@ "is main thread":@"is NOT main thread"); // wich reply true ! } 

So, if you do not call [MyPeripheral ExcecuteCommand]; in another thread, I block the main thread and never allow the .UpdateValueForCharacteristic function to be called.

Finally, my question is: Is there a way to make didUpdateValueForCharacteristic called in the background thread?

Thanks!

+4
source share

Source: https://habr.com/ru/post/1494515/


All Articles