Created
January 24, 2021 19:18
-
-
Save Kdan/f7b72e275e679d4a7cf8bcadd0a5ab72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension BluetoothManager: CentralManagerDelegate { | |
func centralManagerDidUpdateState(_ central: CentralManager) { | |
// Make sure the central manager is powered on. | |
guard central.state == .poweredOn else { return } | |
// We only scan for the listed UUIDs. | |
central.scanForPeripherals(withServices: uuids, options: nil) | |
} | |
func centralManager(_ central: CentralManager, | |
didDiscover peripheral: Peripheral, | |
advertisementData: [String : Any], | |
rssi RSSI: NSNumber) { | |
// Only connect to peripherals with names and manufacturer data. | |
guard let name = peripheral.name ?? advertisementData[CBAdvertisementDataLocalNameKey] as? String, | |
let data = advertisementData[CBAdvertisementDataManufacturerDataKey] as? Data, | |
name != "FORBIDDEN-NAME", | |
!data.isEmpty else { return } | |
central.connect(peripheral, options: nil) | |
} | |
// MARK: - CBCentralManagerDelegate intermediate functions. | |
func centralManagerDidUpdateState(_ central: CBCentralManager) { | |
centralManagerDidUpdateState(central as CentralManager) | |
} | |
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { | |
centralManager(central as CentralManager, | |
didDiscover: peripheral as Peripheral, | |
advertisementData: advertisementData, | |
rssi: RSSI) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment