简体   繁体   English

BLE外围非广告

[英]BLE Peripheral Not Advertising

I am attempting to create a simple Bluetooth LE peripheral to connect to a pre-existing Central. 我正在尝试创建一个简单的Bluetooth LE外围设备以连接到预先存在的Central。 From what I can tell, my Peripheral Manager is set up correctly and the call to begin advertising is in place with the correct service UUID. 据我所知,我的外围设备管理器已正确设置,并且使用正确的服务UUID进行了开始广告的调用。 However, when I run a check to make sure my peripheral is actually advertising, I get a negative result 但是,当我进行检查以确保外围设备确实在做广告时,得到的结果是负面的

I have double checked my UUID and ensured that Bluetooth on my test device is on. 我已经仔细检查了我的UUID,并确保测试设备上的蓝牙已打开。 I am also using a physical device for testing, not the simulator. 我也在使用物理设备进行测试,而不是模拟器。

Here is the code used to set up my peripheral advertisement: 这是用于设置我的外围广告的代码:

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)
    }
}

Here is where I'm checking to see if my code is actually advertising and getting a false result: 这是我要检查我的代码是否真正在广告并得到错误结果的地方:

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

I've also noticed that this function is not calling at all: 我还注意到该函数根本没有调用:

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

You have made one slight error; 您犯了一个小错误; the value for CBAdvertisementDataServiceUUIDsKey should be an array of CBUUID s, not a single CBUUID ; 对于值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