简体   繁体   English

iOS BLE的断开时间

[英]Disconnect time for iOS BLE

I've noticed that when you disconnect for a bluetooth device in an application the iOS device will continue to hold that connection for around 10 seconds. 我注意到,当您在应用程序中为蓝牙设备断开连接时,iOS设备将继续保持该连接大约10秒钟。 I've attempted to get around this by writing to a characteristic that causes the bluetooth module to cancel the connection with the iOS device instead but that isn't working (mostly because I changed the module and the iOS doesn't see the change because I assume the device is cached somewhere). 我试图通过写一个特性来解决这个问题,该特性会导致蓝牙模块改为取消与iOS设备的连接,但是那不起作用(主要是因为我更改了模块,而iOS看不到更改,因为我认为设备缓存在某处)。 Is there a way to make it disconnect instantly in code? 有没有办法使它在代码中立即断开连接? I am using the swift command manager.cancelPeripheralConnection(peripheral) currently. 我目前正在使用swift命令manager.cancelPeripheralConnection(peripheral)

Unfortunately, there isn't. 不幸的是,没有。 The only way to tell the system to disconnect a peripheral is via the cancelPeripheralConnection method that you are already using. 告诉系统断开外围设备连接的cancelPeripheralConnection方法是通过您已经使用的cancelPeripheralConnection方法。 Yet, if you call this method it doesn't necessarily mean that the peripheral will be disconnect. 但是,如果调用此方法,则不一定意味着外围设备将断开连接。

Background 背景

On iOS the whole BLE connection management is maintained by the operating system. 在iOS上,整个BLE连接管理由操作系统维护。 That means that no single application "owns" a connection. 这意味着没有单个应用程序“拥有”连接。 All BLE functionality is multiplexed to allow more than one application to gain access to centrals and peripherals. 所有BLE功能都经过复用,以允许多个应用程序访问中央和外围设备。

For example, if you have installed a fitness application that tracks data from your heart rate sensor in the background then you can also "connect" to the heart rate sensor in your app but you will be unable to trigger a real disconnect as long as the fitness app maintains a connection. 例如,如果您安装了一个健身应用程序,该应用程序在后台跟踪来自您的心率传感器的数据,那么您也可以“连接”到该应用程序中的心率传感器,但是只要您的设备无法触发真正的断开连接,健身应用程序保持连接。

The disconnect delay you have noticed is basically an optimization of the operating system. 您注意到的断开连接延迟基本上是对操作系统的优化。 If no application holds a connection to the peripheral anymore it will wait for some time (to avoid unnecessary connect/disconnect cycles) and then trigger the disconnect on the bluetooth chip. 如果没有应用程序再保持与外围设备的连接,它将等待一段时间(以避免不必要的连接/断开循环),然后触发蓝牙芯片上的断开连接。

Hope that helps. 希望能有所帮助。

In iOS when you call the CBCentralManager method cancelPeripheralConnection(CBPeripheral), it does not always immediately terminate the connection. 在iOS中,当您调用CBCentralManager方法cancelPeripheralConnection(CBPeripheral)时,它并不总是立即终止连接。 As Apple's documentation states: 如Apple的文档所述:

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:方法。

If you have a need to immediately terminate a connection programmatically, say to free up the peripheral to be connected from another central device or to reset one's own security layer, then you need to follow the procedure described in the following StackOverflow thread: 如果您需要立即以编程方式终止连接,比如说要释放要与另一台中央设备连接的外围设备或重置自己的安全层,那么您需要遵循以下StackOverflow线程中所述的过程:

iOS 6 - Bluetooth LE disconnect iOS 6-蓝牙LE断开连接

which is for the app to send your own proprietary command to the peripheral that tells the peripheral to disconnect through normal means (eg "GAPRole_TerminateConnection" or "GAP_TerminateLinkReq" or "LL_Disconnect" or "HCI_Disconnect[_*]" with reason HCI_DISCONNECT_REMOTE_USER_TERM). 这是为了让应用程序将自己的专有命令发送到外围设备,该命令告诉外围设备通过常规方式断开连接(例如,“ H.H.H._H.C。”),例如“ GAPRole_TerminateConnection”或“ GAP_TerminateLinkReq”或“ LL_Disconnect”或“ HCI_Disconnect [_ *]”。 This always works and is not delayed by the connection supervision timeout because it is a formal disconnection notifying the central device (ie iOS). 这始终有效,并且不会因连接监管超时而延迟,因为它是通知中央设备(即iOS)的正式断开连接。 The supervision timeout (up to 6 seconds in iOS; on Android the default is 20 seconds) only comes into play if the disconnection is unplanned as with going out of range or if the peripheral does a disconnect without notifying the remote device as with "LL_EXT_DisconnectImmed" (only available in some BLE peripheral implementations). 监督超时(在iOS中最长为6秒;在Android上默认为20秒)仅在计划外断开连接(如超出范围)或外围设备断开连接而不通知远程设备(如“ LL_EXT_DisconnectImmed”)时起作用”(仅在某些BLE外围设备实现中可用)。

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

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