CoreBluetooth didDiscoverPeripheral 没有在 Swift 中被调用 [英] CoreBluetooth didDiscoverPeripheral not being called in Swift

查看:19
本文介绍了CoreBluetooth didDiscoverPeripheral 没有在 Swift 中被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am 99% sure I followed the instructions to setup CoreBluetooth correctly. No matter what I do, when I run this app on my iPad mini, the Bluetooth is saying its on. It's saying it is scanning for devices, but it is absolutely not finding any devices. If I go to the Bluetooth menu on the device I do see other devices being discovered. I initialize the CBCentralManager. I setup centralManagerDidUpdateState. When that is sure the bluetooth is ready it calls centralManager.scanForPeripheralsWithServices. All this is happening correctly. But my delegate function centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) never ever gets called. My code is very simple. Maybe I am missing something but I was able to confirm that my Macbook is a BTLE device and my ipad mini is a BTLE device as well. Here is my code.

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 做到这一点,它就像我写的那样工作它.

I had to get my macbook advertising. Once I did that using https://github.com/mttrb/BeaconOSX it worked exactly as I had written it.

这篇关于CoreBluetooth didDiscoverPeripheral 没有在 Swift 中被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆