如何检测用户的iPhone关闭或开启的蓝牙?

时间:2022-07-05 05:45:54

I am trying to detect is the Bluetooth of the user's iPhone On or Off. If it is Off I want to send a notification to the user to turn it On. So far I have done this:

我试图检测用户的iPhone开启或关闭的蓝牙。如果它是关我想发送通知给用户打开它。到目前为止,我已经这样做了:

import CoreBluetooth

  class ViewController: UIViewController, CLLocationManagerDelegate,AVCaptureMetadataOutputObjectsDelegate,CBManager {   

 var myBTManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)

  }

func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
    print(#function)
    if peripheral.state == CBManagerState.poweredOn {
        print("Broadcasting...")

    //    myBTManager!.startAdvertising(_broadcastBeaconDict)
    } else if peripheral.state == CBManagerState.poweredOff {
        print("Stopped")
        myBTManager!.stopAdvertising()
    } else if peripheral.state == CBManagerState.unsupported {
        print("Unsupported")
    } else if peripheral.state == CBManagerState.unauthorized {
        print("This option is not allowed by your application")
    }
}

But as You can see from the picture, something is wrong.如何检测用户的iPhone关闭或开启的蓝牙?

但正如你从图中可以看到的那样,出了点问题。

Would you please help me how to fix this issue, I am new to swift and CoreBluetooth technology. I am also using Reachability for detecting the Wi-Fi connection, so if it also works for Bluetooth, I would prefer to use Reachability then.

你能帮我解决一下这个问题,我是swift和CoreBluetooth技术的新手。我也使用Reachability检测Wi-Fi连接,所以如果它也适用于蓝牙,我宁愿使用Reachability。

2 个解决方案

#1


4  

  1. You should be implementing the protocol CBPeripheralManagerDelegate, so replace CBManager in your class definition line with CBPeripheralManagerDelegate.

    您应该实现协议CBPeripheralManagerDelegate,因此使用CBPeripheralManagerDelegate替换类定义行中的CBManager。

  2. In Swift 3, the signature of peripheralManagerDidUpdateState is now:

    在Swift 3中,peripheralManagerDidUpdateState的签名现在是:

    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager)
    
  3. You can't initialize the CBPeripheralManager at class creation time since self is only available after the class has been initialized. Instead, make your property:

    您无法在创建类时初始化CBPeripheralManager,因为self仅在类初始化后可用。相反,让你的财产:

    var myBTManager: CBPeripheralManager?
    

    and initialize it in viewDidLoad:

    并在viewDidLoad中初始化它:

    override func viewDidLoad() {
        ...       
        myBTManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
        ...
    }
    

#2


0  

You can use the CBCentralMangerDelegate method:

您可以使用CBCentralMangerDelegate方法:

    public func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {
            if central.state == .poweredOn {
                //Bluetooth is on
           } else if central.state == .poweredOff {
                //Bluetooth is off
           }
    }

#1


4  

  1. You should be implementing the protocol CBPeripheralManagerDelegate, so replace CBManager in your class definition line with CBPeripheralManagerDelegate.

    您应该实现协议CBPeripheralManagerDelegate,因此使用CBPeripheralManagerDelegate替换类定义行中的CBManager。

  2. In Swift 3, the signature of peripheralManagerDidUpdateState is now:

    在Swift 3中,peripheralManagerDidUpdateState的签名现在是:

    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager)
    
  3. You can't initialize the CBPeripheralManager at class creation time since self is only available after the class has been initialized. Instead, make your property:

    您无法在创建类时初始化CBPeripheralManager,因为self仅在类初始化后可用。相反,让你的财产:

    var myBTManager: CBPeripheralManager?
    

    and initialize it in viewDidLoad:

    并在viewDidLoad中初始化它:

    override func viewDidLoad() {
        ...       
        myBTManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
        ...
    }
    

#2


0  

You can use the CBCentralMangerDelegate method:

您可以使用CBCentralMangerDelegate方法:

    public func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {
            if central.state == .poweredOn {
                //Bluetooth is on
           } else if central.state == .poweredOff {
                //Bluetooth is off
           }
    }