我对快速语言还不熟悉。我一直在努力在我的iOS应用程序和条形码扫描仪之间建立蓝牙连接。条形码扫描仪已启用蓝牙。我试着用我的iphone建立一个全球蓝牙连接,结果成功了。基于一些来自Internet的参考资料,我编写了以下示例代码。

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)")
}

当我尝试运行上述代码时,日志显示“Bluetooth已打开”,就这样。它没有发现我的条形码扫描仪。我还确保条形码扫描仪处于可发现模式。为什么我的代码没有发现支持蓝牙的条码扫描器?我在上面的代码中犯了什么错误吗?
谢谢

最佳答案

你一定要先检查授权!

10-04 13:18