Ios 如果在iPhone的蓝牙设置中建立了连接,如何获得通知?
我有一台iPhone,它正试图通过我的应用程序连接到外设,但由于某种原因(不是问题),连接在一分钟后中断。我的应用程序不会尝试自动重新连接到此外设,但在iPhone设置中,我可以看到外设仍显示为已连接。 我从CoreBluetooth框架获得断开连接回调,但没有后续的didConnect回调,因为应用程序没有尝试再次连接 当系统自动连接到此外设时,我是否可以在我的应用程序中知道?。或者我可以订阅任何系统通知,以便了解连接状态何时更改Ios 如果在iPhone的蓝牙设置中建立了连接,如何获得通知?,ios,swift,bluetooth-lowenergy,core-bluetooth,Ios,Swift,Bluetooth Lowenergy,Core Bluetooth,我有一台iPhone,它正试图通过我的应用程序连接到外设,但由于某种原因(不是问题),连接在一分钟后中断。我的应用程序不会尝试自动重新连接到此外设,但在iPhone设置中,我可以看到外设仍显示为已连接。 我从CoreBluetooth框架获得断开连接回调,但没有后续的didConnect回调,因为应用程序没有尝试再次连接 当系统自动连接到此外设时,我是否可以在我的应用程序中知道?。或者我可以订阅任何系统通知,以便了解连接状态何时更改 提前感谢您的帮助。CoreBluetooth framewor
提前感谢您的帮助。CoreBluetooth framework为您提供了一个代理方法,用于了解蓝牙连接的状态
import CoreBluetooth
class ViewController: CBPeripheralManagerDelegate {
var bluetoothPeripheralManager: CBPeripheralManager?
override func viewDidLoad() {
super.viewDidLoad()
let options = [CBCentralManagerOptionShowPowerAlertKey:0]
bluetoothPeripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: options)
}
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) {
var statusMessage = ""
switch peripheral.state {
case CBPeripheralManagerState.PoweredOn:
statusMessage = "Bluetooth Status: Turned On"
case CBPeripheralManagerState.PoweredOff:
statusMessage = "Bluetooth Status: Turned Off"
case CBPeripheralManagerState.Resetting:
statusMessage = "Bluetooth Status: Resetting"
case CBPeripheralManagerState.Unauthorized:
statusMessage = "Bluetooth Status: Not Authorized"
case CBPeripheralManagerState.Unsupported:
statusMessage = "Bluetooth Status: Not Supported"
default:
statusMessage = "Bluetooth Status: Unknown"
}
print(statusMessage)
}
}
此
外围设备管理器RDIDUPDATESTATE
方法提供蓝牙的状态。希望有帮助。获取状态消息并根据您的需要进行操作。尝试以下操作:-如果外围设备断开连接,则您的应用程序有责任发出新的连接呼叫(如果您需要)want@Paulw11考虑一下这个场景。我通过我的应用程序连接到一个设备,如果设备和手机相距很远,则它们会断开连接,当它们回到范围内时,它们会再次连接。如何通知我的应用程序设备已返回范围并发送连接呼叫?在后台搜索设备会消耗很多电池,不是吗?你不必这样做;只要在外围设备断开连接时发出一个connect
,该连接将挂起。一旦设备回到范围内,连接将完成,您的应用程序将收到通知。如果您没有采用核心蓝牙后台模式,则只有当您的应用程序回到前台且设备在范围内时,连接才会完成。因此,这基本上是一个自动重新连接?如果操作系统连接到设备而不是通过我的应用程序,这会被触发吗?仅通过应用程序