简体   繁体   English

iOS:Homekit-如何检测设备中是否启用了蓝牙和WiFi服务?

[英]iOS : Homekit - How to detect Bluetooth & WiFi service enable in device or not?

Home application show the below alert when we are trying to add accessory. 当我们尝试添加附件时,家庭应用程序会显示以下警报。

I have also used the HomeKit framwork in my application and want to show the alert when user try to add accessory. 我还在应用程序中使用了HomeKit框架,并希望在用户尝试添加附件时显示警报。

What kind of changes i need to do to show the same alert in app? 我需要做哪些更改才能在应用程序中显示相同的警报?

Screenshot of Home App Home App的屏幕截图

For Bluetooth in iOS, you have CBPeripheralManager (in CoreBluetooth Framework). 对于iOS中的蓝牙,您具有CBPeripheralManager(在CoreBluetooth Framework中)。 To check for bluetooth connection, you declare your class as delegate of CBPeripheralManager then create a local variable: 要检查蓝牙连接,请声明您的类为CBPeripheralManager的委托,然后创建一个本地变量:

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 下面的代码是从我的项目中提取的,该项目适用于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 对于Wifi,请看一下这个Github: https : //github.com/ashleymills/Reachability.swift

Source Answer : - Detecting if Wifi or Bluetooth is turned on or off by the user 来源答案:- 检测用户是否打开或关闭了Wifi或蓝牙

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

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