'Swift 3 - CoreBluetooth - Background - array parameter

I have the following setup to run bluetooth scans in background, singleton:

class BluetoothService: NSObject {
    
    var manager: CBCentralManager!
    var characteristic: CBCharacteristic!
    
    var peripheral: CBPeripheral!
    let peripheral_name: String = "mygadget"
    let service_uuid_array: [CBUUID] = [CBUUID(string: "85948G74-245Z-4861-G54R-198F368E380M")]
        
    static let sharedInstance = BluetoothService()
    private override init() {
        super.init()
        manager = CBCentralManager(delegate: self, queue: nil)
    }
    
}

extension BluetoothService: CBCentralManagerDelegate {
    
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if (central.state == CBManagerState.poweredOn) {
            self.manager?.scanForPeripherals(withServices: service_uuid_array, options: nil)
        }
    }
    
    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        if (peripheral.name == self.peripheral_name) {
            self.manager.stopScan()
            self.peripheral = peripheral
            self.peripheral.delegate = self
            self.manager.connect(peripheral, options: nil)
        }
    }
    
    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        peripheral.discoverServices(nil)
    }
    
    func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
        if (central.state == CBManagerState.poweredOn) {
            self.manager?.scanForPeripherals(withServices: service_uuid_array, options: nil)
        }
    }
    
    func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
    }
    
}

extension BluetoothService: CBPeripheralDelegate {
    
    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
    }
    
    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    }
    
    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
    }
    
    func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
    }
    
}

I also activated Background Modes and activated "Uses Bluetooth LE accessories"

When I use

self.manager?.scanForPeripherals(withServices: nil, options: nil)

instead of

self.manager?.scanForPeripherals(withServices: service_uuid_array, options: nil)

It works fine in foreground, but i can not get it working when I pass this array... why?

I need to use "service_uuid_array" in order to get it working in background.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source