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

使用Swift 3.0的附近蓝牙设备

谭彦
2023-03-14
问题内容

我正在寻找一种以编程方式列出我的设备所找到的附近任何蓝牙设备(可发现)的方法。我无法找到有关在Swift3.0中执行此调用的任何信息或教程。这篇QA帖子讨论了使用Swift
1.0查找这些设备并在Xcode 6(而不是最新版本8)中进行构建。

我尽力将代码从1.0转换为3.0语法,但是在运行以下代码时,Playground不返回任何内容:

import Cocoa
import IOBluetooth
import PlaygroundSupport

class BlueDelegate : IOBluetoothDeviceInquiryDelegate {
    func deviceInquiryComplete(_ sender: IOBluetoothDeviceInquiry, error: IOReturn, aborted: Bool) {
        aborted
        print("called")
        let devices = sender.foundDevices()
        for device : Any? in devices! {
            if let thingy = device as? IOBluetoothDevice {
                thingy.getAddress()
            }
        }
    }
}

var delegate = BlueDelegate()
var inquiry = IOBluetoothDeviceInquiry(delegate: delegate)
inquiry?.start()
PlaygroundPage.current.needsIndefiniteExecution = true

问题答案:

正确使用IOBluetooth

下面的代码在Xcode版本8.2.1(8C1002),Swift3.0中可以正常工作。不需要几行,例如的整个方法deviceInquiryStarted

更新:从Xcode 9.2(9B55)和Swift 4开始,这些用法仍然有效。

操场

import Cocoa
import IOBluetooth
import PlaygroundSupport
class BlueDelegate : IOBluetoothDeviceInquiryDelegate {
    func deviceInquiryStarted(_ sender: IOBluetoothDeviceInquiry) {
        print("Inquiry Started...")
//optional, but can notify you when the inquiry has started.
    }
    func deviceInquiryDeviceFound(_ sender: IOBluetoothDeviceInquiry, device: IOBluetoothDevice) {
        print("\(device.addressString!)")
    }
    func deviceInquiryComplete(_ sender: IOBluetoothDeviceInquiry!, error: IOReturn, aborted: Bool) {
//optional, but can notify you once the inquiry is completed.
    }
}
var delegate = BlueDelegate()
var ibdi = IOBluetoothDeviceInquiry(delegate: delegate)
ibdi?.updateNewDeviceNames = true
ibdi?.start()
PlaygroundPage.current.needsIndefiniteExecution = true

项目应用用法

import Cocoa
import IOBluetooth
import ...
class BlueDelegate : IOBluetoothDeviceInquiryDelegate {
    func deviceInquiryStarted(_ sender: IOBluetoothDeviceInquiry) {
        print("Inquiry Started...")
}
    func deviceInquiryDeviceFound(_ sender: IOBluetoothDeviceInquiry, device: IOBluetoothDevice) {
        print("\(device.addressString!)")
    }
}

//other classes here:



//reference the following outside of any class:
var delegate = BlueDelegate()
var ibdi = IOBluetoothDeviceInquiry(delegate: delegate)

//refer to these specifically inside of any class:
ibdi?.updateNewDeviceNames = true
ibdi?.start() //recommended under after an action-button press.

说明

由于查询仍在进行中,我最初遇到的问题是尝试访问信息。

当我访问它时,在许多不同的情况下,我的游乐场都将挂起,并且我将不得不强制退出Xcode.app和com.apple.CoreSimulator.CoreSimulatorService活动监视器。我让自己相信这只是一个Playground错误,只是得知查询完成后我的应用程序将崩溃。

正如AppleAPI参考所指出的那样:

重要说明:请勿使用委托方法在设备上或使用此对象时在设备上执行远程名称请求。如果希望在设备上进行自己的远程名称请求,请在停止该对象后再进行。如果您不注意此警告,则可能导致进程死锁。

这完全解释了我的问题。与其直接IOBluetoothDevicesender.foundDevices()方法中询问信息(我认为可能没有更新..?),我只是使用函数中内置的参数来提及它确实是一个IOBluetoothDevice对象,而只是索要该信息是打印。

最后说明

我希望我创建的这个Q / A可以IOBluetooth在Swift中使用时帮助需要帮助的其他人。缺少任何教程以及大量的过时的Objective-
C代码使查找此信息非常具有挑战性。我要感谢@RobNapier对试图找到这个问题的答案支撑 的开始。我还要感谢NotMyName对我在Apple
Developer Forums上的帖子的答复。

我将更早探索在iOS设备中使用此功能的方法!



 类似资料:
  • 我试图用BLE API扫描附近的蓝牙设备,但它似乎不起作用 我已经在清单中添加了权限 以下内容在创建对象时 创建扫描仪回调对象并进一步扫描 在logcat中,我只看到以下内容 这是我的app build gradle } 有人能指出我在这里遗漏了什么吗?

  • 我想浏览一下附近运行WearOS的Android智能手表,并与之建立联系。我使用CompanionDeviceManager遵循了这里的说明:https://developer.android.com/guide/topics/connectivity/companion-device-pairing CompanyDeviceManager始终显示所有设备类型的列表,包括其他手机、笔记本电脑、蓝

  • ap.stopBluetoothDevicesDiscovery() 停止搜寻附近的蓝牙外围设备。 错误码说明 error 描述 12 停止搜索失败 代码示例 <script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.inc.min.js"></script> <button class

  • ap.startBluetoothDevicesDiscovery(OPTION | services | service, CALLBACK) 开始搜寻附近的蓝牙外围设备。搜索结果将在 bluetoothDeviceFound 事件中返回。可直接传入一个数组作为 OPTION.services 参数。也可直接传入一个字符串作为 OPTION.services 的第一项。 OPTION 参数说明

  • 问题内容: 我正在尝试使用Linux上的Python 列出所有附近的/可发现的蓝牙设备, 包括已经配对的 蓝牙设备。 我知道如何使用其地址列出设备的服务,并且可以成功连接: 如果我不指定任何条件,则阅读PyBluez文档时,我希望附近的任何设备都能显示出来: “如果未指定任何条件,则返回检测到的所有附近服务的列表。” 我现在唯一需要的是能够列出已经配对的设备,无论它们是打开,关闭,附近还是不打开。

  • 我正在制作一个利用蓝牙功能的应用程序,例如扫描设备等。我检查了扫描标志并返回true,但没有显示我正在测试的可发现设备。 我使用三星J7 Pro作为我的应用程序测试设备,三星J7是我想在发现的设备列表中看到的设备。 J7已设置为可发现且蓝牙已打开。 我的代码基于Monkey。蓝牙 以下是我所拥有的: 声明 功能 listview绑定到将显示发现的设备的“vm”。它不显示任何内容,计数始终为零,但我