-
Notifications
You must be signed in to change notification settings - Fork 9
/
BKRemotePeripheral.swift
230 lines (199 loc) · 8.55 KB
/
BKRemotePeripheral.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// BluetoothKit
//
// Copyright (c) 2015 Rasmus Taulborg Hummelmose - https://github.com/rasmusth
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Modified by V. Ganesh to add additional methods to BKRemotePeripheral
// Note: this file to be used with rest of the BluetoothKit library
//
import Foundation
import CoreBluetooth
/**
The delegate of a remote peripheral receives callbacks when asynchronous events occur.
*/
public protocol BKRemotePeripheralDelegate: class {
/**
Called when the remote peripheral updated its name.
- parameter remotePeripheral: The remote peripheral that updated its name.
- parameter name: The new name.
*/
func remotePeripheral(remotePeripheral: BKRemotePeripheral, didUpdateName name: String)
/**
Called when the remote peripheral sent data.
- parameter remotePeripheral: The remote peripheral that sent the data.
- parameter data: The data it sent.
*/
func remotePeripheral(remotePeripheral: BKRemotePeripheral, didSendArbitraryData data: NSData)
}
public func ==(lhs: BKRemotePeripheral, rhs: BKRemotePeripheral) -> Bool {
return lhs.identifier.UUIDString == rhs.identifier.UUIDString
}
/**
Class to represent a remote peripheral that can be connected to by BKCentral objects.
*/
public class BKRemotePeripheral: BKCBPeripheralDelegate, Equatable {
// MARK: Enums
/**
Possible states for BKRemotePeripheral objects.
- Shallow: The peripheral was initialized only with an identifier (used when one wants to connect to a peripheral for which the identifier is known in advance).
- Disconnected: The peripheral is disconnected.
- Connecting: The peripheral is currently connecting.
- Connected: The peripheral is already connected.
- Disconnecting: The peripheral is currently disconnecting.
*/
public enum State {
case Shallow, Disconnected, Connecting, Connected, Disconnecting
}
// MARK: Properties
/// The current state of the remote peripheral, either shallow or derived from an underlying CBPeripheral object.
public var state: State {
if peripheral == nil {
return .Shallow
}
#if os(iOS)
switch peripheral!.state {
case .Disconnected: return .Disconnected
case .Connecting: return .Connecting
case .Connected: return .Connected
case .Disconnecting: return .Disconnecting
}
#else
switch peripheral!.state {
case .Disconnected: return .Disconnected
case .Connecting: return .Connecting
case .Connected: return .Connected
}
#endif
}
/// The name of the remote peripheral, derived from an underlying CBPeripheral object.
public var name: String? {
return peripheral?.name
}
/**
* By VG: the following 3 methods - getPeripheral(), getConfiguration() and getCharacteristic() are added
* to support writing to remote peripherals
*/
public func getPeripheral() -> CBPeripheral? {
return peripheral
}
public func getConfiguration() -> BKConfiguration? {
return configuration
}
public func getCharacteristic() -> CBCharacteristic? {
return characteristic
}
/// The remote peripheral's delegate.
public weak var delegate: BKRemotePeripheralDelegate?
/// The unique identifier of the remote peripheral object.
public let identifier: NSUUID
internal var peripheral: CBPeripheral?
internal var configuration: BKConfiguration?
private var data: NSMutableData?
private var peripheralDelegate: BKCBPeripheralDelegateProxy!
private var characteristic: CBCharacteristic?
// MARK: Initialization
public init(identifier: NSUUID, peripheral: CBPeripheral?) {
self.identifier = identifier
self.peripheralDelegate = BKCBPeripheralDelegateProxy(delegate: self)
self.peripheral = peripheral
}
// MARK: Internal Functions
internal func prepareForConnection() {
peripheral?.delegate = peripheralDelegate
}
internal func discoverServices() {
if peripheral?.services != nil {
peripheral(peripheral!, didDiscoverServices: nil)
return
}
peripheral?.discoverServices(configuration!.serviceUUIDs)
}
internal func unsubscribe() {
guard peripheral?.services != nil else {
return
}
for service in peripheral!.services! {
guard service.characteristics != nil else {
continue
}
for characteristic in service.characteristics! {
peripheral?.setNotifyValue(false, forCharacteristic: characteristic)
}
}
}
// MARK: Private Functions
private func handleReceivedData(receivedData: NSData) {
if receivedData.isEqualToData(configuration!.endOfDataMark) {
if let finalData = data {
delegate?.remotePeripheral(self, didSendArbitraryData: finalData)
}
data = nil
return
}
if let existingData = data {
existingData.appendData(receivedData)
return
}
data = NSMutableData(data: receivedData)
}
// MARK: BKCBPeripheralDelegate
internal func peripheralDidUpdateName(peripheral: CBPeripheral) {
delegate?.remotePeripheral(self, didUpdateName: name!)
}
internal func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
if let services = peripheral.services {
var idx = 1
for service in services {
// print("FOUND SERVICE \(idx)")
// print(service)
// print(service.characteristics)
idx++
if service.characteristics != nil {
self.peripheral(peripheral, didDiscoverCharacteristicsForService: service, error: nil)
} else {
peripheral.discoverCharacteristics(configuration!.characteristicUUIDsForServiceUUID(service.UUID), forService: service)
}
}
}
}
internal func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
if service.UUID == configuration!.dataServiceUUID {
if let dataCharacteristic = service.characteristics?.filter({ $0.UUID == configuration!.dataServiceCharacteristicUUID }).last {
peripheral.setNotifyValue(true, forCharacteristic: dataCharacteristic)
}
}
// TODO: Consider what to do with characteristics from additional services.
// print("didDiscoverCharacteristicsForService")
// print(service.characteristics)
// print(service)
// print(service.includedServices)
/** By VG: the following is added to capture discoved charasteristic */
self.characteristic = service.characteristics?.last
}
internal func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
if characteristic.UUID == configuration!.dataServiceCharacteristicUUID {
handleReceivedData(characteristic.value!)
}
// TODO: Consider what to do with new values for characteristics from additional services.
// print("didUpdateValueForCharacteristic")
// print(characteristic)
}
}