Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- import CoreBluetooth
- struct ContentView: View {
- @StateObject var bleManager = BLEManager()
- @State var selectedPeripheral: CBPeripheral?
- @State var showPeriSearch = false
- @State var loaded: NetworkSettings?
- var body: some View {
- NavigationView{
- VStack{
- if(selectedPeripheral == nil){
- Text("Es wurde noch kein LovePi ausgewählt!")
- }else if(loaded != nil){
- Form{
- Section(header: Text("Netzwerkeinstellungen")){
- HStack{
- Text("WiFi-Name")
- Spacer()
- Text(bleManager.settingData!.ssid)
- }
- HStack{
- Text("WiFi-Passwort")
- Spacer()
- Text(bleManager.settingData!.psk)
- }
- }
- }
- }else{
- Button("Laden...", action: {
- loaded = bleManager.settingData
- })
- }
- }
- .navigationTitle("LovePi")
- .toolbar{
- ToolbarItem(placement: .navigationBarTrailing){
- Button("Suchen", action: {
- showPeriSearch = true
- })
- }
- }
- .sheet(isPresented: $showPeriSearch, onDismiss: {bleManager.stopScanning();dismissed()}){
- NavigationView{
- List(bleManager.peripherals, id: \.name){peri in
- Button(action:{
- selectedPeripheral = peri
- showPeriSearch = false
- }){
- VStack{
- Text(peri.name ?? "Unbenannt")
- if(peri.services != nil){
- VStack{
- ForEach(peri.services!, id: \.self){service in
- Text(service.uuid.uuidString)
- }
- }
- }
- }
- }
- }
- .navigationTitle("LovePi auswählen")
- .navigationBarTitleDisplayMode(.inline)
- .onAppear(perform: {
- bleManager.startScanning()
- })
- }
- }
- }
- }
- func dismissed(){
- if(selectedPeripheral != nil){
- print("Discover devices")
- bleManager.connect(peripheral: selectedPeripheral!)
- }
- }
- }
RAW Paste Data
Copied