【问题标题】:BLE Peripheral Not AdvertisingBLE 外设无广告
【发布时间】:2019-02-08 01:45:10
【问题描述】:

我正在尝试创建一个简单的蓝牙 LE 外围设备来连接到预先存在的 Central。据我所知,我的 Peripheral Manager 设置正确,并且使用正确的服务 UUID 调用开始广告。但是,当我运行检查以确保我的外围设备确实在做广告时,我得到了否定结果

我已经仔细检查了我的 UUID,并确保我的测试设备上的蓝牙已打开。我也在使用物理设备进行测试,而不是模拟器。

这是用于设置我的外围广告的代码:

func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
    if peripheral.state == CBManagerState.poweredOn {
        service = CBMutableService.init(type: SERVICE_UUID, primary: true)

        characteristic = CBMutableCharacteristic.init(type: CHARACTERISTIC_UUID, properties: .read, value: nil, permissions: .readable)
        service?.characteristics = [characteristic!]

        peripheralManager?.add(service!)
        peripheralManager?.delegate = self

        let adData = [CBAdvertisementDataLocalNameKey : "MackJohn_Bluetooth_On_iOS", CBAdvertisementDataServiceUUIDsKey : SERVICE_UUID] as [String : Any]

        peripheralManager?.startAdvertising(adData)
    }
}

我在这里检查我的代码是否真的在做广告并得到错误的结果:

func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
    print(peripheral.isAdvertising)
}

我还注意到这个函数根本没有调用:

func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {
    print("Added Service")
}

【问题讨论】:

    标签: ios swift bluetooth-lowenergy core-bluetooth


    【解决方案1】:

    你犯了一个小错误; CBAdvertisementDataServiceUUIDsKey 的值应该是CBUUIDs 的数组,而不是单个CBUUID;

    let adData = [CBAdvertisementDataLocalNameKey : "MackJohn_Bluetooth_On_iOS", CBAdvertisementDataServiceUUIDsKey : [SERVICE_UUID]] as [String : Any]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-12
      相关资源
      最近更新 更多