4

我在一个众所周知的(由我)串行外围设备上构建了一个 BLE 外围设备。对于那个串行外围设备,我有一个完整的家庭佣人库,它实现了它的串行协议。该协议遵循规则:发送消息然后立即收到答案。

正确的。我已经使用 CoreBluetooth.framework 重新实现了通过蓝牙进行的通信,并且效果还不错。我的主要问题是等到调用 didUpdateValueForCharacteristic 后再考虑获得答复。(感谢stackoverflow的家伙,我在这里找到了挽救我生命的回复数量)

但是现在当与我的设备交谈时,我必须执行以下操作:

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

}

如果我这样做:

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

未调用 didUpdateValueForCharacteristic。执行命令超时,返回false。THEN didUpdateValueForCharacteristic 毕竟被调用了,里面有很好的数据。

我发现在主线程中调用了 didUpdateValueForCharacteristic:

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

所以如果不调用 [MyPeripheral ExecuteCommand]; 在另一个线程中,我阻止了主线程,并且从不让 didUpdateValueForCharacteristic 被调用。

最后我的问题是:有没有办法让 didUpdateValueForCharacteristic 在后台线程中调用?

谢谢 !

4

0 回答 0