-
Notifications
You must be signed in to change notification settings - Fork 0
/
BluetoothCommunication.swift
182 lines (159 loc) · 6.16 KB
/
BluetoothCommunication.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
//
// BluetoothCommunication.swift
// XCDisplayWatch Extension
//
// Created by Stefan on 17.10.17.
// Copyright © 2017 Hanshans. All rights reserved.
// Modified by JEK58
import CoreBluetooth
protocol BluetoothCommunicationDelegate: class {
func bluetoothDidUpdateState()//Status hat sich geändert
func bluetoothIsPoweredOn()//Bluetooth ist verfügbar
func deviceDidConnect()//Verbunden
func deviceDidDisconnect()//wurde getrennt
func deviceHasNewData(_ data: Float)//Neue Daten zurückgeben
}
class BluetoothCommunication: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
//Die Objekte werden nur hier gebraucht, daher privat
private var centralManager: CBCentralManager!
private weak var delegate: BluetoothCommunicationDelegate?
private var peripheral: CBPeripheral!
private var writeCharacteristic: CBCharacteristic!
private let distoCharateristicDistance = "3AB10101-F831-4395-B29D-570977D5BF94"
private let distoCharateristicCommand = "3AB10109-F831-4395-B29D-570977D5BF94"
private var dataBuffer = [Character]()
func start(withDelegate delegate: BluetoothCommunicationDelegate) {
if self.centralManager == nil {
self.centralManager = CBCentralManager(delegate: self, queue: nil)
}
self.delegate = delegate
}
func startScanning() {
print("Start Scanning")
if self.centralManager.state == .poweredOn {
let string = "3ab10100-f831-4395-b29d-570977d5bf94"
self.centralManager.scanForPeripherals(withServices: [CBUUID(string: string)],
options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
} else {
self.centralManager.scanForPeripherals(withServices: nil,
options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
}
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
delegate?.bluetoothDidUpdateState()
switch central.state {
case .unsupported:
break
case .poweredOff:
if self.peripheral != nil {
centralManager( central, didDisconnectPeripheral: self.peripheral, error: nil)
}
case .unauthorized:
break
case .poweredOn:
delegate?.bluetoothIsPoweredOn()
default:
break
}
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
self.peripheral = nil
delegate?.deviceDidDisconnect()
}
func centralManager(_ central: CBCentralManager,
didDiscover peripheral: CBPeripheral,
advertisementData: [String: Any],
rssi RSSI: NSNumber) {
self.peripheral = peripheral
self.peripheral.delegate = self
central.stopScan()
self.connect()
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
self.peripheral.discoverServices(nil)
delegate?.deviceDidConnect()
}
/*
Original function
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
for service in peripheral.services! as [CBService] {
if error == nil {
switch service.uuid.uuidString {
case "3AB10100-F831-4395-B29D-570977D5BF94":
peripheral.discoverCharacteristics(nil, for: service)
self.writeCharacteristic = nil
default:
break
}
}
}
}
*/
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
for service in peripheral.services! as [CBService] where error == nil {
switch service.uuid.uuidString {
case "3AB10100-F831-4395-B29D-570977D5BF94":
peripheral.discoverCharacteristics(nil, for: service)
self.writeCharacteristic = nil
default:
break
}
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if error == nil {
for characteristic in service.characteristics! as [CBCharacteristic] {
switch characteristic.uuid.uuidString {
case distoCharateristicDistance://Data Transfer
peripheral.setNotifyValue(true, for: characteristic)
case distoCharateristicCommand: //Navigation Characteristic
self.writeCharacteristic = characteristic
default:
break
}
}
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if error == nil {
switch characteristic.uuid.uuidString {
case distoCharateristicDistance:
self.update(deviceData: characteristic.value!)
default:
break
}
}
}
func connect() {
if self.peripheral != nil {
let state = self.peripheral.state
switch state {
case .disconnected:
self.centralManager.connect(self.peripheral, options: nil)
case .connecting:
break
case .connected:
break
case .disconnecting:
break
@unknown default:
fatalError()
}
}
}
// Modified
func writeByte(data: UInt8) {
let newData = NSMutableData(bytes: [data], length: 1)
guard peripheral != nil else {
print("*** Peripheral is nil ***")
return
}
self.peripheral!.writeValue(newData as Data, for: self.writeCharacteristic!,
type: CBCharacteristicWriteType.withoutResponse)
}
func update(deviceData: Data) {
var fvalue: Float = 0
(deviceData as NSData).getBytes(&fvalue, length: 4)
delegate?.deviceHasNewData(fvalue)
}
}