简体   繁体   English

多点连接 wifi 和蓝牙关闭

[英]Multipeer Connectivity wifi & bluetooth off

Is there any way in iOS to detect if Wifi & Bluetooth is off so that I can show alert to the user to enable it from settings. iOS 中有什么方法可以检测 Wifi 和蓝牙是否关闭,以便我可以向用户显示警报以从设置中启用它。

Note: I am interested in the hardware setting is on/off not the internet is reachable or not.注意:我对硬件设置是开/关感兴趣,而不是互联网是否可以访问。

//For Wifi Use SwiftReachability https://github.com/ashleymills/Reachability.swift //对于 Wifi 使用 SwiftReachability https://github.com/ashleymills/Reachability.swift

//declare this property where it won't go out of scope relative to your listener //声明此属性不会超出与您的侦听器相关的范围

 let reachability = try! Reachability()

//declare this inside of viewWillAppear

     NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
    do{
      try reachability.startNotifier()
    }catch{
      print("could not start reachability notifier")
    }

  @objc func reachabilityChanged(note: Notification) {
   let reachability = note.object as! Reachability
     switch reachability.connection {
  case .wifi:
      print("Reachable via WiFi")
  case .cellular:
      print("Reachable via Cellular")
  case .unavailable:
    print("Network not reachable")
  }
}

stopping notifications停止通知

reachability.stopNotifier()
NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)

// For bluetooth import CoreBluetooth // 对于蓝牙导入 CoreBluetooth

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

//BT Manager //BT经理

func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager)
{

    if peripheral.state == .poweredOn {
   // myBTManager!.startAdvertising(_broadcastBeaconDict)
    } else if peripheral.state == .poweredOff {
   // myBTManager!.stopAdvertising()
    } else if peripheral.state == .unsupported
    {
    } else if peripheral.state == .unauthorized
      {
      }
 }

You can try the following for network connection availability您可以尝试以下网络连接可用性

func isInternetAvailable() -> Bool {
var isNetworkReachable = false
if let reachability: Reachability = try? Reachability() {
    isNetworkReachable = reachability.connection != .unavailable
}
return isNetworkReachable

} }

for BLE低功耗蓝牙

class BLEManager: NSObject, CBCentralManagerDelegate {

var bleManagerDelegate: BLEManagerDelegate?
var bluetoothManager: CBCentralManager?

private override init() {
        super.init()
    bleManager = CBCentralManager(delegate: self, queue:nil,
                                        options:[CBCentralManagerOptionShowPowerAlertKey: true])
}

//CBCentralManagerDelegate method invoked
func centralManagerDidUpdateState(_ central: CBCentralManager) {
        var bleStateError: BLEError  = .kBLEStateUnknown
            switch central.state {
         case .poweredOff:
         // powered Off
         case .poweredOn:
         // powered On
             case .unknown:
             //
         case .resetting:
             //
         case .unsupported:
         //
             case .unauthorized:
             //
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM