如何使用Swift 3打开/关闭蓝牙?

时间:2017-01-09 10:50:36

标签: ios swift bluetooth

我是Swift的新手,并尝试学习与蓝牙和wifi通信。

我经历了许多在线教程,但他们似乎只教授如何启动centralDeviceManager并扫描并连接到设备,还检查状态,但我无法找到如何启用/禁用蓝牙。我在viewController.swift文件中得到了这个。任何人都可以建议我如何启用它。

import CoreBluetooth

class ProfileListViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,CBPeripheralDelegate,CBCentralManagerDelegate {

var manager:CBCentralManager!
    var peripheral:CBPeripheral!
    let BEAN_NAME = "Robu"
    let BEAN_SCRATCH_UUID =
        CBUUID(string: "bb2819df-f8ef-4f90-811e-20a4f53eb949")
    let BEAN_SERVICE_UUID =
        CBUUID(string: "bb2819df-f8ef-4f90-811e-20a4f53eb949")

func centralManagerDidUpdateState(_ central: CBCentralManager){
        print("CentralManager is initialized")

        switch central.state{
        case CBManagerState.unauthorized:
            print("The app is not authorized to use Bluetooth low energy.")
        case CBManagerState.poweredOff:
            print("Bluetooth is currently powered off.")
        case CBManagerState.poweredOn:
            print("Bluetooth is currently powered on and available to use.")
        default:break
        }
    }
}

1 个答案:

答案 0 :(得分:1)

Apple不允许以编程方式启用或禁用蓝牙。

最佳做法是在需要蓝牙时显示提醒,并要求用户将其打开。