简体   繁体   中英

BLE Peripheral Not Advertising

I am attempting to create a simple Bluetooth LE peripheral to connect to a pre-existing 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. 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. 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 ;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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