检查蓝牙是否打开时的iOS问题

xxls0lw8  于 2022-09-19  发布在  iOS
关注(0)|答案(3)|浏览(170)

我需要检查一下蓝牙是否打开了。

我使用以下代码:

func startDetectingBluetoothState() {
        if self.centralManager == nil {
            self.centralManager = CBCentralManager(delegate: self, queue: self.workingQueue, options: [CBCentralManagerOptionShowPowerAlertKey: false])
        }
    }

func centralManagerDidUpdateState(_ central: CBCentralManager) {
        let state = central.state
        DispatchQueue.main.async { [weak self] in
            // notify observers about state change
            self?.stateObservers.invokeDelegates { stateObserver in
                stateObserver.bluetoothStateChanged()
            }
        }
    }

我在iPhone X 11.3上启动了应用程序(设置中打开了蓝牙),然后它几乎立即进入centralManagerDidUpdateState(_ central: CBCentralManager)state == .poweredOff。因此,这个值是错误的。然后我在设置中关闭蓝牙,没有调用centralManagerDidUpdateState(_ central: CBCentralManager)函数(因为它检测到的前一个状态是.PoweredOff),然后我打开蓝牙,使用.poweredOn值调用centralManagerDidUpdateState(_ central: CBCentralManager)。然后,它会正确地检测到每个状态变化。但如果我重新启动应用程序,并且蓝牙处于打开状态,它将无法再次检测到它。如果在启动过程中蓝牙关闭,则一切正常。

我有另一台设备iPhone 6+``11.2.5,一切都像魔咒一样工作:)

sy5wg1nm

sy5wg1nm1#

需要实现CBCentralManagerDelegate。以下是可能帮助您检查情况的示例代码-

var centralManager:CBCentralManager!

 in init() or viewDidLoad()
 {
     centralManager = CBCentralManager()
     centralManager.delegate = self
 }

func centralManagerDidUpdateState(_ central: CBCentralManager) {

  if central.state == .poweredOn {
     print("Bluetooth is connected")
  }
  else if central.state == .poweredOff{
     print("Bluetooth is not Connected.")
  }
}
llew8vvj

llew8vvj2#

使用此代码为您工作

var centralManager:CBCentralManager!

 override func viewDidLoad()
{
    super.viewDidLoad()

    // This will trigger centralManagerDidUpdateState
    centralManager = CBCentralManager(delegate: self, queue: nil)

}

 func centralManagerDidUpdateState(_ central: CBCentralManager)
{
    print("Central Manager did update state")

    if (central.state == .poweredOn)
    {

        self.centralManager?.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
    }
    else 
     {
        // Bluetooth is unavailable for some reason

        // Give feedback
        var message = String()
        switch central.state
        {
        case .unsupported:
            message = "Bluetooth is unsupported"
        case .unknown:
            message = "Bluetooth state is unkown"
        case .unauthorized:
            message = "Bluetooth is unauthorized"
        case .poweredOff:
            message = "Bluetooth is powered off"
        default:
            break
        }
        print(message)

        UIAlertView(
            title: "Bluetooth unavailable",
            message: message,
            delegate: nil,
            cancelButtonTitle: "OK")
            .show()
    }
}
juud5qan

juud5qan3#

这个代码起作用了。SWIFT 5版

typealias Completion = ((Any?)->Void)
class BLEManager: NSObject {
    static let shared = BLEManager()
    var manager: CBCentralManager?
    var completion: Completion?

    func enabled() -> Bool {
        if #available(iOS 13.0, *) {
            return CBCentralManager(delegate: nil, queue: nil, options: [CBCentralManagerOptionShowPowerAlertKey: false]).authorization == .allowedAlways
        }
        else {
            let manager = CBCentralManager(delegate: nil, queue: nil, options: [CBCentralManagerOptionShowPowerAlertKey: false])
            return manager.state != .poweredOn
        }
    }

    func requestAccess(_ completion: @escaping Completion) {
        self.completion = completion
        manager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionShowPowerAlertKey: true])
    }
}

extension BLEManager: CBCentralManagerDelegate {
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        completion?(central.state)
    }
}

相关问题