Skip to content

Commit 0ff4187

Browse files
committed
Merge pull request #12 from loudnate/dev
Version 0.2.0
2 parents 3e04af4 + 1e4b955 commit 0ff4187

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

xDripG5/BluetoothManager.swift

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,24 @@ import Foundation
1111

1212

1313
protocol BluetoothManagerDelegate: class {
14+
15+
/**
16+
Tells the delegate that the bluetooth manager has finished connecting to and discovering all required services of its peripheral, or that it failed to do so
17+
18+
- parameter manager: The bluetooth manager
19+
- parameter error: An error describing why bluetooth setup failed
20+
*/
1421
func bluetoothManager(manager: BluetoothManager, isReadyWithError error: NSError?)
22+
23+
/**
24+
Asks the delegate whether the discovered or restored peripheral should be connected
25+
26+
- parameter manager: The bluetooth manager
27+
- parameter peripheral: The found peripheral
28+
29+
- returns: True if the peripheral should connect
30+
*/
31+
func bluetoothManager(manager: BluetoothManager, shouldConnectPeripheral peripheral: CBPeripheral) -> Bool
1532
}
1633

1734

@@ -294,21 +311,24 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
294311
func centralManager(central: CBCentralManager, willRestoreState dict: [String : AnyObject]) {
295312
if peripheral == nil, let peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey] as? [CBPeripheral] {
296313
for peripheral in peripherals {
297-
298-
self.peripheral = peripheral
299-
peripheral.delegate = self
314+
if delegate == nil || delegate!.bluetoothManager(self, shouldConnectPeripheral: peripheral) {
315+
self.peripheral = peripheral
316+
peripheral.delegate = self
317+
}
300318
}
301319
}
302320
}
303321

304322
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
305323

306-
self.peripheral = peripheral
307-
peripheral.delegate = self
324+
if delegate == nil || delegate!.bluetoothManager(self, shouldConnectPeripheral: peripheral) {
325+
self.peripheral = peripheral
326+
peripheral.delegate = self
308327

309-
central.connectPeripheral(peripheral, options: nil)
328+
central.connectPeripheral(peripheral, options: nil)
310329

311-
central.stopScan()
330+
central.stopScan()
331+
}
312332
}
313333

314334
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {

xDripG5/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.0</string>
18+
<string>0.2.0</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

xDripG5/Transmitter.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import Foundation
10+
import CoreBluetooth
1011

1112

1213
public protocol TransmitterDelegate: class {
@@ -24,6 +25,7 @@ public enum TransmitterError: ErrorType {
2425

2526

2627
public class Transmitter: BluetoothManagerDelegate {
28+
2729
public var ID: String
2830

2931
public var startTimeInterval: NSTimeInterval?
@@ -99,6 +101,28 @@ public class Transmitter: BluetoothManagerDelegate {
99101
}
100102
}
101103

104+
/**
105+
Convenience helper for getting a substring of the last two characters of a string.
106+
107+
The Dexcom G5 advertises a peripheral name of "DexcomXX" where "XX" is the last-two characters
108+
of the transmitter ID.
109+
110+
- parameter string: The string to parse
111+
112+
- returns: A new string, containing the last two characters of the input string
113+
*/
114+
private func lastTwoCharactersOfString(string: String) -> String {
115+
return string.substringFromIndex(string.endIndex.advancedBy(-2, limit: string.startIndex))
116+
}
117+
118+
func bluetoothManager(manager: BluetoothManager, shouldConnectPeripheral peripheral: CBPeripheral) -> Bool {
119+
if let name = peripheral.name where lastTwoCharactersOfString(name) == lastTwoCharactersOfString(ID) {
120+
return true
121+
} else {
122+
return false
123+
}
124+
}
125+
102126
// MARK: - Helpers
103127

104128
private func authenticate() throws {

0 commit comments

Comments
 (0)