Skip to content

Commit a1f2e2e

Browse files
committed
Refactored BLE connection process to eliminate a lot of cruft. Left the dead code as comments just in case, or roll back to the previous commit if it's really bad. Changed build number to 5
1 parent be396c2 commit a1f2e2e

3 files changed

Lines changed: 51 additions & 29 deletions

File tree

Infini-iOS.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@
780780
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
781781
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
782782
CODE_SIGN_STYLE = Automatic;
783-
CURRENT_PROJECT_VERSION = 3;
783+
CURRENT_PROJECT_VERSION = 5;
784784
DEVELOPMENT_ASSET_PATHS = "\"Infini-iOS/Preview Content\"";
785785
DEVELOPMENT_TEAM = 99BHY6DC82;
786786
ENABLE_PREVIEWS = YES;
@@ -804,7 +804,7 @@
804804
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
805805
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
806806
CODE_SIGN_STYLE = Automatic;
807-
CURRENT_PROJECT_VERSION = 3;
807+
CURRENT_PROJECT_VERSION = 5;
808808
DEVELOPMENT_ASSET_PATHS = "\"Infini-iOS/Preview Content\"";
809809
DEVELOPMENT_TEAM = 99BHY6DC82;
810810
ENABLE_PREVIEWS = YES;

Infini-iOS/BLE/BLEManager.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate {
6464
@Published var blePermissions: Bool!
6565

6666
// Selecting and connecting variables
67-
@Published var peripherals = [Peripheral]() // used to print human-readable device names to UI in selection process
67+
@Published var peripherals = [Peripheral]()
68+
@Published var newPeripherals: [CBPeripheral] = [] // used to print human-readable device names to UI in selection process
6869
@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
6970
@Published var peripheralDictionary: [Int: CBPeripheral] = [:] // this is the dictionary that relates human-readable peripheral names to the CBPeripheral class that CoreBluetooth actually interacts with
7071
@Published var infiniTime: CBPeripheral! // variable to save the CBPeripheral that you're connecting to
@@ -99,6 +100,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate {
99100
isScanning = true
100101
peripherals = [Peripheral]()
101102
peripheralDictionary = [:]
103+
newPeripherals = []
102104
}
103105

104106
func stopScanning() {
@@ -138,18 +140,22 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate {
138140

139141
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
140142

141-
var peripheralName: String!
143+
// var peripheralName: String!
142144

143-
if let name = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
144-
peripheralName = name
145-
let devUUIDString: String = peripheral.identifier.uuidString
146-
let devUUID: CBUUID = CBUUID(string: devUUIDString)
147-
let newPeripheral = Peripheral(id: peripheralDictionary.count, name: peripheralName, rssi: RSSI.intValue, peripheralHash: peripheral.hash, deviceUUID: devUUID, stringUUID: peripheral.identifier.uuidString)
145+
if let _ = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
146+
// peripheralName = name
147+
// let devUUIDString: String = peripheral.identifier.uuidString
148+
// let devUUID: CBUUID = CBUUID(string: devUUIDString)
149+
// let newPeripheral = Peripheral(id: peripheralDictionary.count, name: peripheralName, rssi: RSSI.intValue, peripheralHash: peripheral.hash, deviceUUID: devUUID, stringUUID: peripheral.identifier.uuidString)
148150

149151
guard BLEAutoconnectManager.shared.connect(peripheral: peripheral) else {
150-
if !peripherals.contains(where: {$0.deviceUUID == newPeripheral.deviceUUID}) {
151-
peripherals.append(newPeripheral)
152-
peripheralDictionary[newPeripheral.peripheralHash] = peripheral
152+
// if !peripherals.contains(where: {$0.deviceUUID == newPeripheral.deviceUUID}) {
153+
// peripherals.append(newPeripheral)
154+
// peripheralDictionary[newPeripheral.peripheralHash] = peripheral
155+
// }
156+
if !newPeripherals.contains(where: {$0.identifier.uuidString == peripheral.identifier.uuidString}) {
157+
newPeripherals.append(peripheral)
158+
print(newPeripherals.count)
153159
}
154160
return
155161
}

Infini-iOS/View Components/Sheets/BLEConnectView.swift

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct Connect: View {
1515
@Environment(\.presentationMode) var presentation
1616
@AppStorage("autoconnect") var autoconnect: Bool = false
1717
@AppStorage("autoconnectUUID") var autoconnectUUID: String = ""
18-
18+
1919
var body: some View {
2020
SheetCloseButton()
2121
VStack (){
@@ -37,25 +37,41 @@ struct Connect: View {
3737
.padding()
3838
.frame(maxWidth: .infinity, alignment: .leading)
3939
}
40-
List(bleManager.peripherals) { peripheral in
41-
let deviceName = DeviceNameManager.init().getName(deviceUUID: peripheral.stringUUID)
42-
HStack {
43-
Button(action: {
44-
self.bleManager.deviceToConnect = peripheral.peripheralHash
45-
self.bleManager.connect(peripheral: self.bleManager.peripheralDictionary[peripheral.peripheralHash]!)
46-
presentation.wrappedValue.dismiss()
47-
}) {
48-
if deviceName == "" {
49-
Text(peripheral.name)
50-
} else {
51-
Text(deviceName)
52-
}
40+
// List(bleManager.peripherals) { peripheral in
41+
// let deviceName = DeviceNameManager.init().getName(deviceUUID: peripheral.stringUUID)
42+
// HStack {
43+
// Button(action: {
44+
// //self.bleManager.deviceToConnect = peripheral.peripheralHash
45+
// self.bleManager.connect(peripheral: self.bleManager.peripheralDictionary[peripheral.peripheralHash]!)
46+
// presentation.wrappedValue.dismiss()
47+
// }) {
48+
// if deviceName == "" {
49+
// Text(peripheral.name)
50+
// } else {
51+
// Text(deviceName)
52+
// }
53+
// }
54+
// Spacer()
55+
// Text("RSSI: " + String(peripheral.rssi))
56+
// }
57+
// }
58+
// Divider()
59+
// Text("new list")
60+
List(bleManager.newPeripherals, id: \.identifier.uuidString) { i in
61+
let deviceName = DeviceNameManager.init().getName(deviceUUID: i.identifier.uuidString)
62+
Button {
63+
bleManager.connect(peripheral: i)
64+
presentation.wrappedValue.dismiss()
65+
} label: {
66+
if deviceName == "" {
67+
Text(i.name ?? "Unnamed")
68+
} else {
69+
Text(deviceName)
5370
}
54-
Spacer()
55-
Text("RSSI: " + String(peripheral.rssi))
5671
}
72+
// Spacer()
5773
}
58-
74+
5975
Spacer()
6076
}.onDisappear {
6177
if bleManager.isScanning {

0 commit comments

Comments
 (0)