Xen*_*Yan 8 macos bluetooth iobluetooth swift
我在Xcode 6游乐场中有以下代码:
import Cocoa
import IOBluetooth
class BlueDelegate : IOBluetoothDeviceInquiryDelegate {
func deviceInquiryComplete(sender: IOBluetoothDeviceInquiry, error: IOReturn, aborted: Bool) {
aborted
var devices = sender.foundDevices()
for device : AnyObject in devices {
if let thingy = device as? IOBluetoothDevice {
thingy.getAddress()
}
}
}
}
var inquiry = IOBluetoothDeviceInquiry(delegate: BlueDelegate())
inquiry.start()
Run Code Online (Sandbox Code Playgroud)
我刚开始使用OSX下的蓝牙,而我目前想要的只是范围内的设备列表.
它似乎根本没有调用我的委托方法.
我是OSX开发和Swift的新手,所以请保持温和.:)
要告诉Playground您的代码在后台执行某些操作,您必须import XCPlayground
并致电XCPSetExecutionShouldContinueIndefinitely()
.
这将使IOBluetoothDeviceInquiry在Playground中保持活动状态,并允许它在完成时调用委托方法.
import Cocoa
import IOBluetooth
import XCPlayground
class BlueDelegate : IOBluetoothDeviceInquiryDelegate {
func deviceInquiryComplete(sender: IOBluetoothDeviceInquiry, error: IOReturn, aborted: Bool) {
aborted
println("called")
var devices = sender.foundDevices()
for device : AnyObject in devices {
if let thingy = device as? IOBluetoothDevice {
thingy.getAddress()
}
}
}
}
var delegate = BlueDelegate()
var inquiry = IOBluetoothDeviceInquiry(delegate: delegate)
inquiry.start()
XCPSetExecutionShouldContinueIndefinitely()
Run Code Online (Sandbox Code Playgroud)
虽然上述方法有效,但我发现为需要像异步代码,委托等概念的任务创建简单的传统测试项目更容易.
归档时间: |
|
查看次数: |
11445 次 |
最近记录: |