ios - BLE外设不做广告

标签 ios swift bluetooth-lowenergy core-bluetooth

我正在尝试创建一个简单的蓝牙 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")
}

最佳答案

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

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

关于ios - BLE外设不做广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54584990/

相关文章:

ios - 在 XCode 9 中启动模拟器(不是 headless 的)

ios - NSFileManager.defaultManager().fileExistsAtPath 返回 false 而不是 true

iOS 通过 Gatt 忽略 HID 消费者页面的输入报告

ubuntu - Hcitool 白名单命令不起作用

ios - AVSim库捕获图像保存

iphone - 在我的 iPhone 应用程序中读取 "Settings"应用程序数据

swift - 闭包中的弱引用

swift - Swift Generic Collection 的紧凑化 where constraint

ios - 将 subview 固定到 stackview 的前缘和后缘

c# - 是否可以使 GattCharacteristic ValueChange 事件发生在多个设备上?