feat: Simple bluetooth status indication
This commit is contained in:
23
Sources/scoobytag/BluetoothManager.swift
Normal file
23
Sources/scoobytag/BluetoothManager.swift
Normal file
@@ -0,0 +1,23 @@
|
||||
import SwiftUI
|
||||
import CoreBluetooth
|
||||
|
||||
final class BluetoothManager: NSObject, ObservableObject {
|
||||
private var centralManager: CBCentralManager!
|
||||
@Published var status = "Starting..."
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
centralManager = CBCentralManager(delegate: self, queue: nil)
|
||||
}
|
||||
}
|
||||
|
||||
extension BluetoothManager: CBCentralManagerDelegate {
|
||||
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
||||
switch central.state {
|
||||
case .poweredOn:
|
||||
status = "Bluetooth is on"
|
||||
default:
|
||||
status = "Bluetooth unavailable"
|
||||
}
|
||||
}
|
||||
}
|
||||
16
Sources/scoobytag/ContentView.swift
Normal file
16
Sources/scoobytag/ContentView.swift
Normal file
@@ -0,0 +1,16 @@
|
||||
import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
@EnvironmentObject var bluetoothManager: BluetoothManager
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Image(systemName: "globe")
|
||||
.imageScale(.large)
|
||||
.foregroundStyle(.tint)
|
||||
Text("Hello, world!")
|
||||
Text(bluetoothManager.status)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
13
Sources/scoobytag/ScoobyTagApp.swift
Normal file
13
Sources/scoobytag/ScoobyTagApp.swift
Normal file
@@ -0,0 +1,13 @@
|
||||
import SwiftUI
|
||||
import CoreBluetooth
|
||||
|
||||
@main
|
||||
struct ScoobyTagApp: App {
|
||||
@StateObject private var bluetoothManager = BluetoothManager()
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
.environmentObject(bluetoothManager)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user