我有一个在后台运行的应用程序。我使用 CBPeripheralManager 来做广告和 CBCentralManager 来扫描。我使用两个 ıos(IOS 11.3 和 IOS 13.4.1)设备。第一个是广告前景和背景。第二个是扫描前景和背景。我可以扫描;
后台应用程序,手机已解锁 - 完美运行
后台应用程序,手机被锁定,屏幕被点亮 - 完美运行
后台应用程序,手机锁定,屏幕关闭 - 不起作用!
/* 我检查了在 Android 设备中运行后台显示的广告应用 */
问题是什么。请告诉我。如何解决这个问题?我想在后台扫描两者。我的代码如下;
let scanOptions = [
CBCentralManagerScanOptionAllowDuplicatesKey: NSNumber(value: true)
]
let services = [CBUUID(string: "e2c56db5-dffb-48d2-b060-d0f5a71096e0")]
let advertisingData = [
CBAdvertisementDataLocalNameKey: "xxx",
CBAdvertisementDataServiceUUIDsKey:[CBUUID(string: "e2c56db5-dffb-48d2-b060-d0f5a71096e0")]
] as [String : Any]
func initLocal() {
peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
cbCentralManager = CBCentralManager(delegate: self, queue: nil,options: nil)
}
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
if peripheral.state == .poweredOn {
peripheralManager.startAdvertising(advertisingData)
}
else if peripheral.state == .poweredOff {
peripheralManager.stopAdvertising()
}
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn{
central.scanForPeripherals(withServices: services,options:scanOptions)
print("scanning...")
}
else {
print("Bluetooth is not active")
}
}
func centralManager(_ central: CBCentralManager,didDiscover peripheral: CBPeripheral,advertisementData: [String : Any],
rssi RSSI: NSNumber)
{
print("RSSI : \(RSSI)")
}