ios - 无法让 Core Bluetooth 在 Swift iOS playground 中工作

标签 ios swift bluetooth

我是 swift 的新手。我无法在 Playground 上获得对 centralManagerDidUpdateState::的回调(即:我认为初始化会回调到 centralManagerDidUpdateState):

import CoreBluetooth
class BTDiscovery:NSObject,
CBCentralManagerDelegate {

    func centralManagerDidUpdateState(central: CBCentralManager!) {
        println("here")
    }
}

var bt = BTDiscovery()

iOS Swift playground 是否支持 Core Bluetooth? 我在 OSX playground 和 IOBluetooth 上试过这个。这也没有用。我做错了什么?

谢谢。

最佳答案

我认为您遇到的是 playground 本质上是同步的,而蓝牙发现是异步的。为了让它工作,你需要在你的 Playground 上添加一些东西来允许异步操作:

import XCPlayground
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

另请注意,由于 iOS playground 是在模拟器上运行的,所以我根本不希望 CB 在那里工作。

您还有更根本的问题,因为您没有做任何实际触发发现的事情。您需要创建一个 CBCentralManager 实例并使用它来驱动发现过程:

import Cocoa
import XCPlayground
import CoreBluetooth

class BTDiscovery:NSObject, CBCentralManagerDelegate {

    func centralManagerDidUpdateState(central: CBCentralManager!) {
        println("here")
    }

}

var bt = BTDiscovery()
var central = CBCentralManager(delegate: bt, queue: dispatch_get_main_queue())

XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

关于ios - 无法让 Core Bluetooth 在 Swift iOS playground 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27844397/

相关文章:

swift - 状态栏外观无法正常工作 - 使用 iOS 13 深色模式

linux - Bluez 5.48 - 如何强制蓝牙传输状态从事件到空闲 - 嵌入式接收器端

iphone - 使用 dequeueReusableCellWithReuseIdentifier 的标识符问题

iOS 应用程序 - 仅在 iOS13 上的“Today Widget”和“通知扩展”中不显示数据

ios - 如何支持具有动态类型标识符但具有特定扩展名的文件?

ios - 如果应用程序直接从后台启动而不是从通知消息启动,如何知道推送消息信息?

ios - SwiftUI - 从 subview 转换回主视图后删除导航栏中的多余空间

bash - 向应用程序发送命令并通过 bash 中的文件描述符读取其输出

c# - 如何通过蓝牙将字体发送到 Zebra 打印机

ios - 如何解决xcode 5 中的apple LLVM 5.0 错误?