2021-08-20 07:56:05 -07:00
|
|
|
//
|
2021-11-29 21:35:23 -08:00
|
|
|
// Connect.swift
|
2021-08-20 07:56:05 -07:00
|
|
|
// MeshtasticClient
|
|
|
|
|
//
|
|
|
|
|
// Created by Garth Vander Houwen on 8/18/21.
|
|
|
|
|
//
|
|
|
|
|
|
2021-09-12 11:37:19 -07:00
|
|
|
// Abstract:
|
|
|
|
|
// A view allowing you to interact with nearby meshtastic nodes
|
|
|
|
|
|
2021-08-20 07:56:05 -07:00
|
|
|
import SwiftUI
|
|
|
|
|
import MapKit
|
|
|
|
|
import CoreLocation
|
2021-10-03 20:47:04 -07:00
|
|
|
import CoreBluetooth
|
2021-08-20 07:56:05 -07:00
|
|
|
|
2021-09-18 17:10:22 -07:00
|
|
|
struct Connect: View {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-12-12 17:17:46 -08:00
|
|
|
@Environment(\.managedObjectContext) var context
|
|
|
|
|
@EnvironmentObject var bleManager: BLEManager
|
2021-10-22 10:03:50 -07:00
|
|
|
@EnvironmentObject var userSettings: UserSettings
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-17 15:08:12 -07:00
|
|
|
@State var isPreferredRadio: Bool = false
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-08-20 07:56:05 -07:00
|
|
|
var body: some View {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-17 15:08:12 -07:00
|
|
|
NavigationView {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-09-10 07:41:26 -07:00
|
|
|
VStack {
|
2021-09-18 15:33:35 -07:00
|
|
|
if bleManager.isSwitchedOn {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-09-18 15:33:35 -07:00
|
|
|
List {
|
2021-10-22 10:03:50 -07:00
|
|
|
if bleManager.lastConnectionError.count > 0 {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-22 10:03:50 -07:00
|
|
|
Section(header: Text("Connection Error").font(.title)) {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-23 00:19:23 -07:00
|
|
|
Text(bleManager.lastConnectionError).font(.title3).foregroundColor(.red)
|
2021-10-22 10:03:50 -07:00
|
|
|
}
|
|
|
|
|
.textCase(nil)
|
|
|
|
|
}
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-23 10:27:10 -07:00
|
|
|
Section(header: Text("Connected Radio").font(.title)) {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-03 20:47:04 -07:00
|
|
|
if bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral.peripheral.state == .connected {
|
2021-09-23 20:10:53 -07:00
|
|
|
HStack {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-09-21 20:49:12 -07:00
|
|
|
Image(systemName: "antenna.radiowaves.left.and.right")
|
|
|
|
|
.symbolRenderingMode(.hierarchical)
|
|
|
|
|
.imageScale(.large).foregroundColor(.green)
|
|
|
|
|
.padding(.trailing)
|
2021-11-29 15:59:06 -08:00
|
|
|
|
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
|
|
2021-12-15 23:53:45 -08:00
|
|
|
if bleManager.connectedPeripheral != nil {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-12-16 14:13:54 -08:00
|
|
|
Text(bleManager.connectedPeripheral.longName).font(.title2)
|
2021-12-25 23:48:12 -08:00
|
|
|
|
2021-11-29 15:59:06 -08:00
|
|
|
} else {
|
|
|
|
|
|
2021-10-20 00:31:22 -07:00
|
|
|
Text(String(bleManager.connectedPeripheral.peripheral.name ?? "Unknown")).font(.title2)
|
|
|
|
|
}
|
2021-12-16 14:13:54 -08:00
|
|
|
Text("BLE Name: ").font(.caption)+Text(bleManager.connectedPeripheral.name)
|
|
|
|
|
.font(.caption).foregroundColor(Color.gray)
|
2021-12-15 23:53:45 -08:00
|
|
|
if bleManager.connectedPeripheral != nil {
|
2021-12-16 14:13:54 -08:00
|
|
|
Text("FW Version: ").font(.caption)+Text(bleManager.connectedPeripheral.firmwareVersion)
|
|
|
|
|
.font(.caption).foregroundColor(Color.gray)
|
2021-10-20 00:31:22 -07:00
|
|
|
}
|
2021-11-17 07:34:37 -08:00
|
|
|
if bleManager.connectedPeripheral.subscribed {
|
|
|
|
|
Text("Properly Subscribed").font(.caption)
|
|
|
|
|
}
|
2021-10-20 00:31:22 -07:00
|
|
|
}
|
|
|
|
|
Spacer()
|
2021-11-29 15:59:06 -08:00
|
|
|
|
|
|
|
|
VStack(alignment: .center) {
|
|
|
|
|
|
2021-10-20 00:31:22 -07:00
|
|
|
Text("Preferred").font(.caption2)
|
|
|
|
|
Text("Radio").font(.caption2)
|
|
|
|
|
Toggle("Preferred Radio", isOn: $isPreferredRadio)
|
|
|
|
|
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
|
|
|
|
|
.labelsHidden()
|
|
|
|
|
.onChange(of: isPreferredRadio) { value in
|
|
|
|
|
if value {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-12-15 23:53:45 -08:00
|
|
|
if bleManager.connectedPeripheral != nil {
|
2021-12-25 23:48:12 -08:00
|
|
|
|
2021-12-15 23:53:45 -08:00
|
|
|
let deviceName = (bleManager.connectedPeripheral.peripheral.name ?? "")
|
2021-10-22 10:03:50 -07:00
|
|
|
userSettings.preferredPeripheralName = deviceName
|
2021-12-25 23:48:12 -08:00
|
|
|
|
2021-10-17 15:08:12 -07:00
|
|
|
} else {
|
2021-12-25 23:48:12 -08:00
|
|
|
|
2021-12-18 01:01:56 -08:00
|
|
|
userSettings.preferredPeripheralName = bleManager.connectedPeripheral.longName
|
2021-10-17 15:08:12 -07:00
|
|
|
}
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-22 10:03:50 -07:00
|
|
|
userSettings.preferredPeripheralId = bleManager.connectedPeripheral!.peripheral.identifier.uuidString
|
2021-10-20 00:31:22 -07:00
|
|
|
|
|
|
|
|
} else {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-28 00:15:47 -07:00
|
|
|
if bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral.peripheral.identifier.uuidString == userSettings.preferredPeripheralId {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-22 20:58:20 -07:00
|
|
|
userSettings.preferredPeripheralId = ""
|
|
|
|
|
userSettings.preferredPeripheralName = ""
|
|
|
|
|
}
|
2021-10-17 15:08:12 -07:00
|
|
|
}
|
2021-10-20 00:31:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
2021-09-22 21:39:28 -07:00
|
|
|
}
|
2021-10-17 15:08:12 -07:00
|
|
|
.swipeActions {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-17 15:08:12 -07:00
|
|
|
Button(role: .destructive) {
|
2021-11-29 15:59:06 -08:00
|
|
|
if bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral.peripheral.state == CBPeripheralState.connected {
|
2021-11-21 13:48:28 -08:00
|
|
|
bleManager.disconnectPeripheral()
|
2021-10-17 21:10:36 -07:00
|
|
|
isPreferredRadio = false
|
2021-10-17 15:08:12 -07:00
|
|
|
}
|
|
|
|
|
} label: {
|
2021-10-17 20:30:04 -07:00
|
|
|
Label("Disconnect", systemImage: "antenna.radiowaves.left.and.right.slash")
|
2021-10-17 15:08:12 -07:00
|
|
|
}
|
|
|
|
|
}
|
2021-10-23 00:19:23 -07:00
|
|
|
.padding([.top, .bottom])
|
2021-11-29 15:59:06 -08:00
|
|
|
} else {
|
|
|
|
|
HStack {
|
2021-09-21 20:49:12 -07:00
|
|
|
Image(systemName: "antenna.radiowaves.left.and.right.slash")
|
|
|
|
|
.symbolRenderingMode(.hierarchical)
|
|
|
|
|
.imageScale(.large).foregroundColor(.red)
|
|
|
|
|
.padding(.trailing)
|
|
|
|
|
Text("No device connected").font(.title3)
|
|
|
|
|
}
|
2021-09-22 21:39:28 -07:00
|
|
|
.padding()
|
2021-09-18 15:33:35 -07:00
|
|
|
}
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-22 10:03:50 -07:00
|
|
|
}
|
|
|
|
|
.textCase(nil)
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-23 00:19:23 -07:00
|
|
|
if bleManager.peripherals.count > 0 {
|
2021-10-23 10:27:10 -07:00
|
|
|
Section(header: Text("Available Radios").font(.title)) {
|
2021-11-16 10:14:20 -08:00
|
|
|
ForEach(bleManager.peripherals.filter({ $0.peripheral.state == CBPeripheralState.disconnected }).sorted(by: { $0.name < $1.name })) { peripheral in
|
2021-10-23 00:19:23 -07:00
|
|
|
HStack {
|
|
|
|
|
Image(systemName: "circle.fill")
|
|
|
|
|
.imageScale(.large).foregroundColor(.gray)
|
|
|
|
|
.padding(.trailing)
|
|
|
|
|
Button(action: {
|
|
|
|
|
self.bleManager.stopScanning()
|
2021-11-29 15:59:06 -08:00
|
|
|
if bleManager.connectedPeripheral != nil && bleManager.connectedPeripheral.peripheral.state == CBPeripheralState.connected {
|
|
|
|
|
|
2021-11-21 13:48:28 -08:00
|
|
|
self.bleManager.disconnectPeripheral()
|
2021-10-23 00:19:23 -07:00
|
|
|
}
|
|
|
|
|
self.bleManager.connectTo(peripheral: peripheral.peripheral)
|
|
|
|
|
if userSettings.preferredPeripheralId == peripheral.peripheral.identifier.uuidString {
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-23 00:19:23 -07:00
|
|
|
isPreferredRadio = true
|
2021-11-29 15:59:06 -08:00
|
|
|
} else {
|
|
|
|
|
|
2021-10-23 00:19:23 -07:00
|
|
|
isPreferredRadio = false
|
|
|
|
|
}
|
|
|
|
|
}) {
|
|
|
|
|
Text(peripheral.name).font(.title3)
|
2021-10-17 21:10:36 -07:00
|
|
|
}
|
2021-10-23 00:19:23 -07:00
|
|
|
Spacer()
|
|
|
|
|
Text(String(peripheral.rssi) + " dB").font(.title3)
|
2021-11-29 15:59:06 -08:00
|
|
|
}.padding([.bottom, .top])
|
2021-10-23 00:19:23 -07:00
|
|
|
}
|
|
|
|
|
}.textCase(nil)
|
|
|
|
|
}
|
2021-09-18 15:33:35 -07:00
|
|
|
}
|
2021-11-29 15:59:06 -08:00
|
|
|
|
|
|
|
|
HStack(alignment: .center) {
|
2021-09-18 15:33:35 -07:00
|
|
|
Spacer()
|
|
|
|
|
Button(action: {
|
|
|
|
|
self.bleManager.startScanning()
|
|
|
|
|
}) {
|
|
|
|
|
Image(systemName: "play.fill").imageScale(.large).foregroundColor(.gray)
|
|
|
|
|
Text("Start Scanning").font(.caption)
|
|
|
|
|
.font(.caption)
|
|
|
|
|
.foregroundColor(.gray)
|
2021-09-10 21:50:54 -07:00
|
|
|
}
|
2021-10-28 00:15:47 -07:00
|
|
|
.disabled(self.bleManager.isScanning)
|
2021-09-18 15:33:35 -07:00
|
|
|
.padding()
|
|
|
|
|
.background(Color(.systemGray6))
|
|
|
|
|
.clipShape(Capsule())
|
|
|
|
|
Spacer()
|
|
|
|
|
Button(action: {
|
|
|
|
|
self.bleManager.stopScanning()
|
|
|
|
|
}) {
|
|
|
|
|
Image(systemName: "stop.fill").imageScale(.large).foregroundColor(.gray)
|
|
|
|
|
Text("Stop Scanning")
|
|
|
|
|
.font(.caption)
|
2021-09-12 11:37:19 -07:00
|
|
|
.foregroundColor(.gray)
|
2021-09-18 15:33:35 -07:00
|
|
|
}
|
2021-10-28 00:15:47 -07:00
|
|
|
.disabled(!self.bleManager.isScanning)
|
2021-09-18 15:33:35 -07:00
|
|
|
.padding()
|
|
|
|
|
.background(Color(.systemGray6))
|
|
|
|
|
.clipShape(Capsule())
|
|
|
|
|
Spacer()
|
2021-10-23 01:00:42 -07:00
|
|
|
}
|
|
|
|
|
.padding(.bottom, 10)
|
2021-11-29 15:59:06 -08:00
|
|
|
|
|
|
|
|
} else {
|
2021-09-18 15:33:35 -07:00
|
|
|
Text("Bluetooth: OFF")
|
|
|
|
|
.foregroundColor(.red)
|
|
|
|
|
.font(.title)
|
2021-09-10 07:41:26 -07:00
|
|
|
}
|
2021-09-18 15:33:35 -07:00
|
|
|
}
|
|
|
|
|
.navigationTitle("Bluetooth Radios")
|
2021-09-18 21:11:54 -07:00
|
|
|
.navigationBarItems(trailing:
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-12 17:54:10 -07:00
|
|
|
ZStack {
|
2021-12-25 23:48:12 -08:00
|
|
|
|
2021-12-16 14:13:54 -08:00
|
|
|
ConnectedDevice(
|
|
|
|
|
bluetoothOn: bleManager.isSwitchedOn,
|
|
|
|
|
deviceConnected: bleManager.connectedPeripheral != nil,
|
|
|
|
|
name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName :
|
|
|
|
|
"???")
|
2021-09-22 13:00:46 -07:00
|
|
|
}
|
2021-09-18 21:11:54 -07:00
|
|
|
)
|
2021-10-05 09:33:10 -07:00
|
|
|
}
|
2021-10-12 17:54:10 -07:00
|
|
|
.navigationViewStyle(StackNavigationViewStyle())
|
2021-10-17 15:08:12 -07:00
|
|
|
.onAppear(perform: {
|
2021-10-23 00:19:23 -07:00
|
|
|
|
2021-12-12 17:17:46 -08:00
|
|
|
self.bleManager.context = context
|
|
|
|
|
|
2021-10-17 15:08:12 -07:00
|
|
|
if bleManager.connectedPeripheral != nil && userSettings.preferredPeripheralId == bleManager.connectedPeripheral.peripheral.identifier.uuidString {
|
|
|
|
|
isPreferredRadio = true
|
2021-11-29 15:59:06 -08:00
|
|
|
} else {
|
2021-10-17 21:10:36 -07:00
|
|
|
isPreferredRadio = false
|
2021-10-17 15:08:12 -07:00
|
|
|
}
|
2021-10-23 00:19:23 -07:00
|
|
|
})
|
2021-08-20 07:56:05 -07:00
|
|
|
}
|
|
|
|
|
}
|
2021-09-18 21:11:54 -07:00
|
|
|
|
|
|
|
|
struct Connect_Previews: PreviewProvider {
|
|
|
|
|
|
|
|
|
|
static var previews: some View {
|
2021-09-22 13:00:46 -07:00
|
|
|
Connect()
|
2021-10-28 00:15:47 -07:00
|
|
|
|
2021-09-22 13:00:46 -07:00
|
|
|
.environmentObject(BLEManager())
|
2021-09-18 21:11:54 -07:00
|
|
|
}
|
|
|
|
|
}
|