CoreBluetooth在重新连接时不会发现服务 [英] CoreBluetooth doesn't discover services on reconnect

查看:194
本文介绍了CoreBluetooth在重新连接时不会发现服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个与我们自己的BLE设备通信的应用。当我们推送新固件时,我们重新启动,我们得到 centralManager:didDisconnectPeripheral:错误: centralManager:didConnectPeripheral:但是当我们调用 [peripheral discoverServices:nil] 时,我们永远不会得到外围设备的回调:didDiscoverServices:。我们在最初连接时得到它,但在重新连接时却没有。 CoreBluetooth似乎知道服务和特性是什么,因为我们可以与设备进行交互,但我们希望能够在重启后立即编写特性,但无法找到可靠的方法来判断它是什么时候。

We have an app that is communicating with our own BLE device. When we push a new firmware we reboot and we get the centralManager: didDisconnectPeripheral: error: and the centralManager: didConnectPeripheral: but when we call [peripheral discoverServices:nil] we never get the callback for peripheral: didDiscoverServices:. We get it when we initially connect but not when we reconnect. CoreBluetooth seems to know what the services and characteristics are because we can interact with the device but we want to be able to write a characteristic right after the reboot but can't find a reliable way to tell when that is.

- = EDIT = -
这是我们用于编写特征的代码。当我们在第二次调用 didConnect 时尝试调用它时,它失败了,因为外围设备没有任何服务:

-= EDIT =- This is the code we're using for writing characteristics. When we try calling it on the second call to didConnect it fails because the peripheral doesn't have any services:

+(void)writeData:(NSData*)data toPeripheral:(CBPeripheral*)peripheral service:(NSString*)sCBUUID characteristic:(NSString*)cCBUUID
{
    // Sends data to BLE peripheral to process HID and send EHIF command to PC
    for(CBService *service in peripheral.services)
    {
        if([service.UUID.stringValue isEqualToString:sCBUUID])
        {
            for(CBCharacteristic *characteristic in service.characteristics)
            {
                if([characteristic.UUID.stringValue isEqualToString:cCBUUID])
                {
                    /* EVERYTHING IS FOUND, WRITE characteristic ! */
                    [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
                    return;
                }
            }
        }
    }
}


推荐答案

我记得在我使用CoreBluetooth时,这种行为是有意的,因为它是以这种方式记录的。断开连接后,我们不应重用相同的外围设备实例。 相反,我们应该要求CBCentralManager使用其已知的外设UUID为我们提供一个新的CBPeripheral。另请参阅: CBCentralManager.retrievePeripheralsWithIdentifiers:

I remember from my work with CoreBluetooth that this behavior is intended because it's documented this way. We shouldn't reuse the same peripheral instance once disconnected. Instead we should ask CBCentralManager to give us a fresh CBPeripheral using its known peripheral UUID. See also: CBCentralManager.retrievePeripheralsWithIdentifiers:

这篇关于CoreBluetooth在重新连接时不会发现服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆