Ios 附近使用Swift 3.0的蓝牙设备

Ios 附近使用Swift 3.0的蓝牙设备,ios,swift,macos,bluetooth,iobluetooth,Ios,Swift,Macos,Bluetooth,Iobluetooth,我正在寻找一种方法,以编程方式列出我的设备找到的任何附近的蓝牙设备(可发现)。我还没有找到任何关于在Swift 3.0中执行此呼叫的信息或教程。本文讨论如何使用Swift 1.0查找这些设备,并在Xcode 6中构建,而不是使用最新版本8 我尽了最大努力将代码从1.0转换为3.0语法,但在运行以下代码时,没有返回任何内容: import Cocoa import IOBluetooth import PlaygroundSupport class BlueDelegate : IOBlueto

我正在寻找一种方法,以编程方式列出我的设备找到的任何附近的蓝牙设备(可发现)。我还没有找到任何关于在Swift 3.0中执行此呼叫的信息或教程。本文讨论如何使用Swift 1.0查找这些设备,并在Xcode 6中构建,而不是使用最新版本8

我尽了最大努力将代码从1.0转换为3.0语法,但在运行以下代码时,没有返回任何内容:

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
正确使用蓝牙 以下代码在Xcode版本8.2.1(8C1002),Swift 3.0中完美地工作。有几行是不需要的,例如
deviceInquiryStarted
的整个方法

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

游乐场 项目应用程序使用 解释 我最初面临的问题是试图获取信息,因为调查仍在进行中

当我访问它时,在许多不同的情况下,我的游乐场会被挂起,我会被迫从活动监视器中退出Xcode.app和
com.apple.CoreSimulator.CoreSimulatorService
。我让自己相信这只是一个游乐场的bug,结果发现一旦查询完成,我的应用程序就会崩溃

正如苹果公司所说:

重要提示:不要从委托方法或在使用此对象时对设备执行远程名称请求。如果希望在设备上执行自己的远程名称请求,请在停止此对象后执行这些请求。如果不注意此警告,可能会导致进程死锁

这完全解释了我的问题。我没有直接从
sender.foundDevices()
method(我相信可能没有更新过…?)请求
IOBluetoothDevice
信息,而是使用函数中内置的参数来说明它确实是一个
IOBluetoothDevice
对象,只是要求打印这些信息

最后说明 我希望在Swift中使用
IOBluetooth
时,我创建的这个Q/A能够帮助其他需要帮助的人。由于缺少任何教程和大量过时的Objective-C代码,查找这些信息非常具有挑战性。我要感谢@RobNapier在开始时对我的支持,帮助我找到这个谜的答案。我还要感谢NotMyName在苹果开发者论坛上的回复


我将探索在iOS设备中使用它的更快

你是否看不到附近的iPhone或Mac蓝牙面板上显示的设备是可发现的?Mac电脑没有提供通用的蓝牙嗅探器。所以这些设备都显示为可被附近的其他设备发现,但上面的代码没有看到它们?(当你说“按下按钮后”是指“使设备可发现的按钮”还是指“我的UI中的按钮?”)执行搜索的设备是否可发现并不重要。如果要检测的设备当前可被发现,这很重要。如果它们出现在iOS或Mac的其他蓝牙首选项面板中,您就会知道这一点。如果他们没有出现在那里,他们就不太可能出现在这里。这实际上不适合StackOverflow。观看视频,播放一点无线蓝牙,然后通过robnapier@gmail.com如果你发现自己陷入困境。蓝牙有点复杂。我想在下面的回答中告诉大家,我正确地实现了它:@RobNapierI尚未测试,但这是一个非常好的答案。恭喜你,谢谢你,这可能会很有用。这段代码似乎对我不起作用。它从不归还任何东西。我还尝试将
ibdi.searchType
设置为
kIOBluetoothDeviceSearchLE.rawValue
kIOBluetoothDeviceSearchClass.rawValue
,但仍然一无所获。苹果的文档中有这样一句话:“它不会让你进行无限量的背对背查询,但是如果在一小段时间内尝试了太多的查询,它会限制尝试查询的次数。”但这对于你开发工具来说似乎非常令人沮丧。而且似乎没有办法检查你是否被节流。有什么想法吗?你是在游乐场环境中还是在项目中?在操场上它似乎工作得很好@克里索克,什么是游乐场:是的,我也不工作(只是试图在正常环境下构建一个mac应用程序),甚至“查询开始”也没有被调用。。。我是不是在plist中丢失了一些权限?啊,该死的-在macOS上,你需要在你的项目目标中添加一个“功能:应用程序沙盒”,在沙盒中,打开“蓝牙”,然后一切神奇地开始工作。。。
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.