检测用户是否打开或关闭了Wifi或蓝牙

3

如何使用Swift语言确定蓝牙或Wifi是否已开启/关闭?

我的应用程序使用蓝牙或Wifi与其他设备通信。我们在这些通信方面没有问题,但是我们希望在用户使用应用程序时通知用户是否已关闭Wifi和/或蓝牙。我无法在Swift中实现此操作。

1个回答

5
在iOS中,针对蓝牙技术,你可以使用CoreBluetooth框架中的CBPeripheralManager。要检查蓝牙连接状态,需将你的类声明为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


实际上,如果我们能够访问系统设置并读取当前设置,那将是非常好的。 - Hope
http://github.com/ashleymills/Reachability.swift 现在支持 CocoaPods。 - Ashley Mills
此 API 已被弃用,请使用 CBCentralManagerDelegate。 - Tripti Kumar
@AshleyMills ,“Reachability.swift” 是否具有蓝牙检测功能? - mustaq
@mustaq 不,它没有。它具有与苹果的Objective-C可达性库相同的基本功能。 - Ashley Mills

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接