-
Notifications
You must be signed in to change notification settings - Fork 8
/
CBPeripheralMock.swift
110 lines (84 loc) · 3.66 KB
/
CBPeripheralMock.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// CBPeripheralMock.swift
// factory-tourguide-iOS
//
// Created by kazuya ito on 2020/10/28.
//
import Foundation
import CoreBluetooth
class CBPeripheralMock: Mock, CBPeripheralProtocol {
weak var delegate: CBPeripheralDelegate?
var state: CBPeripheralState = .disconnected
var identifier: UUID
var name: String?
var services: [CBService]?
var manager: CBCentralManagerMock
private var serviceCharacteristic = ServiceCharacteristicsMock()
var debugDescription: String {
return "\(identifier) \(name ?? "")"
}
init(identifier: UUID, name: String?, manager: CBCentralManagerMock) {
self.identifier = identifier
self.name = name
self.manager = manager
log(#function)
}
func didConnect(_ central: CBCentralManagerProtocol, peripheral: CBPeripheralProtocol){
state = .connected
}
func didDisconnect(_ central: CBCentralManagerProtocol, peripheral: CBPeripheralProtocol, error: Error?) {
state = .disconnected
}
func discoverServices(_ serviceUUIDs: [CBUUID]?) {
log(#function)
services = serviceCharacteristic.service()
guard let delegate = delegate as? CBPeripheralProtocolDelegate else { return }
delegate.didDiscoverServices(self, error: nil)
}
func discoverCharacteristics(_ characteristicUUIDs: [CBUUID]?, for service: CBService) {
log(#function)
guard let mutableService = service as? CBMutableService,
let delegate = delegate as? CBPeripheralProtocolDelegate
else { return }
mutableService.characteristics = serviceCharacteristic.characteristics(service.uuid)
delegate.didDiscoverCharacteristics(self, service: mutableService, error: nil)
}
func readValue(for characteristic: CBCharacteristic) {
log(#function)
guard let mutableCharacteristic = characteristic as? CBMutableCharacteristic,
let delegate = delegate as? CBPeripheralProtocolDelegate
else { return }
mutableCharacteristic.value = serviceCharacteristic.value(uuid: mutableCharacteristic.uuid)
if let _ = mutableCharacteristic.value {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
delegate.didUpdateValue(self,
characteristic: characteristic,
error: nil)
}
}
}
func writeValue(_ data: Data, for characteristic: CBCharacteristic, type: CBCharacteristicWriteType) {
log(#function)
guard let mutableCharacteristic = characteristic as? CBMutableCharacteristic,
let delegate = delegate as? CBPeripheralProtocolDelegate
else { return }
serviceCharacteristic.writeValue(uuid: mutableCharacteristic.uuid, writeValue: data)
mutableCharacteristic.value = serviceCharacteristic.value(uuid: mutableCharacteristic.uuid)
if let _ = mutableCharacteristic.value {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
delegate.didUpdateValue(self,
characteristic: characteristic,
error: nil)
}
}
}
func setNotifyValue(_ enabled: Bool, for characteristic: CBCharacteristic) {
log(#function)
}
private func dataNotify(_ delegate: CBPeripheralProtocolDelegate) {
log(#function)
}
func discoverDescriptors(for characteristic: CBCharacteristic) {
log(#function)
}
}