0
votes

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

1 Answers

3
votes

You have made one slight error; the value for CBAdvertisementDataServiceUUIDsKey should be an array of CBUUIDs, not a single CBUUID;

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