У меня есть программа на swiftui, использующая BLE. Но когда я хочу узнать статус centralManager (мой iPhone), он отображает неизвестный. Я не знаю, есть ли ошибка в моем коде или проблема в моем телефоне.
Мой Contentview:
import SwiftUI
import CoreBluetooth
struct ContentView: View {
var body: some View {
VStack {
Text("View").background(Color.init(red: 56, green: 22, blue: 22))
}
.onAppear(perform: connectBLEDevice)
}
private func connectBLEDevice(){
print("connectBLEDevice")
let ble = BLEConnection()
ble.startCentralManager()
}
}
Мой класс для BLE:
import CoreBluetooth
class BLEConnection: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
var centralManager: CBCentralManager!
var myPeripheral: CBPeripheral!
func startCentralManager() {
self.centralManager = CBCentralManager(delegate: self, queue: nil)
print("Central Manager State: \(self.centralManager.state)")
centralManagerDidUpdateState(self.centralManager)
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch (central.state) {
case .unsupported:
print("BLE is Unsupported")
break
case .unauthorized:
print("BLE is Unauthorized")
break
case .unknown:
print("BLE is Unknown")
break
case .resetting:
print("BLE is Resetting")
break
case .poweredOff:
print("BLE is Powered Off")
break
case .poweredOn:
print("BLE powered on")
break
@unknown default:
break
}
}
}