Skip to content

Commit 0c37c77

Browse files
committed
Cleaned up a number of comments and obsolete debug lines.
1 parent e46830b commit 0c37c77

3 files changed

Lines changed: 30 additions & 65 deletions

File tree

Infini-iOS/BLEManager.swift

Lines changed: 27 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate {
2626
var myCentral: CBCentralManager!
2727
var notifyCharacteristic: CBCharacteristic!
2828

29-
@Published var isSwitchedOn = false
30-
@Published var peripherals = [Peripheral]()
31-
@Published var peripheralDictionary: [Int: CBPeripheral] = [:]
32-
@Published var isConnectedToPinetime = false
33-
@Published var infiniTime: CBPeripheral!
34-
@Published var heartBPM: String!
35-
@Published var batteryLevel: String!
36-
@Published var isScanning = false
37-
@Published var deviceToConnect: Int!
29+
// UI flag variables
30+
@Published var isSwitchedOn = false // for now this is used to display if bluetooth is on in the main app screen. maybe an alert in the future?
31+
@Published var isScanning = false // another UI flag. Probably not necessary for anything but debugging. I dunno maybe a little swirly animation or something could be triggered by this
32+
@Published var isConnectedToPinetime = false // another flag published to update UI stuff. Can probably be implemented better in the future
33+
@Published var heartBPM: String! // published var to communicate the HRM data to the UI. I don't know enough about Swift to know if this is a bad idea.
34+
@Published var batteryLevel: String! // Same as heartBPM but for battery data
35+
36+
// Selecting and connecting variables
37+
@Published var peripherals = [Peripheral]() // used to print human-readable device names to UI in selection process
38+
@Published var deviceToConnect: Int! // When the user selects a device from the UI, that peripheral's ID goes in this var, which is passed to the peripheralDictionary
39+
@Published var peripheralDictionary: [Int: CBPeripheral] = [:] // this is the dictionary that relates human-readable peripheral names to the CBPeripheral class that CoreBluetooth actually interacts with
40+
@Published var infiniTime: CBPeripheral! // variable to save the CBPeripheral that you're connecting to
3841

3942
override init() {
4043
super.init()
@@ -66,7 +69,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate {
6669
}
6770

6871
func connect(peripheral: CBPeripheral) {
69-
// working on adding user choice, but still blocking anything but InfiniTime until I can set up a proper way to test it, because it might crash everything?
72+
// Still blocking connections to anything not named "InfiniTime" until I can set up a proper way to test other devices
7073

7174
if peripheral.name == "InfiniTime" {
7275
self.myCentral.stopScan()
@@ -81,8 +84,6 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate {
8184
}
8285

8386
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
84-
// scan BLE devices, looking for any named InfiniTime, and then automatically connect to InfiniTime
85-
// I know this sucks for anyone who has more than one watch, a dev/sealed pair, waspOS, etc. I'll open this up when I've got the core functionality for InfiniTime locked in
8687

8788
var peripheralName: String! // ** not necessary without below scan list thing
8889

@@ -97,55 +98,21 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate {
9798
print(newPeripheral)
9899

99100
// ************************* remove if statement from code! but holy fuck this TV is the noisiest BLE advertiser in the world what the actual fuck *****************************
101+
/*
102+
P.S. I'd love to figure out how to filter by a unique ID other than the name, but all I can seem to find online is that MAC address and other hardware flag things I think would be helpful to filter by are not advertised over BLE, so I don't know what to do about it.
103+
A unique identifier would be super helpful for flagging your pinetime in the app so you can have the app automatically connect and skip the process of scanning and selecting a pinetime.
104+
For now I'm going to selfishly block my TV, and anyone else who touches this can either modify the if statement below to suit their TV or remove it.
105+
*/
100106

101107
if newPeripheral.name != "[TV] Samsung 8 Series (50)" {
102108
peripherals.append(newPeripheral)
103109
peripheralDictionary[newPeripheral.id] = peripheral
104110
}
105-
106-
/*
107-
108-
this can probably be a function
109-
110-
if let pname = peripheral.name {
111-
if pname == "InfiniTime" {
112-
self.myCentral.stopScan()
113-
isScanning = false
114-
115-
self.infiniTime = peripheral
116-
infiniTime.delegate = self
117-
self.myCentral.connect(peripheral, options: nil)
118-
119-
isConnectedToPinetime = true
120-
}
121-
}
122-
123-
*/
124-
125-
/*
126-
127-
********
128-
scan for all nearby BLE devices -- this was part of one of the tuts I was following. For now I'm just going to keep the app tightly coupled to PineTimes running Infinitime to reduce complexity for initial development. This code will be useful if it turns out this app is functional with other similar watches/waspOS/etc.
129-
********
130-
131-
var peripheralName: String! ** not necessary without below scan list thing
132-
133-
if let name = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
134-
peripheralName = name
135-
}
136-
else {
137-
peripheralName = "Unknown"
138-
}
139-
140-
let newPeripheral = Peripheral(id: peripherals.count, name: peripheralName, rssi: RSSI.intValue)
141-
print(newPeripheral)
142-
peripherals.append(newPeripheral)
143-
144-
*/
145111
}
146112

147113
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
148114
self.infiniTime.discoverServices(nil)
115+
sendNotification(notification: "iOS Connected!")
149116
}
150117

151118
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
@@ -182,12 +149,11 @@ extension BLEManager: CBPeripheralDelegate {
182149
// subscribe to values that can be subscribed to
183150
if characteristic.properties.contains(.notify) {
184151
peripheral.setNotifyValue(true, for: characteristic)
185-
// print(characteristic.uuid, "can notify") // debug
186152
}
187153

188154
if characteristic.properties.contains(.write) {
189-
//print(characteristic.uuid, "is writable") // debug
190155
if characteristic.uuid == notifyCBUUID {
156+
// I'm sure there's a less clunky way to grab the full characteristic for the sendNotification() function, but this ad-hoc method works okay and allows it to be published as well.
191157
notifyCharacteristic = characteristic
192158
}
193159
}
@@ -199,7 +165,7 @@ extension BLEManager: CBPeripheralDelegate {
199165
// listen for the music controller notifications
200166
let musicControl = [UInt8](characteristic.value!)
201167
let musicNumber = String(musicControl[0])
202-
// for now just print to console, but I am getting the numbers as a string here, and hopefully I can use that to control music apps soon
168+
// for now just print to console, but I am getting the numbers as a string here, so hopefully I can use that to control music apps soon
203169
print(musicNumber) // debug
204170

205171
case hrmCBUUID:
@@ -222,14 +188,17 @@ extension BLEManager: CBPeripheralDelegate {
222188
}
223189

224190
func sendNotification(notification: String) {
225-
let paddedNotification = " " + notification // I'm pretty sure this is due to a lack of understanding on my part of the notification protocol, but sending ascii text as a notification eats the first 3 characters, so add 3 spaces here to absorb that
191+
// I'm pretty sure this is due to a lack of understanding on my part of the notification protocol, but sending ascii text as a notification eats the first 3 characters seemingly no matter what they are, so add 3 spaces here to absorb that, then encode the string to ASCII Data
192+
let paddedNotification = " " + notification
226193
let notificationData = paddedNotification.data(using: .ascii)!
194+
195+
// this line prevents crashes when sending a notification before the app has finished establishing the notification write characteristic
227196
if notifyCharacteristic != nil {
228197
infiniTime.writeValue(notificationData, for: notifyCharacteristic, type: .withResponse)
229198
}
230199
}
231200

232-
// function to translate heart rate to decimal
201+
// function to translate heart rate to decimal, copied straight up from this tut: https://www.raywenderlich.com/231-core-bluetooth-tutorial-for-ios-heart-rate-monitor#toc-anchor-014
233202
private func heartRate(from characteristic: CBCharacteristic) -> Int {
234203
guard let characteristicData = characteristic.value else { return -1 }
235204
let byteArray = [UInt8](characteristicData)
@@ -244,7 +213,7 @@ extension BLEManager: CBPeripheralDelegate {
244213
}
245214
}
246215

247-
// this function pulls date from phone, shuffles it around, and then hex-encodes it to a format that InfiniTime can understand
216+
// this function pulls date from phone, shuffles it into the correct order, and then hex-encodes it to a format that InfiniTime can understand
248217
private func currentTime() -> String {
249218
let now = Date() // current time
250219

Infini-iOS/ContentView.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ struct ContentView: View {
4646
HStack {
4747
Button(action: {
4848
self.bleManager.deviceToConnect = peripheral.id
49-
print(peripheral.id)
50-
//print(self.bleManager.peripheralDictionary[peripheral.id]?.name)
5149
self.bleManager.connect(peripheral: self.bleManager.peripheralDictionary[peripheral.id]!)
5250
}) {
5351
Text(peripheral.name)

Infini-iOS/HexStringToData.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
//
2-
// File.swift
2+
// HexStringToData.swift
33
// Infini-iOS
44
//
5-
// Created by xan-m on 8/4/21.
5+
// Cloned by xan-m on 8/4/21 (with many thanks) from https://gist.github.com/gamako/94c8cb8873cabb97291b5a839ead50ca
66
//
7-
// cloned from https://gist.github.com/gamako/94c8cb8873cabb97291b5a839ead50ca
8-
//
9-
// extends String to add .hex and .hexData conversions
7+
// extends String to add .hex and .hexData conversions to make my life a million times easier when writing to PineTime
108
//
119

1210
import Foundation

0 commit comments

Comments
 (0)