-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathViewController.swift
More file actions
183 lines (145 loc) · 6.94 KB
/
ViewController.swift
File metadata and controls
183 lines (145 loc) · 6.94 KB
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
import UIKit
import CoreBluetooth
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var centralManager:CBCentralManager!
var connectingPeripheral:CBPeripheral!
@IBOutlet var label1: UILabel!
let POLARH7_HRM_HEART_RATE_SERVICE_UUID = "180D"
let POLARH7_HRM_DEVICE_INFO_SERVICE_UUID = "180A"
override func viewDidLoad() {
let heartRateServiceUUID = CBUUID(string: POLARH7_HRM_HEART_RATE_SERVICE_UUID)
let deviceInfoServiceUUID = CBUUID(string: POLARH7_HRM_DEVICE_INFO_SERVICE_UUID)
let services = [heartRateServiceUUID, deviceInfoServiceUUID];
//let centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
let centralManager = CBCentralManager(delegate: self, queue: nil)
centralManager.scanForPeripherals(withServices: services, options: nil)
//[centralManager scanForPeripheralsWithServices:services options:nil];
self.centralManager = centralManager;
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
print("--- centralManagerDidUpdateState")
switch central.state{
case .poweredOn:
print("poweredOn")
let serviceUUIDs:[AnyObject] = [CBUUID(string: "180D")]
let lastPeripherals = centralManager.retrieveConnectedPeripherals(withServices: serviceUUIDs as! [CBUUID])
if lastPeripherals.count > 0{
let device = lastPeripherals.last! as CBPeripheral;
connectingPeripheral = device;
centralManager.connect(connectingPeripheral, options: nil)
}
else {
centralManager.scanForPeripherals(withServices: serviceUUIDs as? [CBUUID], options: nil)
}
case .poweredOff:
print("--- central state is powered off")
case .resetting:
print("--- central state is resetting")
case .unauthorized:
print("--- central state is unauthorized")
case .unknown:
print("--- central state is unknown")
case .unsupported:
print("--- central state is unsupported")
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("--- didDiscover peripheral")
if let localName = advertisementData[CBAdvertisementDataLocalNameKey] as? String{
print("--- found heart rate monitor named \(localName)")
self.centralManager.stopScan()
connectingPeripheral = peripheral
connectingPeripheral.delegate = self
centralManager.connect(connectingPeripheral, options: nil)
}else{
print("!!!--- can't unwrap advertisementData[CBAdvertisementDataLocalNameKey]")
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("--- didConnectPeripheral")
peripheral.delegate = self
peripheral.discoverServices(nil)
print("--- peripheral state is \(peripheral.state)")
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if (error) != nil{
print("!!!--- error in didDiscoverServices: \(error?.localizedDescription)")
}
else {
print("--- error in didDiscoverServices")
for service in peripheral.services as [CBService]!{
peripheral.discoverCharacteristics(nil, for: service)
}
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if (error) != nil{
print("!!!--- error in didDiscoverCharacteristicsFor: \(error?.localizedDescription)")
}
else {
if service.uuid == CBUUID(string: "180D"){
for characteristic in service.characteristics! as [CBCharacteristic]{
switch characteristic.uuid.uuidString{
case "2A37":
// Set notification on heart rate measurement
print("Found a Heart Rate Measurement Characteristic")
peripheral.setNotifyValue(true, for: characteristic)
case "2A38":
// Read body sensor location
print("Found a Body Sensor Location Characteristic")
peripheral.readValue(for: characteristic)
case "2A29":
// Read body sensor location
print("Found a HRM manufacturer name Characteristic")
peripheral.readValue(for: characteristic)
case "2A39":
// Write heart rate control point
print("Found a Heart Rate Control Point Characteristic")
var rawArray:[UInt8] = [0x01];
let data = NSData(bytes: &rawArray, length: rawArray.count)
peripheral.writeValue(data as Data, for: characteristic, type: CBCharacteristicWriteType.withoutResponse)
default:
print()
}
}
}
}
}
func update(heartRateData:Data){
print("--- UPDATING ..")
var buffer = [UInt8](repeating: 0x00, count: heartRateData.count)
heartRateData.copyBytes(to: &buffer, count: buffer.count)
var bpm:UInt16?
if (buffer.count >= 2){
if (buffer[0] & 0x01 == 0){
bpm = UInt16(buffer[1]);
}else {
bpm = UInt16(buffer[1]) << 8
bpm = bpm! | UInt16(buffer[2])
}
}
if let actualBpm = bpm{
print(actualBpm)
label1.text = ("\(actualBpm)")
}else {
label1.text = ("\(bpm!)")
print(bpm!)
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
print("--- didUpdateValueForCharacteristic")
if (error) != nil{
}else {
switch characteristic.uuid.uuidString{
case "2A37":
update(heartRateData:characteristic.value!)
default:
print("--- something other than 2A37 uuid characteristic")
}
}
}
}