首页 > 解决方案 > 如何将 MacBook 变成具有 BLE 扫描服务的外围设备?

问题描述

我正在尝试将我的 MacBook 用作具有蓝牙扫描服务的外围设备,因此可以像这样使用它:

 var manager:CBCentralManager? 

  manager?.scanForPeripherals(withServices: nil, options: nil)//the withServices will be replaced with custom service UUID

我尝试创建自己的自定义服务 UUID 并将其添加到 peripheralManager 中,如下所示:

var peripheralManager: CBPeripheralManager?

   override func viewDidLoad() {
        super.viewDidLoad()
        createServiceUUID()
         peripheralManager?.startAdvertising(["Test" : "Test"])
    }


    func createServiceUUID() {
        let customServiceUUID = CBUUID(string: customServiceUUIDString)
        let mutableService = CBMutableService(type: customServiceUUID, primary: true)
        peripheralManager?.add(mutableService)
    }

然后我尝试扫描我刚刚创建的 serviceUUID,但它没有显示:

 manager?.scanForPeripherals(withServices: [CBUUID(string: customServiceUUIDString)], options: nil)

我觉得我可能错过了将我的 Mac 变成可以使用

scanForPeripherals(withServices:)

功能。关于如何实现这一点的任何想法?

标签: iosswiftbluetooth-lowenergyuuidcore-bluetooth

解决方案


推荐阅读