ios - CoreBluetooth 广告未启动

标签 ios iphone swift bluetooth

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

import UIKit
import CoreBluetooth

class 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/

相关文章:

ios - #error 不支持的架构 - cdefs.h - Swift BLE 项目在 Swift 2.3 中工作正常,但仅在真实设备上的 Swift 3.0 中给出错误

ios - Swift:无法导入桥接头

iphone - 从裁剪图像中删除不需要的区域

iphone - 如何打开 iPhone 的默认浏览器?

Swift:从另一个类调用时 UIPanGestureRecognizer 不起作用

ios - TableView scrollToRow 阻塞了主 ui 线程

ios - TableView 自动重新排序

ios - SWIFT:如果我以编程方式实现 UITAB 栏,是否可以在 storyBoard 中编辑不同的 tabItems

iphone - CGDataProviderCopyData() 实际上复制字节吗?或者只是指针?

ios - 如何将 NSData 对象转换为 UIImage?使用 swift