-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy path_Characteristic.generated.swift
196 lines (178 loc) · 9.42 KB
/
_Characteristic.generated.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import Foundation
import RxSwift
import CoreBluetooth
@testable import RxBluetoothKit
/// _Characteristic is a class implementing ReactiveX which wraps CoreBluetooth functions related to interaction with [CBCharacteristicMock](https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBCharacteristic_Class/)
class _Characteristic {
/// Intance of CoreBluetooth characteristic class
let characteristic: CBCharacteristicMock
/// _Service which contains this characteristic
let service: _Service
/// Current value of characteristic. If value is not present - it's `nil`.
var value: Data? {
return characteristic.value
}
/// The Bluetooth UUID of the `_Characteristic` instance.
var uuid: CBUUID {
return characteristic.uuid
}
/// Flag which is set to true if characteristic is currently notifying
var isNotifying: Bool {
return characteristic.isNotifying
}
/// Properties of characteristic. For more info about this refer to [CBCharacteristicProperties](https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBCharacteristic_Class/#//apple_ref/c/tdef/CBCharacteristicProperties)
var properties: CBCharacteristicProperties {
return characteristic.properties
}
/// Value of this property is an array of `_Descriptor` objects. They provide more detailed information about characteristics value.
var descriptors: [_Descriptor]? {
return characteristic.descriptors?.map { _Descriptor(descriptor: $0, characteristic: self) }
}
init(characteristic: CBCharacteristicMock, service: _Service) {
self.characteristic = characteristic
self.service = service
}
convenience init?(characteristic: CBCharacteristicMock, peripheral: _Peripheral) {
guard let _service = characteristic.service else {
return nil
}
let service = _Service(peripheral: peripheral, service: _service)
self.init(characteristic: characteristic, service: service)
}
/// Function that triggers descriptors discovery for characteristic.
/// - returns: `Single` that emits `next` with array of `_Descriptor` instances, once they're discovered.
///
/// Observable can ends with following errors:
/// * `_BluetoothError.descriptorsDiscoveryFailed`
/// * `_BluetoothError.peripheralDisconnected`
/// * `_BluetoothError.destroyed`
/// * `_BluetoothError.bluetoothUnsupported`
/// * `_BluetoothError.bluetoothUnauthorized`
/// * `_BluetoothError.bluetoothPoweredOff`
/// * `_BluetoothError.bluetoothInUnknownState`
/// * `_BluetoothError.bluetoothResetting`
func discoverDescriptors() -> Single<[_Descriptor]> {
return service.peripheral.discoverDescriptors(for: self)
}
/// Function that allow to observe writes that happened for characteristic.
/// - Returns: `Observable` that emits `next` with `_Characteristic` instance every time when write has happened.
/// It's **infinite** stream, so `.complete` is never called.
///
/// Observable can ends with following errors:
/// * `_BluetoothError.characteristicWriteFailed`
/// * `_BluetoothError.peripheralDisconnected`
/// * `_BluetoothError.destroyed`
/// * `_BluetoothError.bluetoothUnsupported`
/// * `_BluetoothError.bluetoothUnauthorized`
/// * `_BluetoothError.bluetoothPoweredOff`
/// * `_BluetoothError.bluetoothInUnknownState`
/// * `_BluetoothError.bluetoothResetting`
func observeWrite() -> Observable<_Characteristic> {
return service.peripheral.observeWrite(for: self)
}
/// Function that allows to know the exact time, when isNotyfing value has changed on a characteristic.
///
/// - returns: `Observable` emitting `_Characteristic` when isNoytfing value has changed.
///
/// Observable can ends with following errors:
/// * `_BluetoothError.characteristicSetNotifyValueFailed`
/// * `_BluetoothError.peripheralDisconnected`
/// * `_BluetoothError.destroyed`
/// * `_BluetoothError.bluetoothUnsupported`
/// * `_BluetoothError.bluetoothUnauthorized`
/// * `_BluetoothError.bluetoothPoweredOff`
/// * `_BluetoothError.bluetoothInUnknownState`
/// * `_BluetoothError.bluetoothResetting`
func observeNotifyValue() -> Observable<_Characteristic> {
return service.peripheral.observeNotifyValue(for: self)
}
/// Function that triggers write of data to characteristic. Write is called after subscribtion to `Observable` is made.
/// Behavior of this function strongly depends on [CBCharacteristicWriteType](https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBPeripheral_Class/#//apple_ref/swift/enum/c:@E@CBCharacteristicWriteType), so be sure to check this out before usage of the method.
/// - parameter data: `Data` that'll be written to the `_Characteristic`
/// - parameter type: Type of write operation. Possible values: `.withResponse`, `.withoutResponse`
/// - returns: `Single` whose emission depends on `CBCharacteristicWriteType` passed to the function call.
/// Behavior is following:
///
/// - `withResponse` - `Observable` emits `next` with `_Characteristic` instance write was confirmed without any errors.
/// If any problem has happened, errors are emitted.
/// - `withoutResponse` - `Observable` emits `next` with `_Characteristic` instance once write was called.
/// Result of this call is not checked, so as a user you are not sure
/// if everything completed successfully. Errors are not emitted
///
/// Observable can ends with following errors:
/// * `_BluetoothError.characteristicWriteFailed`
/// * `_BluetoothError.peripheralDisconnected`
/// * `_BluetoothError.destroyed`
/// * `_BluetoothError.bluetoothUnsupported`
/// * `_BluetoothError.bluetoothUnauthorized`
/// * `_BluetoothError.bluetoothPoweredOff`
/// * `_BluetoothError.bluetoothInUnknownState`
/// * `_BluetoothError.bluetoothResetting`
func writeValue(_ data: Data, type: CBCharacteristicWriteType) -> Single<_Characteristic> {
return service.peripheral.writeValue(data, for: self, type: type)
}
/// Function that allow to observe value updates for `_Characteristic` instance.
/// - Returns: `Observable` that emits `Next` with `_Characteristic` instance every time when value has changed.
/// It's **infinite** stream, so `.complete` is never called.
///
/// Observable can ends with following errors:
/// * `_BluetoothError.characteristicReadFailed`
/// * `_BluetoothError.peripheralDisconnected`
/// * `_BluetoothError.destroyed`
/// * `_BluetoothError.bluetoothUnsupported`
/// * `_BluetoothError.bluetoothUnauthorized`
/// * `_BluetoothError.bluetoothPoweredOff`
/// * `_BluetoothError.bluetoothInUnknownState`
/// * `_BluetoothError.bluetoothResetting`
func observeValueUpdate() -> Observable<_Characteristic> {
return service.peripheral.observeValueUpdate(for: self)
}
/// Function that triggers read of current value of the `_Characteristic` instance.
/// Read is called after subscription to `Observable` is made.
/// - Returns: `Single` which emits `next` with given characteristic when value is ready to read.
///
/// Observable can ends with following errors:
/// * `_BluetoothError.characteristicReadFailed`
/// * `_BluetoothError.peripheralDisconnected`
/// * `_BluetoothError.destroyed`
/// * `_BluetoothError.bluetoothUnsupported`
/// * `_BluetoothError.bluetoothUnauthorized`
/// * `_BluetoothError.bluetoothPoweredOff`
/// * `_BluetoothError.bluetoothInUnknownState`
/// * `_BluetoothError.bluetoothResetting`
func readValue() -> Single<_Characteristic> {
return service.peripheral.readValue(for: self)
}
/// Setup characteristic notification in order to receive callbacks when given characteristic has been changed.
/// Returned observable will emit `_Characteristic` on every notification change.
/// It is possible to setup more observables for the same characteristic and the lifecycle of the notification will be shared among them.
///
/// Notification is automaticaly unregistered once this observable is unsubscribed
///
/// - returns: `Observable` emitting `next` with `_Characteristic` when given characteristic has been changed.
///
/// This is **infinite** stream of values.
///
/// Observable can ends with following errors:
/// * `_BluetoothError.characteristicReadFailed`
/// * `_BluetoothError.peripheralDisconnected`
/// * `_BluetoothError.destroyed`
/// * `_BluetoothError.bluetoothUnsupported`
/// * `_BluetoothError.bluetoothUnauthorized`
/// * `_BluetoothError.bluetoothPoweredOff`
/// * `_BluetoothError.bluetoothInUnknownState`
/// * `_BluetoothError.bluetoothResetting`
func observeValueUpdateAndSetNotification() -> Observable<_Characteristic> {
return service.peripheral.observeValueUpdateAndSetNotification(for: self)
}
}
extension _Characteristic: Equatable {}
extension _Characteristic: UUIDIdentifiable {}
/// Compare two characteristics. Characteristics are the same when their UUIDs are the same.
///
/// - parameter lhs: First characteristic to compare
/// - parameter rhs: Second characteristic to compare
/// - returns: True if both characteristics are the same.
func == (lhs: _Characteristic, rhs: _Characteristic) -> Bool {
return lhs.characteristic == rhs.characteristic
}