From d2b578dbada1723c37798351c2750537cadde253 Mon Sep 17 00:00:00 2001 From: "severin.memmishofer" Date: Thu, 13 Jul 2023 16:00:02 +0200 Subject: [PATCH] Added BT Controller & Peripheral as EnvironmentObjects --- RippleChat/BTPeripheral.swift | 50 ++++++----------------------- RippleChat/ContentView.swift | 10 ++++++ RippleChat/Views/FeedListView.swift | 1 + RippleChat/Views/PeeringView.swift | 11 ++++--- 4 files changed, 27 insertions(+), 45 deletions(-) diff --git a/RippleChat/BTPeripheral.swift b/RippleChat/BTPeripheral.swift index 48c3c02..913b7e0 100644 --- a/RippleChat/BTPeripheral.swift +++ b/RippleChat/BTPeripheral.swift @@ -21,64 +21,34 @@ class BluetoothPeripheral: NSObject, ObservableObject { } } -// TODO: Change variable names, etc... - extension BluetoothPeripheral: CBPeripheralManagerDelegate { func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) { switch peripheral.state { case .unknown: - print("Bluetooth Device is UNKNOWN") + print("BT Device is UNKNOWN") case .unsupported: - print("Bluetooth Device is UNSUPPORTED") + print("BT Device is UNSUPPORTED") case .unauthorized: - print("Bluetooth Device is UNAUTHORIZED") + print("BT Device is UNAUTHORIZED") case .resetting: - print("Bluetooth Device is RESETTING") + print("BT Device is RESETTING") case .poweredOff: - print("Bluetooth Device is POWERED OFF") + print("BT Device is POWERED OFF") case .poweredOn: - print("Bluetooth Device is POWERED ON") - addServices() + print("BT Device is POWERED ON") + addBTService() @unknown default: fatalError() } } - func addServices() { - - let myCharacteristic = CBMutableCharacteristic(type: BLE_CHARACTERISTIC_UUID_RX, properties: [.read, .write, .notify], value: nil, permissions: [.readable]) - - // 2. Create instance of CBMutableService + func addBTService() { + let myCharacteristic = CBMutableCharacteristic(type: BLE_CHARACTERISTIC_UUID_RX, properties: [.read, .write, .notify], value: nil, permissions: [.readable, .writeable]) let myService = CBMutableService(type: BLE_SERVICE_UUID, primary: true) - - // 3. Add characteristics to the service myService.characteristics = [myCharacteristic] - - // 4. Add service to peripheralManager peripheralManager!.add(myService) - - // 5. Start advertising - peripheralManager!.startAdvertising([CBAdvertisementDataLocalNameKey : "RippleChat", CBAdvertisementDataServiceUUIDsKey : BLE_SERVICE_UUID]) + peripheralManager!.startAdvertising([CBAdvertisementDataLocalNameKey : "RippleChat", CBAdvertisementDataServiceUUIDsKey: [BLE_SERVICE_UUID]]) print("Started Advertising") } - -// func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) { -// -// messageLabel.text = "Data getting Read" -// readValueLabel.text = value -// -// // Perform your additional operations here -// -// } -// -// func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) { -// -// messageLabel.text = "Writing Data" -// -// if let value = requests.first?.value { -// writeValueLabel.text = value.hexEncodedString() -// //Perform here your additional operations on the data you get -// } -// } } diff --git a/RippleChat/ContentView.swift b/RippleChat/ContentView.swift index dcbc02c..bbd95d3 100644 --- a/RippleChat/ContentView.swift +++ b/RippleChat/ContentView.swift @@ -11,6 +11,8 @@ import CoreBluetooth struct ContentView: View { @State var currentView = 0 @EnvironmentObject var dataStore: DataStore + @StateObject private var bluetoothController = BluetoothController() + @StateObject private var bluetoothPeripheral = BluetoothPeripheral() @Environment(\.scenePhase) private var scenePhase let saveAction: ()->Void @@ -20,12 +22,20 @@ struct ContentView: View { case 0: PeeringView() .environmentObject(dataStore) + .environmentObject(bluetoothController) + .environmentObject(bluetoothPeripheral) + .navigationTitle("Peering") case 1: FeedListView() .environmentObject(dataStore) + .environmentObject(bluetoothController) + .environmentObject(bluetoothPeripheral) + .navigationTitle("Feeds") case 2: SettingsView() .environmentObject(dataStore) + .environmentObject(bluetoothController) + .environmentObject(bluetoothPeripheral) .navigationTitle("Settings") default: FeedListView() diff --git a/RippleChat/Views/FeedListView.swift b/RippleChat/Views/FeedListView.swift index 41d5bf2..8f6107e 100644 --- a/RippleChat/Views/FeedListView.swift +++ b/RippleChat/Views/FeedListView.swift @@ -26,6 +26,7 @@ struct FeedListView: View { } } } + .navigationTitle("Feeds") NewFeedEntryView() } } diff --git a/RippleChat/Views/PeeringView.swift b/RippleChat/Views/PeeringView.swift index 6be1871..96a1ef9 100644 --- a/RippleChat/Views/PeeringView.swift +++ b/RippleChat/Views/PeeringView.swift @@ -8,17 +8,16 @@ import SwiftUI struct PeeringView: View { - @ObservedObject private var bluetoothController = BluetoothController() - @ObservedObject private var bluetoothPeripheral = BluetoothPeripheral() @EnvironmentObject var dataStore: DataStore + @EnvironmentObject var btController: BluetoothController + @EnvironmentObject var btPeripheral: BluetoothPeripheral var body: some View { - Text("Peering View") NavigationStack { - List(bluetoothController.peripheralNames, id: \.self) { peripheral in + List(btController.peripheralNames, id: \.self) { peripheral in Text(peripheral) } - .navigationTitle("Peripherals") + .navigationTitle("Peering") .navigationViewStyle(StackNavigationViewStyle()) } } @@ -27,5 +26,7 @@ struct PeeringView: View { struct PeeringView_Previews: PreviewProvider { static var previews: some View { PeeringView() + .environmentObject(BluetoothPeripheral()) + .environmentObject(BluetoothController()) } }