【问题标题】:Detecting if Wifi or Bluetooth is turned on or off by the user [closed]检测用户是否打开或关闭了 Wifi 或蓝牙 [关闭]
【发布时间】:2015-01-21 19:25:15
【问题描述】:

我们如何确定是否使用 Swift 语言打开/关闭了蓝牙或 Wifi?

我的应用程序使用蓝牙或 Wifi 与其他设备进行通信。我们对这些通信没有任何问题,但如果 Wifi 和/或蓝牙关闭(当用户使用应用程序时),我们想通知用户。我无法在 Swift 中做到这一点。

【问题讨论】:

    标签: swift bluetooth wifi


    【解决方案1】:

    对于 iOS 中的蓝牙,您有 CBPeripheralManager(在 CoreBluetooth 框架中)。要检查蓝牙连接,请将您的类声明为 CBPeripheralManager 的委托,然后创建一个局部变量:

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

    然后,您的类必须实现回调,以便在启用或禁用蓝牙时引起注意。下面的代码是从我的项目中提取的,用于 Beacon 管理器

    //BT Manager
        func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
            println(__FUNCTION__)
            if peripheral.state == CBPeripheralManagerState.PoweredOn {
                println("Broadcasting...")
                //start broadcasting
                myBTManager!.startAdvertising(_broadcastBeaconDict)
            } else if peripheral.state == CBPeripheralManagerState.PoweredOff {
                println("Stopped")
                myBTManager!.stopAdvertising()
            } else if peripheral.state == CBPeripheralManagerState.Unsupported {
                println("Unsupported")
            } else if peripheral.state == CBPeripheralManagerState.Unauthorized {
                println("This option is not allowed by your application")
            }
         }
    

    对于 Wifi,看看这个 Github:https://github.com/ashleymills/Reachability.swift

    【讨论】:

    • 其实,如果我们能进入系统设置并读取当前设置,那就太好了
    • 此 API 现已弃用。请立即使用 CBCentralManagerDelegate。
    • @AshleyMills 。 “Reachability.swift”有蓝牙检测吗?
    • @mustaq 不,它没有。它具有与 Apple 的 Objective-C 可达性库相同的基本功能
    猜你喜欢
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多