swift 中的后台扫描 BLE
Background scan BLE in swift
我正在尝试在后台连接 BLE,但它没有在后台连接。
当我的应用程序处于前台时它正在工作。
我正在尝试使用外围设备的 UUID 进行扫描。
这是附加的代码。
override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
var msg = ""
switch central.state {
case .poweredOff:
msg = "Bluetooth is Off"
case .poweredOn:
msg = "Bluetooth is On"
let arrayOfServices: [CBUUID] = [CBUUID(string: "CCAExxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
manager?.scanForPeripherals(withServices:arrayOfServices, options: nil)
case .unsupported:
msg = "Not Supported"
default:
msg = "Not Connected"
}
print("STATE: " + msg)
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("Name: \(peripheral.name)") //print the names of all peripherals connected.
//you are going to use the name here down here ⇩
if peripheral.name == "Name of device" { //if is it my peripheral, then connect
self.myBluetoothPeripheral = peripheral //save peripheral
self.myBluetoothPeripheral.delegate = self
manager.stopScan() //stop scanning for peripherals
manager.connect(myBluetoothPeripheral, options: nil) //connect to my peripheral
}
}
如何解决?
您需要做的是,当您实例化 CentralManager 时,您需要使用恢复标识符对其进行实例化。
例如:
CBCentralManager(delegate: self,options:
[CBCentralManagerOptionRestoreIdentifierKey: "bleCentralManager"])
这是必要的,因为在苹果文档中它说 "Core Bluetooth preserves the state of only those objects that have a restoration identifier"。
然后,当您的应用程序重新启动到后台时,您必须在 appDelegate 的 application:didFinishLaunchingWithOptions 中使用相同的恢复标识符重新实例化适当的中央管理器:method.You 可以获得这样的恢复标识符:
let centralManagerIdentifiers = launchOptions![UIApplicationLaunchOptionsKey.bluetoothCentrals]
最后,在您的中央管理器 centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) 委托方法中,您可以获得中央管理器已连接或正在尝试连接的所有外围设备的列表,以及在这个方法中做任何想做的事。
func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {
let peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey]
}
开启'Background Modes'能力。
并开启以下两个选项:
1. 用户蓝牙 LE 配件。
2. 作为蓝牙 LE 配件。
与屏幕截图相同:
现在 BLE 在 iOS 应用程序中以后台模式工作。
我正在尝试在后台连接 BLE,但它没有在后台连接。 当我的应用程序处于前台时它正在工作。 我正在尝试使用外围设备的 UUID 进行扫描。 这是附加的代码。
override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
var msg = ""
switch central.state {
case .poweredOff:
msg = "Bluetooth is Off"
case .poweredOn:
msg = "Bluetooth is On"
let arrayOfServices: [CBUUID] = [CBUUID(string: "CCAExxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
manager?.scanForPeripherals(withServices:arrayOfServices, options: nil)
case .unsupported:
msg = "Not Supported"
default:
msg = "Not Connected"
}
print("STATE: " + msg)
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("Name: \(peripheral.name)") //print the names of all peripherals connected.
//you are going to use the name here down here ⇩
if peripheral.name == "Name of device" { //if is it my peripheral, then connect
self.myBluetoothPeripheral = peripheral //save peripheral
self.myBluetoothPeripheral.delegate = self
manager.stopScan() //stop scanning for peripherals
manager.connect(myBluetoothPeripheral, options: nil) //connect to my peripheral
}
}
如何解决?
您需要做的是,当您实例化 CentralManager 时,您需要使用恢复标识符对其进行实例化。
例如:
CBCentralManager(delegate: self,options:
[CBCentralManagerOptionRestoreIdentifierKey: "bleCentralManager"])
这是必要的,因为在苹果文档中它说 "Core Bluetooth preserves the state of only those objects that have a restoration identifier"。
然后,当您的应用程序重新启动到后台时,您必须在 appDelegate 的 application:didFinishLaunchingWithOptions 中使用相同的恢复标识符重新实例化适当的中央管理器:method.You 可以获得这样的恢复标识符:
let centralManagerIdentifiers = launchOptions![UIApplicationLaunchOptionsKey.bluetoothCentrals]
最后,在您的中央管理器 centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) 委托方法中,您可以获得中央管理器已连接或正在尝试连接的所有外围设备的列表,以及在这个方法中做任何想做的事。
func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {
let peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey]
}
开启'Background Modes'能力。
并开启以下两个选项: 1. 用户蓝牙 LE 配件。 2. 作为蓝牙 LE 配件。
与屏幕截图相同:
现在 BLE 在 iOS 应用程序中以后台模式工作。