ios - 没有在 Swift 中调用 CoreBluetooth didDiscoverPeripheral

标签 ios bluetooth swift bluetooth-lowenergy core-bluetooth

我 99% 确定我按照说明正确设置了 CoreBluetooth。无论我做什么,当我在我的 iPad mini 上运行这个应用程序时,蓝牙都会显示它已打开。它说它正在扫描设备,但绝对找不到任何设备。如果我转到设备上的蓝牙菜单,我会看到其他设备被发现。我初始化 CBCentralManager。我设置了 centralManagerDidUpdateState。当确定蓝牙已准备就绪时,它会调用 centralManager.scanForPeripheralsWithServices。所有这一切都在正确发生。但是我的委托(delegate)函数 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 做广告。一旦我使用 https://github.com/mttrb/BeaconOSX 做到了这一点它完全按照我写的那样工作。

关于ios - 没有在 Swift 中调用 CoreBluetooth didDiscoverPeripheral,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25039103/

相关文章:

ios - 使用 Swift 在 Parse 中同步保存

swift - NSTableView/NSOutlineView 重新加载指定行

c# - 如何将值传递给上一个 ViewController

ios - 在 ios 中使用 Alamofire 在 collectionView 中显示图像

android - 在android应用程序的Onclick监听器方法之外调用方法

C# 控制台应用程序通过蓝牙与 Arduino 对话

ios - 如何从 nsarray 的 nsarray 中搜索特定联系人

iphone - 为 iOS 启动图像使用自定义文件名

ios - armv6 和 armv7 的条件编译

android - 使用蓝牙 socket 处理方向变化的最佳方法