1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- import CoreBluetooth
- final class PeripheralDelegate: NSObject, CBPeripheralDelegate {
- typealias ServicesDiscoveryHandler = (CBPeripheral, Error?) -> Void
- typealias CharacteristicsDiscoverHandler = (CBService, Error?) -> Void
- typealias CharacteristicNotificationStateUpdateHandler = (CBCharacteristic, Error?) -> Void
- typealias CharacteristicValueUpdateHandler = (CBCharacteristic, Error?) -> Void
- typealias CharacteristicValueWriteHandler = (CBCharacteristic, Error?) -> Void
- private let onServicesDiscovery: ServicesDiscoveryHandler
- private let onCharacteristicsDiscovery: CharacteristicsDiscoverHandler
- private let onCharacteristicNotificationStateUpdate: CharacteristicNotificationStateUpdateHandler
- private let onCharacteristicValueUpdate: CharacteristicValueUpdateHandler
- private let onCharacteristicValueWrite: CharacteristicValueWriteHandler
- init(
- onServicesDiscovery: @escaping ServicesDiscoveryHandler,
- onCharacteristicsDiscovery: @escaping CharacteristicsDiscoverHandler,
- onCharacteristicNotificationStateUpdate: @escaping CharacteristicNotificationStateUpdateHandler,
- onCharacteristicValueUpdate: @escaping CharacteristicValueUpdateHandler,
- onCharacteristicValueWrite: @escaping CharacteristicValueWriteHandler
- ) {
- self.onServicesDiscovery = onServicesDiscovery
- self.onCharacteristicsDiscovery = onCharacteristicsDiscovery
- self.onCharacteristicNotificationStateUpdate = onCharacteristicNotificationStateUpdate
- self.onCharacteristicValueUpdate = onCharacteristicValueUpdate
- self.onCharacteristicValueWrite = onCharacteristicValueWrite
- }
- func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
- onServicesDiscovery(peripheral, error)
- }
- func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
- onCharacteristicsDiscovery(service, error)
- }
- func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
- onCharacteristicNotificationStateUpdate(characteristic, error)
- }
- func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
- onCharacteristicValueUpdate(characteristic, error)
- }
- func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
- onCharacteristicValueWrite(characteristic, error)
- }
- }
|