列出Swift中蓝牙设备范围内的设备 [英] List devices that are in range of Bluetooth device in 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()
在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()
虽然上述方法可行,但我发现为需要异步代码,委托等概念的任务创建简单的传统测试项目更加容易.
I have the following code in a Xcode 6 playground:
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()
I'm just getting started with Bluetooth under OSX, and all I currently would like is a list of devices in range.
It doesn't seem to be calling my delegate method at all.
I'm new to OSX development and Swift, so be gentle. :)
To tell a Playground that your code does something in the background, you have to import XCPlayground
and call XCPSetExecutionShouldContinueIndefinitely()
.
This will keep the IOBluetoothDeviceInquiry alive in the Playground and allow it to call the delegate method when finished.
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()
While the above approach works, I find it easier to create simple, traditional test projects for tasks that need concepts like async-code, delegation, ...
这篇关于列出Swift中蓝牙设备范围内的设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!