我是 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/