You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Finally added support for reconnecting to a device that wasn't explicitly disconnected by the user
- Set application characteristics to include permission to run in the background to maintain current BLE connections, and reconnect to the device if it disconnects unexpectedly
- Added functionality to the autoconnect option: rather than grabbing the first device named InfiniTime, the user can manually connect to a device, and then press a button in the settings to save the UUID of the connected device. On app open, if autoconnect is enabled, scan for devices until it sees that UUID, then connect immediately.
Copy file name to clipboardExpand all lines: Infini-iOS/BLE/BLEManager.swift
+49-13Lines changed: 49 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,14 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate {
20
20
varartist:CBCharacteristic!
21
21
}
22
22
23
+
structPeripheral:Identifiable{
24
+
letid:Int
25
+
letname:String
26
+
letrssi:Int
27
+
letperipheralHash:Int
28
+
letdeviceUUID:CBUUID
29
+
}
30
+
23
31
@PublishedvarmusicChars=musicCharacteristics()
24
32
25
33
letsettings=UserDefaults.standard
@@ -29,14 +37,18 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate {
29
37
@PublishedvarisScanning=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
30
38
@PublishedvarisConnectedToPinetime=false // another flag published to update UI stuff. Can probably be implemented better in the future
31
39
@PublishedvarheartBPM:String="Disconnected" // published var to communicate the HRM data to the UI.
32
-
@PublishedvarbatteryLevel:String="20"//"Disconnected" // Same as heartBPM but for battery data
40
+
@PublishedvarbatteryLevel:String="Disconnected" // Same as heartBPM but for battery data
@Publishedvarperipherals=[Peripheral]() // used to print human-readable device names to UI in selection process
37
45
@PublishedvardeviceToConnect:Int! // When the user selects a device from the UI, that peripheral's ID goes in this var, which is passed to the peripheralDictionary
38
46
@PublishedvarperipheralDictionary:[Int:CBPeripheral]=[:] // this is the dictionary that relates human-readable peripheral names to the CBPeripheral class that CoreBluetooth actually interacts with
39
47
@PublishedvarinfiniTime:CBPeripheral! // variable to save the CBPeripheral that you're connecting to
48
+
@PublishedvarautoconnectPeripheral:CBPeripheral!
49
+
@PublishedvarsetAutoconnectUUID:String="" // placeholder for now while I figure out how to save the whole device in UserDefaults to save "favorite" devices
50
+
51
+
varfirstConnect:Bool=true // makes iOS connected message only show up on first connect, not if device drops connection and reconnects
40
52
41
53
// declare some CBUUIDs for easier reference
42
54
lethrmCBUUID=CBUUID(string:"2A37")
@@ -48,12 +60,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate {
@@ -109,35 +130,50 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate {
109
130
- the hash I'm using is only unique between PineTimes because the peripheral struct includes an incrementing ID number that's part of the hash.
110
131
- this works for getting more than one PT in the menu, but is obviously a drag because it's not at all guaranteed to be persistent
111
132
- there's some stuff happening here that doesn't need to happen - ex. there's an array and a dictionary doing basically the same thing?
133
+
- I mistakenly was under the impression that the UUID was generated by InfiniTime and was probably the same across all instances of InfiniTime, but it seems like that's incorrect!
112
134
*/
113
135
114
136
iflet name =advertisementData[CBAdvertisementDataLocalNameKey]as?String{
Toggle("Autoconnect to Nearest PineTime", isOn: $autoconnect)
30
31
if autoconnect {
31
-
Text("Warning! Autoconnect is insecure! Only use in safe environments.").foregroundColor(Color.red)
32
+
Text("Autoconnect is currently experimental! Connection is made based on device UUID, which I am only sort of sure is static.").foregroundColor(Color.red)
0 commit comments