feat: BLE beacond
This commit is contained in:
@@ -2,22 +2,31 @@ import SwiftUI
|
||||
import CoreBluetooth
|
||||
|
||||
final class BluetoothManager: NSObject, ObservableObject {
|
||||
private var centralManager: CBCentralManager!
|
||||
private var periphManager: CBPeripheralManager!
|
||||
@Published var status = "Starting..."
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
centralManager = CBCentralManager(delegate: self, queue: nil)
|
||||
periphManager = CBPeripheralManager(delegate: self, queue: nil)
|
||||
}
|
||||
}
|
||||
|
||||
extension BluetoothManager: CBCentralManagerDelegate {
|
||||
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
||||
switch central.state {
|
||||
case .poweredOn:
|
||||
status = "Bluetooth is on"
|
||||
default:
|
||||
status = "Bluetooth unavailable"
|
||||
extension BluetoothManager: CBPeripheralManagerDelegate {
|
||||
func peripheralManagerDidUpdateState(_ central: CBPeripheralManager) {
|
||||
switch central.state {
|
||||
case .poweredOn:
|
||||
status = "Broadcasting"
|
||||
let uuid = CBUUID(string: "12345678-1234-1234-1234-123456789ABC")
|
||||
periphManager.startAdvertising(
|
||||
[
|
||||
CBAdvertisementDataServiceUUIDsKey: [uuid],
|
||||
CBAdvertisementDataLocalNameKey: "scbt-Test"
|
||||
])
|
||||
case .poweredOff:
|
||||
status = "Bluetooth off"
|
||||
|
||||
default:
|
||||
status = "Bluetooth unavailable"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user