【问题标题】:CoreBluetooth didDiscoverPeripheral not being called in SwiftCoreBluetooth didDiscoverPeripheral 没有在 Swift 中被调用
【发布时间】:2014-09-22 05:30:33
【问题描述】:

我 99% 确信我按照说明正确设置了 CoreBluetooth。不管我做什么,当我在我的 iPad mini 上运行这个应用程序时,蓝牙都在说它打开。它说它正在扫描设备,但绝对没有找到任何设备。如果我转到设备上的蓝牙菜单,我确实会看到其他设备正在被发现。我初始化CBCentralManager。我设置了centralManagerDidUpdateState。当确定蓝牙准备就绪时,它会调用centralManager.scanForPeripheralsWithServices。这一切都在正确地发生。但是我的委托函数centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) 永远不会被调用。我的代码非常简单。也许我遗漏了一些东西,但我能够确认我的 Macbook 是 BTLE 设备,而我的 ipad mini 也是 BTLE 设备。这是我的代码。

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate {

    var centralManager:CBCentralManager!
    var blueToothReady = false

    override func viewDidLoad() {
        super.viewDidLoad()
        startUpCentralManager()
    }

    func startUpCentralManager() {
        println("Initializing central manager")
        centralManager = CBCentralManager(delegate: self, queue: nil)
    }

    func discoverDevices() {
        println("discovering devices")
        centralManager.scanForPeripheralsWithServices(nil, options: nil)
    }

    func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
        println("Discovered \(peripheral.name)")
    }

    func centralManagerDidUpdateState(central: CBCentralManager!) {
        println("checking state")
        switch (central.state) {
            case .PoweredOff:
            println("CoreBluetooth BLE hardware is powered off")

            case .PoweredOn:
            println("CoreBluetooth BLE hardware is powered on and ready")
            blueToothReady = true;

            case .Resetting:
            println("CoreBluetooth BLE hardware is resetting")

            case .Unauthorized:
            println("CoreBluetooth BLE state is unauthorized")

            case .Unknown:
            println("CoreBluetooth BLE state is unknown");

            case .Unsupported:
            println("CoreBluetooth BLE hardware is unsupported on this platform");

        }
        if blueToothReady {
            discoverDevices()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

【问题讨论】:

  • 你的 MacBook 打广告吗?例如,如果您在 iPad Mini 上使用 LightBlue,您会找到其他设备吗?
  • 其实 Lightblue 没有。
  • 如果你的 MacBook 不做广告,你的 iPad 就找不到它。
  • 你是对的。一旦我得到了我的 macbook 广告,它就可以正常工作了。

标签: ios bluetooth swift bluetooth-lowenergy core-bluetooth


【解决方案1】:

我必须为我的 macbook 做广告。一旦我使用https://github.com/mttrb/BeaconOSX 完成此操作,它的工作方式与我编写的完全一样。

【讨论】:

    猜你喜欢
    • 2014-12-14
    • 1970-01-01
    • 2018-01-01
    • 2015-03-30
    • 2015-01-31
    • 2021-05-30
    • 1970-01-01
    • 2015-03-07
    • 2015-05-17
    相关资源
    最近更新 更多