繁体   English   中英

BLE外围非广告

[英]BLE Peripheral Not Advertising

我正在尝试创建一个简单的Bluetooth LE外围设备以连接到预先存在的Central。 据我所知,我的外围设备管理器已正确设置,并且使用正确的服务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 S,不是一个单一的CBUUID ;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM