我们如何使用Swift语言确定是打开/关闭蓝牙还是Wifi?
我的应用程序使用蓝牙或Wifi与其他设备通信。我们对这些通信没有任何问题,但我们想告知用户Wifi和/或蓝牙是否已关闭(当用户使用该应用程序时)。我无法在Swift中做到这一点。
答案 0 :(得分:4)
对于iOS中的蓝牙,您有CBPeripheralManager(在CoreBluetooth Framework中)。要检查蓝牙连接,请将您的类声明为CBPeripheralManager的委托,然后创建一个局部变量:
var myBTManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
然后,您的班级必须实施回调,以便在启用或禁用蓝牙时获得注意。下面的代码是从我的Beacon manager
项目中提取的//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