From d978408dcf822a76d6347ea20a323d71e9394a25 Mon Sep 17 00:00:00 2001 From: Nathan Racklyeft Date: Sat, 16 Jan 2016 09:48:53 -0800 Subject: [PATCH 1/3] Set the version string correctly for the next release --- xDripG5/Info.plist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xDripG5/Info.plist b/xDripG5/Info.plist index d3de8eef..6019f05b 100644 --- a/xDripG5/Info.plist +++ b/xDripG5/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.0 + 0.2.0 CFBundleSignature ???? CFBundleVersion From 6fb6525e8537c874120ac10df343b6a6f8d49960 Mon Sep 17 00:00:00 2001 From: Nathan Racklyeft Date: Sat, 16 Jan 2016 09:52:23 -0800 Subject: [PATCH 2/3] Match advertised name to transmitter ID before connecting --- xDripG5/BluetoothManager.swift | 34 +++++++++++++++++++++++++++------- xDripG5/Transmitter.swift | 24 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/xDripG5/BluetoothManager.swift b/xDripG5/BluetoothManager.swift index f7e97741..7e1d15de 100644 --- a/xDripG5/BluetoothManager.swift +++ b/xDripG5/BluetoothManager.swift @@ -11,7 +11,24 @@ import Foundation protocol BluetoothManagerDelegate: class { + + /** + 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 + + - parameter manager: The bluetooth manager + - parameter error: An error describing why bluetooth setup failed + */ func bluetoothManager(manager: BluetoothManager, isReadyWithError error: NSError?) + + /** + Asks the delegate whether the discovered or restored peripheral should be connected + + - parameter manager: The bluetooth manager + - parameter peripheral: The found peripheral + + - returns: True if the peripheral should connect + */ + func bluetoothManager(manager: BluetoothManager, shouldConnectToPeripheral peripheral: CBPeripheral) -> Bool } @@ -294,21 +311,24 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate func centralManager(central: CBCentralManager, willRestoreState dict: [String : AnyObject]) { if peripheral == nil, let peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey] as? [CBPeripheral] { for peripheral in peripherals { - - self.peripheral = peripheral - peripheral.delegate = self + if delegate == nil || delegate!.bluetoothManager(self, shouldConnectToPeripheral: peripheral) { + self.peripheral = peripheral + peripheral.delegate = self + } } } } func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) { - self.peripheral = peripheral - peripheral.delegate = self + if delegate == nil || delegate!.bluetoothManager(self, shouldConnectToPeripheral: peripheral) { + self.peripheral = peripheral + peripheral.delegate = self - central.connectPeripheral(peripheral, options: nil) + central.connectPeripheral(peripheral, options: nil) - central.stopScan() + central.stopScan() + } } func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) { diff --git a/xDripG5/Transmitter.swift b/xDripG5/Transmitter.swift index f0316e51..6fcdc76f 100644 --- a/xDripG5/Transmitter.swift +++ b/xDripG5/Transmitter.swift @@ -7,6 +7,7 @@ // import Foundation +import CoreBluetooth public protocol TransmitterDelegate: class { @@ -24,6 +25,7 @@ public enum TransmitterError: ErrorType { public class Transmitter: BluetoothManagerDelegate { + public var ID: String public var startTimeInterval: NSTimeInterval? @@ -99,6 +101,28 @@ public class Transmitter: BluetoothManagerDelegate { } } + /** + Convenience helper for getting a substring of the last two characters of a string. + + The Dexcom G5 advertises a peripheral name of "DexcomXX" where "XX" is the last-two characters + of the transmitter ID. + + - parameter string: The string to parse + + - returns: A new string, containing the last two characters of the input string + */ + private func lastTwoCharactersOfString(string: String) -> String { + return string.substringFromIndex(string.endIndex.advancedBy(-2, limit: string.startIndex)) + } + + func bluetoothManager(manager: BluetoothManager, shouldConnectToPeripheral peripheral: CBPeripheral) -> Bool { + if let name = peripheral.name where lastTwoCharactersOfString(name) == lastTwoCharactersOfString(ID) { + return true + } else { + return false + } + } + // MARK: - Helpers private func authenticate() throws { From c86d1b57ddc0808f8c518acdf5b547a050ff0764 Mon Sep 17 00:00:00 2001 From: Nathan Racklyeft Date: Sat, 16 Jan 2016 09:54:54 -0800 Subject: [PATCH 3/3] Make the delegate name more Cocoa-ic Fixes #8 --- xDripG5/BluetoothManager.swift | 6 +++--- xDripG5/Transmitter.swift | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/xDripG5/BluetoothManager.swift b/xDripG5/BluetoothManager.swift index 7e1d15de..d47b47d1 100644 --- a/xDripG5/BluetoothManager.swift +++ b/xDripG5/BluetoothManager.swift @@ -28,7 +28,7 @@ protocol BluetoothManagerDelegate: class { - returns: True if the peripheral should connect */ - func bluetoothManager(manager: BluetoothManager, shouldConnectToPeripheral peripheral: CBPeripheral) -> Bool + func bluetoothManager(manager: BluetoothManager, shouldConnectPeripheral peripheral: CBPeripheral) -> Bool } @@ -311,7 +311,7 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate func centralManager(central: CBCentralManager, willRestoreState dict: [String : AnyObject]) { if peripheral == nil, let peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey] as? [CBPeripheral] { for peripheral in peripherals { - if delegate == nil || delegate!.bluetoothManager(self, shouldConnectToPeripheral: peripheral) { + if delegate == nil || delegate!.bluetoothManager(self, shouldConnectPeripheral: peripheral) { self.peripheral = peripheral peripheral.delegate = self } @@ -321,7 +321,7 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) { - if delegate == nil || delegate!.bluetoothManager(self, shouldConnectToPeripheral: peripheral) { + if delegate == nil || delegate!.bluetoothManager(self, shouldConnectPeripheral: peripheral) { self.peripheral = peripheral peripheral.delegate = self diff --git a/xDripG5/Transmitter.swift b/xDripG5/Transmitter.swift index 6fcdc76f..f5642b3a 100644 --- a/xDripG5/Transmitter.swift +++ b/xDripG5/Transmitter.swift @@ -115,7 +115,7 @@ public class Transmitter: BluetoothManagerDelegate { return string.substringFromIndex(string.endIndex.advancedBy(-2, limit: string.startIndex)) } - func bluetoothManager(manager: BluetoothManager, shouldConnectToPeripheral peripheral: CBPeripheral) -> Bool { + func bluetoothManager(manager: BluetoothManager, shouldConnectPeripheral peripheral: CBPeripheral) -> Bool { if let name = peripheral.name where lastTwoCharactersOfString(name) == lastTwoCharactersOfString(ID) { return true } else {