CoreBluetooth确实没有在Swift中调用DisodheralPeripheral

Joh*_*ton 7 bluetooth ios core-bluetooth bluetooth-lowenergy swift

我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.
    }

}
Run Code Online (Sandbox Code Playgroud)

Joh*_*ton 2

我必须给我的 MacBook 做广告。当我使用https://github.com/mttrb/BeaconOSX完成此操作后,它的工作原理与我编写的完全一样。