当前位置: 首页 > 面试题库 >

列出Swift中蓝牙设备范围内的设备

富涛
2023-03-14
问题内容

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

尽管上述方法有效,但我发现为需要异步代码,委托等概念的任务创建简单的传统测试项目更加容易。



 类似资料:
  • 我有两部Android手机。我想通过蓝牙在他们之间建立自动连接。例如 我的Android手机与另一个蓝牙配对。当我把这些手机放在一起时,它们需要检测蓝牙设备,并自动连接到选定的Android手机(已知地址/MAC/之前配对)。我不需要再连接它。我想在我的Android应用程序中实现这种连接。 我在谷歌上找到了一些相关的参考资料,但他们都还没有解决这个问题。我认为我需要创建一个线程/服务,当蓝牙在范

  • 问题内容: 我正在寻找一种以编程方式列出我的设备所找到的附近任何蓝牙设备(可发现)的方法。我无法找到有关在Swift3.0中执行此调用的任何信息或教程。这篇QA帖子讨论了使用Swift 1.0查找这些设备并在Xcode 6(而不是最新版本8)中进行构建。 我尽力将代码从1.0转换为3.0语法,但是在运行以下代码时,Playground不返回任何内容: 问题答案: 正确使用IOBluetooth 下

  • deviceName String deviceId String

  • deviceName String 设备名称 deviceId String 设备Id BluetoothDevice Object deviceName String deviceId String

  • deviceName String 设备名称 deviceId String 设备Id

  • 连接设备 接口说明 用于连接 扫描出来的蓝牙设备。 参数说明 字段 类型 必须? 说明 device RKBLEDevice 是 蓝牙设备 示例代码 Swift: RokidMobileSDK.binder.connect(device: RKBLEDevice) Objc: [RokidMobileSDK.binder connect:device]; 断开设备 接口说明 用于断开已经连接的