gpt4 book ai didi

ios - CoreBluetooth 广告未启动

转载 作者:行者123 更新时间:2023-11-30 13:50:37 27 4
gpt4 key购买 nike

我正在尝试编写一个简单的应用程序,即使应用程序未处于事件状态,它也会不断广播“信标”。我知道,当应用程序未使用时,使用 CoreLocation 会关闭此功能,因此我尝试使用 Core Bluetooth 构建一个解决方案。问题是我无法让应用程序开始转换广告。

import UIKitimport CoreBluetoothclass ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralManagerDelegate {    var centralManager:CBCentralManager = CBCentralManager()    var peripheralManager:CBPeripheralManager = CBPeripheralManager()    let uuid:CBUUID = CBUUID(string: "DCEF54A2-31EB-467F-AF8E-350FB641C97B")    override func viewDidLoad() {        super.viewDidLoad()        self.peripheralManager = CBPeripheralManager(delegate: self, queue: nil)        self.centralManager.delegate = self        let advertisingData = [CBAdvertisementDataLocalNameKey:"my-peripheral", CBAdvertisementDataServiceUUIDsKey: uuid]        peripheralManager.startAdvertising(advertisingData)        centralManager.scanForPeripheralsWithServices([uuid], options: nil)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }    func peripheralManagerDidStartAdvertising(peripheral: CBPeripheralManager, error: NSError?) {        print("started advertising")        print(peripheral)    }    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {        print("peripheral discovered")        print("peripheral: \(peripheral)")        print("RSSI: \(RSSI)")    }    func centralManagerDidUpdateState(central: CBCentralManager) {        print("central state updated")        print(central.description)        if central.state == .PoweredOff {            print("bluetooth is off")        }        if central.state == .PoweredOn {            print("bluetooth is on")            let advertisingData = [CBAdvertisementDataLocalNameKey:"my-peripheral", CBAdvertisementDataServiceUUIDsKey: uuid]            let service = CBMutableService(type: uuid, primary: true)            self.peripheralManager.addService(service)            peripheralManager.startAdvertising(advertisingData)            centralManager.scanForPeripheralsWithServices(nil, options: nil)        }        if central.state == .Unsupported {            print("bluetooth is unsupported on this device")        }    }    func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) {        print("peripheral state updated")        print("\(peripheral.description)")    }}

我已在两台设备上安装了此程序,问题似乎出在广告传输中,因为从未调用 peripheralManagerDidStartAdvertising()

最佳答案

此问题与 .addService() 调用后立即调用 .startAdvertising() 有关。

您必须仅在调用 funcperipheralManager(_peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) 委托(delegate)方法之后调用 .startAdvertising()

关于ios - CoreBluetooth 广告未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34334996/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com