scanForPeripheralWithServices不返回任何内容

时间:2015-11-10 03:38:54

标签: ios swift bluetooth

我是快速语言的新手。我一直在努力在我的iOS应用程序和条形码扫描仪之间建立蓝牙连接。条形码扫描仪启用了蓝牙功能。我试图与我的iPhone建立全球蓝牙连接,它的工作原理。根据互联网上的一些参考资料,我编写了以下示例代码。

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {

var manager : CBCentralManager!

override func viewDidLoad() {
    super.viewDidLoad()
    manager = CBCentralManager(delegate : self, queue : nil)

}

func centralManagerDidUpdateState(central: CBCentralManager) {

    var consoleMsg = "hello"

    switch(central.state) {

    case .PoweredOff:
        consoleMsg = "Bluetooth is powered off"

    case .PoweredOn:
        consoleMsg = "Bluetooth is powered on"
        manager.scanForPeripheralsWithServices(nil, options: nil)

    case .Resetting:
        consoleMsg = "Bluetooth is Resetting"

    case .Unauthorized:
        consoleMsg = "Bluetooth is Unauthorized"

    case .Unknown:
        consoleMsg = "Bluetooth is Unknown"            

    case .Unsupported:
        consoleMsg = "Bluetooth is Unsupported"

    }

    print("\(consoleMsg)")
}

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {

    print("Discovered a peripheral \(peripheral)")
}

当我尝试运行上面的代码时,日志显示"蓝牙已启动"就是这样。它没有发现我的条形码扫描仪。我还确保条形码扫描仪处于可发现模式。为什么我的代码没有发现附近的蓝牙启用条码扫描器?我在上面的代码中做过任何错误吗?

由于

1 个答案:

答案 0 :(得分:0)

您应该首先检查授权!