gpt4 book ai didi

ios - scanForPeripheralWithServices 不返回任何东西

转载 作者:行者123 更新时间:2023-11-28 08:53:43 25 4
gpt4 key购买 nike

我是 swift 语言的新手。我一直致力于在我的 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)")
}

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

谢谢

最佳答案

您绝对应该先检查授权!

关于ios - scanForPeripheralWithServices 不返回任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33622461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com