Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions xDripG5/BluetoothManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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, shouldConnectPeripheral peripheral: CBPeripheral) -> Bool
}


Expand Down Expand Up @@ -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, shouldConnectPeripheral: 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, shouldConnectPeripheral: 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) {
Expand Down
2 changes: 1 addition & 1 deletion xDripG5/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>0.2.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
24 changes: 24 additions & 0 deletions xDripG5/Transmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import CoreBluetooth


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


public class Transmitter: BluetoothManagerDelegate {

public var ID: String

public var startTimeInterval: NSTimeInterval?
Expand Down Expand Up @@ -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, shouldConnectPeripheral peripheral: CBPeripheral) -> Bool {
if let name = peripheral.name where lastTwoCharactersOfString(name) == lastTwoCharactersOfString(ID) {
return true
} else {
return false
}
}

// MARK: - Helpers

private func authenticate() throws {
Expand Down