简体   繁体   English

CBPeripheralManager防止外围设备断开连接

[英]CBPeripheralManager prevent peripherals from disconnecting

I'm using both CBCentralManager and CBPeripheralManager in my app. 我在我的应用程序中同时使用了CBCentralManagerCBPeripheralManager The work I do with PeripheralManager is straightforward - it just add one service with one readable/notify characteristic. 我使用PeripheralManager所做的工作很简单-它仅添加一项具有一种可读/通知特性的服务。 That's it. 而已。

I connect to discovered CBPeripheral by CentralManager and then cancel the connection: centralManager.cancelPeripheralConnection(peripheral) . 我通过CentralManager连接到发现的CBPeripheral,然后取消连接: centralManager.cancelPeripheralConnection(peripheral) I correctly get a delegate callback that peripheral was sucessfully disconnected. 我正确地获得了外围设备已成功断开连接的委托回调。 I then dealloc all strong references to that CBPeripheral . 然后,我CBPeripheral所有对该CBPeripheral强引用。

What is troubling me is that system was keeping a connection to the peripheral anyway. 让我感到困扰的是,系统始终与外围设备保持连接。 I can see connected status in Settings app + my BLE device is indicating that it's still connected. 我可以在“设置”应用中看到connected状态,并且我的BLE设备表明它仍处于连接状态。

What I found out using memory-graph tool is that CBPeripheralManager holds strong reference to some CBPeripheral which I believe is my device. 我使用内存图工具发现, CBPeripheralManager对某些我相信是我的设备的CBPeripheralManager拥有很强的参考力。 When I disabled CBPeripheralManager in my app (didn't add any service to it), I could then cancel connection to the peripheral for real . 当我在应用程序中禁用CBPeripheralManager时(未向其中添加任何服务),然后可以取消与real的外设连接。

Is that a bug or a desired behavior? 这是错误还是期望的行为?

You might have figured this out by now, but Apple keeps the connection alive for a short period of time after cancelling the connection. 您可能已经知道了这一点,但是Apple在取消连接后将连接保留了很短的一段时间。

From their documentation : 从他们的文档中

Because other apps may still have a connection to the peripheral, canceling a local connection does not guarantee that the underlying physical link is immediately disconnected. 由于其他应用可能仍与外围设备保持连接,因此取消本地连接并不能保证基础物理链接立即断开。 From the app's perspective, however, the peripheral is considered disconnected, and the central manager object calls the centralManager:didDisconnectPeripheral:error: method of its delegate object. 但是,从应用程序的角度来看,外围设备被视为已断开连接,中央管理器对象调用了其委托对象的centralManager:didDisconnectPeripheral:error:方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM