Added BT Controller & Peripheral as EnvironmentObjects

This commit is contained in:
severin.memmishofer 2023-07-13 16:00:02 +02:00
parent 2ca1465ce9
commit d2b578dbad
4 changed files with 27 additions and 45 deletions

View File

@ -21,64 +21,34 @@ class BluetoothPeripheral: NSObject, ObservableObject {
} }
} }
// TODO: Change variable names, etc...
extension BluetoothPeripheral: CBPeripheralManagerDelegate { extension BluetoothPeripheral: CBPeripheralManagerDelegate {
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) { func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
switch peripheral.state { switch peripheral.state {
case .unknown: case .unknown:
print("Bluetooth Device is UNKNOWN") print("BT Device is UNKNOWN")
case .unsupported: case .unsupported:
print("Bluetooth Device is UNSUPPORTED") print("BT Device is UNSUPPORTED")
case .unauthorized: case .unauthorized:
print("Bluetooth Device is UNAUTHORIZED") print("BT Device is UNAUTHORIZED")
case .resetting: case .resetting:
print("Bluetooth Device is RESETTING") print("BT Device is RESETTING")
case .poweredOff: case .poweredOff:
print("Bluetooth Device is POWERED OFF") print("BT Device is POWERED OFF")
case .poweredOn: case .poweredOn:
print("Bluetooth Device is POWERED ON") print("BT Device is POWERED ON")
addServices() addBTService()
@unknown default: @unknown default:
fatalError() fatalError()
} }
} }
func addServices() { func addBTService() {
let myCharacteristic = CBMutableCharacteristic(type: BLE_CHARACTERISTIC_UUID_RX, properties: [.read, .write, .notify], value: nil, permissions: [.readable, .writeable])
let myCharacteristic = CBMutableCharacteristic(type: BLE_CHARACTERISTIC_UUID_RX, properties: [.read, .write, .notify], value: nil, permissions: [.readable])
// 2. Create instance of CBMutableService
let myService = CBMutableService(type: BLE_SERVICE_UUID, primary: true) let myService = CBMutableService(type: BLE_SERVICE_UUID, primary: true)
// 3. Add characteristics to the service
myService.characteristics = [myCharacteristic] myService.characteristics = [myCharacteristic]
// 4. Add service to peripheralManager
peripheralManager!.add(myService) peripheralManager!.add(myService)
peripheralManager!.startAdvertising([CBAdvertisementDataLocalNameKey : "RippleChat", CBAdvertisementDataServiceUUIDsKey: [BLE_SERVICE_UUID]])
// 5. Start advertising
peripheralManager!.startAdvertising([CBAdvertisementDataLocalNameKey : "RippleChat", CBAdvertisementDataServiceUUIDsKey : BLE_SERVICE_UUID])
print("Started Advertising") 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
// }
// }
} }

View File

@ -11,6 +11,8 @@ import CoreBluetooth
struct ContentView: View { struct ContentView: View {
@State var currentView = 0 @State var currentView = 0
@EnvironmentObject var dataStore: DataStore @EnvironmentObject var dataStore: DataStore
@StateObject private var bluetoothController = BluetoothController()
@StateObject private var bluetoothPeripheral = BluetoothPeripheral()
@Environment(\.scenePhase) private var scenePhase @Environment(\.scenePhase) private var scenePhase
let saveAction: ()->Void let saveAction: ()->Void
@ -20,12 +22,20 @@ struct ContentView: View {
case 0: case 0:
PeeringView() PeeringView()
.environmentObject(dataStore) .environmentObject(dataStore)
.environmentObject(bluetoothController)
.environmentObject(bluetoothPeripheral)
.navigationTitle("Peering")
case 1: case 1:
FeedListView() FeedListView()
.environmentObject(dataStore) .environmentObject(dataStore)
.environmentObject(bluetoothController)
.environmentObject(bluetoothPeripheral)
.navigationTitle("Feeds")
case 2: case 2:
SettingsView() SettingsView()
.environmentObject(dataStore) .environmentObject(dataStore)
.environmentObject(bluetoothController)
.environmentObject(bluetoothPeripheral)
.navigationTitle("Settings") .navigationTitle("Settings")
default: default:
FeedListView() FeedListView()

View File

@ -26,6 +26,7 @@ struct FeedListView: View {
} }
} }
} }
.navigationTitle("Feeds")
NewFeedEntryView() NewFeedEntryView()
} }
} }

View File

@ -8,17 +8,16 @@
import SwiftUI import SwiftUI
struct PeeringView: View { struct PeeringView: View {
@ObservedObject private var bluetoothController = BluetoothController()
@ObservedObject private var bluetoothPeripheral = BluetoothPeripheral()
@EnvironmentObject var dataStore: DataStore @EnvironmentObject var dataStore: DataStore
@EnvironmentObject var btController: BluetoothController
@EnvironmentObject var btPeripheral: BluetoothPeripheral
var body: some View { var body: some View {
Text("Peering View")
NavigationStack { NavigationStack {
List(bluetoothController.peripheralNames, id: \.self) { peripheral in List(btController.peripheralNames, id: \.self) { peripheral in
Text(peripheral) Text(peripheral)
} }
.navigationTitle("Peripherals") .navigationTitle("Peering")
.navigationViewStyle(StackNavigationViewStyle()) .navigationViewStyle(StackNavigationViewStyle())
} }
} }
@ -27,5 +26,7 @@ struct PeeringView: View {
struct PeeringView_Previews: PreviewProvider { struct PeeringView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
PeeringView() PeeringView()
.environmentObject(BluetoothPeripheral())
.environmentObject(BluetoothController())
} }
} }