Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当应用程序处于后台时,如何在广告数据更改时获取通知?_Ios_Swift_Bluetooth Lowenergy_Cbperipheral_Cbperipheralmanager - Fatal编程技术网

Ios 当应用程序处于后台时,如何在广告数据更改时获取通知?

Ios 当应用程序处于后台时,如何在广告数据更改时获取通知?,ios,swift,bluetooth-lowenergy,cbperipheral,cbperipheralmanager,Ios,Swift,Bluetooth Lowenergy,Cbperipheral,Cbperipheralmanager,我想在BLE设备发生故障时得到通知。 如果BLE设备将某些数据/命令传递给应用程序,则应用程序内的advertisementData不会更改。 但同样的事情,我们可以做的安卓系统,它的工作非常完美。 我想实现一些功能,比如当advertisementData发生更改时,我想得到通知。 请帮助我实现这一点。 下面是我的AppDelegate.swift类 private var centralManager : CBCentralManager! func application(_ appli

我想在BLE设备发生故障时得到通知。 如果BLE设备将某些数据/命令传递给应用程序,则应用程序内的advertisementData不会更改。 但同样的事情,我们可以做的安卓系统,它的工作非常完美。 我想实现一些功能,比如当advertisementData发生更改时,我想得到通知。 请帮助我实现这一点。 下面是我的AppDelegate.swift类

private var centralManager : CBCentralManager!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    centralManager = CBCentralManager(delegate: self, queue: nil, options: nil)

    return true
}

func applicationWillEnterForeground(_ application: UIApplication) {

    print("entering foreground...")
}

func applicationDidEnterBackground(_ application: UIApplication) {
    print("entered background...")

    centralManager = CBCentralManager(delegate: self, queue: nil, options: nil)
}


func centralManagerDidUpdateState(_ central: CBCentralManager) {
    if central.state == .poweredOn {
        print("Bluetooth is On")

        let kTrackStandardDeviceInfoServiceUUID  = CBUUID(string: "180A")

        let dictionaryOfOptions = [CBCentralManagerScanOptionAllowDuplicatesKey : true]
        let arrayOfServices: [CBUUID] = [kTrackStandardDeviceInfoServiceUUID]

        centralManager.scanForPeripherals(withServices: arrayOfServices, options: dictionaryOfOptions)
    } else {
        print(central.state)
    }
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

    print("\nName   : \(peripheral.name ?? "(No name)")")
    print("RSSI   : \(RSSI)")

    let name = peripheral.name ?? ""
    if name.contains("ETGuardian") {

        let DetailData = advertisementData["kCBAdvDataManufacturerData"]
        let DiscoveredData = String(describing: DetailData)

        print(DiscoveredData)

        for ad in advertisementData {
            print("AD Data: \(ad)")
        }
    }
}

为了让应用程序可以在后台工作,您需要在应用程序中实现一些后台服务

通常,后台服务是位置获取。 请注意,后台服务将使您的应用程序更快地耗尽电池电量

要实现后台服务,请单击项目、签名和功能、后台模式、启用BLE功能