在范围与SWIFT获取蓝牙设备 [英] Get bluetooth devices in range with SWIFT

查看:983
本文介绍了在范围与SWIFT获取蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个X code 6操场下面code。我想只得到OSX下启动了蓝牙,以及所有我想目前在范围内的设备的清单。

这似乎并没有在所有调用我的委托方法。

我是新来的OSX开发而迅速,所以要温柔。 :)

 进口可可
进口IOBluetooth类BlueDelegate:IOBluetoothDeviceInquiryDelegate {
    FUNC deviceInquiryComplete(发件人:IOBluetoothDeviceInquiry,错误:IOReturn,中止:布尔){
        中止
        VAR设备= sender.foundDevices()
        对于设备:机器AnyObject {
            如果让啄=设备作为? IOBluetoothDevice {
                thingy.getAddress()
            }
        }
    }
}VAR查询= IOBluetoothDeviceInquiry(代表:BlueDelegate())
inquiry.start()


解决方案

要告诉你的code做了背景中的游乐场,你必须进口XCPlayground 和呼叫 XCPSetExecutionShouldContinueIndefinitely()。结果
这将保持IOBluetoothDeviceInquiry活在操场上,并允许在完成以后调用委托方法。

 进口可可
进口IOBluetooth
进口XCPlayground类BlueDelegate:IOBluetoothDeviceInquiryDelegate {
    FUNC deviceInquiryComplete(发件人:IOBluetoothDeviceInquiry,错误:IOReturn,中止:布尔){
        中止
        的println(称为)
        VAR设备= sender.foundDevices()
        对于设备:机器AnyObject {
            如果让啄=设备作为? IOBluetoothDevice {
                thingy.getAddress()
            }
        }
    }
}VAR委托= BlueDelegate()
VAR查询= IOBluetoothDeviceInquiry(代表:代表)
inquiry.start()
XCPSetExecutionShouldContinueIndefinitely()

虽然上述方法是有效的,我觉得它更容易为那些需要像async- code,代表团...

概念任务创建简单的,传统的测试项目

So, I have the following code in a XCode 6 playground. I'm trying to just get started with Bluetooth under OSX, and all I want currently is a list of devices in range.

It doesn't seem to be calling my delegate method at all.

I'm new to OSX dev and swift, so be gentle. :)

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()

解决方案

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屋!

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