Detecting if Wifi or Bluetooth is turned on or off by the user [closed]

↘锁芯ラ 提交于 2019-12-30 07:06:08

问题


How can we determine if Bluetooth or Wifi was turned on/off using the Swift language?

My application uses Bluetooth or Wifi to communicate with other devices. We have no problem with these communications, but we would like to inform the user if Wifi and/or Bluetooth is turned off (when the user is using the application). I haven't been able to do this in Swift.


回答1:


For Bluetooth in iOS, you have CBPeripheralManager (in CoreBluetooth Framework). To check for bluetooth connection, you declare your class as delegate of CBPeripheralManager then create a local variable:

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

then, your class must implement the callback to get noticed when your Bluetooth is enabled or disabled. The code below is extracted from my project which is for 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")
        }
     }

And for Wifi, take a look at this Github: https://github.com/ashleymills/Reachability.swift



来源:https://stackoverflow.com/questions/28075198/detecting-if-wifi-or-bluetooth-is-turned-on-or-off-by-the-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!