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 {
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
// }
// }
}

View File

@ -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()

View File

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

View File

@ -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())
}
}