Slide 15
Slide 15 text
class CentralManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
var manager: CBCentralManager? = nil
func start() {
manager = CBCentralManager(delegate: self, queue: nil)
}
// MARK: - CBCentralManagerDelegate
func centralManagerDidUpdateState(_ central: CBCentralManager) {
guard central.state == .poweredOn else { return }
central.scanForPeripherals(withServices: [smartLockServiceUUID])
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI:
NSNumber) {
central.stopScan()
central.connect(peripheral)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.delegate = self
peripheral.discoverServices([smartLockServiceUUID])
self.connectedPeripheral = peripheral
}
// MARK: - CBPeripheralDelegate
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
guard let service = peripheral.services?.first(where: { $0.uuid == smartLockServiceUUID }) else { return }
peripheral.discoverCharacteristics([smartLockCharacteristicUUID], for: service)
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristic = service.characteristics?.first(where: { $0.uuid == smartLockCharacteristicUUID }) else { return }
peripheral.setNotifyValue(true, for: characteristic)
}
}
Πϕϯτຖʹϝιου͕ผΕ͓ͯΓɺ
ॲཧͷྲྀΕ͕͍ͮΒ͍