CoreBluetooth peripheral on Mac not advertising service

50 views Asked by At

My macOS application is setting up a Bluetooth service using CoreBluetooth. The service and characteristic is added correctly, except the service UUID is not part of the advertisement data. I have tried running this code on iOS and then the service UUID shows up in the advertisement data.

It seems as if any data I pass on the line self.peripheralManager?.startAdvertising([CBAdvertisementDataServiceUUIDsKey: [serviceUUID]]) is ignored when running under macOS but I can't find any information about this in documentation. Are service UUIDs supposed to be part of advertisement data under macOS?

class BluetoothController: NSObject, CBPeripheralManagerDelegate {
    private var peripheralManager: CBPeripheralManager?
    private var cyclingPowerService: CBMutableService?
    private var cyclingPowerCharacteristic: CBMutableCharacteristic?

    override init() {
        super.init()

        self.peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
    }

    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
        if (peripheral.state == .poweredOn) {
            // UUIDs for the service and characteristic
            let serviceUUID = CBUUID(string: "1818")
            let characteristicUUID = CBUUID(string: "2A63")
            
            // Create a characteristic
            self.cyclingPowerCharacteristic = CBMutableCharacteristic(type: characteristicUUID, properties: .read, value: nil, permissions: .readable)
            
            // Create a service
            self.cyclingPowerService = CBMutableService(type: serviceUUID, primary: true)
            
            // Add the characteristic to the service
            self.cyclingPowerService?.characteristics = [self.cyclingPowerCharacteristic!]
            
            // Add the service to the peripheral manager
            self.peripheralManager?.add(self.cyclingPowerService!)
            
            // Start advertising
            self.peripheralManager?.startAdvertising([CBAdvertisementDataServiceUUIDsKey: [serviceUUID]])
        }
        else {
            self.peripheralManager?.remove(self.cyclingPowerService!)
        }
    }
0

There are 0 answers