Set preferred radio automatically

This commit is contained in:
Garth Vander Houwen 2022-10-01 09:37:10 -07:00
parent 68587bdba3
commit ada2093e73
2 changed files with 16 additions and 5 deletions

View file

@ -252,6 +252,15 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
self.isConnected = true
if userSettings?.preferredPeripheralId.count ?? 0 < 1 {
self.userSettings?.preferredPeripheralId = peripheral.identifier.uuidString
self.preferredPeripheral = true
} else if userSettings!.preferredPeripheralId == peripheral.identifier.uuidString {
self.preferredPeripheral = true
} else {
print("Trying to connect a non prefered peripheral")
}
// Invalidate and reset connection timer count, remove any connection errors
self.lastConnectionError = ""
self.timeoutTimerCount = 0
@ -259,7 +268,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
self.timeoutTimer!.invalidate()
}
// Map the peripheral to the connectedPeripheral ObservedObjects
connectedPeripheral = peripherals.filter({ $0.peripheral.identifier == peripheral.identifier }).first
@ -731,7 +740,7 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
// Get all the channels
var i: UInt32 = 1;
var max: Int32 = self.connectedPeripheral.maxChannels
let max: Int32 = self.connectedPeripheral.maxChannels
Timer.scheduledTimer(withTimeInterval: 0.4, repeats: true) { timer in

View file

@ -79,17 +79,18 @@ struct Connect: View {
Text("Preferred").font(.caption2)
Text("Radio").font(.caption2)
Toggle("Preferred Radio", isOn: $isPreferredRadio)
Toggle("Preferred Radio", isOn: $bleManager.preferredPeripheral)
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
.labelsHidden()
.onChange(of: isPreferredRadio) { value in
.onChange(of: bleManager.preferredPeripheral) { value in
if value {
if bleManager.connectedPeripheral != nil {
userSettings.preferredPeripheralId = bleManager.connectedPeripheral!.peripheral.identifier.uuidString
bleManager.preferredPeripheral = true
bleManager.preferredPeripheral = true
isPreferredRadio = true
}
@ -100,6 +101,7 @@ struct Connect: View {
userSettings.preferredPeripheralId = ""
bleManager.preferredPeripheral = false
isPreferredRadio = false
}
}
}