本文介绍了附近使用 Swift 3.0 的蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a way to programmatically list any nearby Bluetooth devices (discoverable) that my device finds. I have not been able to find any information or tutorials regarding performing this call in Swift 3.0. This Q-A post discusses finding these devices using Swift 1.0 and building in Xcode 6, rather than the latest version 8.

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

I did my best to try to make my code into the 3.0 Syntax from the 1.0, but while running the following code, nothing is returned in the 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), Swift 3.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.

说明

我最初面临的问题是在查询仍在进行中尝试访问信息.

Explanation

The issue I was originally faced with was trying to access the information as the inquiry was still in process.

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

When I accessed it, under many different occasions my playground would hang and I would be forced to force quit both Xcode.app, and com.apple.CoreSimulator.CoreSimulatorService from the Activity Monitor. I lead myself to believe that this was just a Playground bug, only to learn that my application would crash once the inquiry finished.

正如 Apple 的 API 参考 所述:

As Apple's API Reference states:

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

这完全解释了我的问题.而不是直接从 sender.foundDevices() 方法(我认为它可能没有更新..?)中询问 IOBluetoothDevice 信息,我只是使用内置的参数函数提到它确实是一个 IOBluetoothDevice 对象,并且只是要求打印该信息.

Which entirely explained my issue. Rather than directly asking for the IOBluetoothDevice information from the sender.foundDevices() method (which I believe may not have been updating..?) I simply used the parameters built into the function to mention that it was indeed an IOBluetoothDevice object, and simply to ask for that information to be printed.

我希望我创建的这个 Q/A 在 Swift 中使用 IOBluetooth 时可以帮助有需要的人.由于缺乏任何教程以及大量过时的 Objective-C 代码,因此查找这些信息非常具有挑战性.我要感谢@RobNapier 在一开始就试图找到这个谜语 的答案的支持.我还要感谢 NotMyName 对我在 Apple 开发者论坛上的帖子的回复.

I hope that this Q/A I've created helps others in need when using IOBluetooth in Swift. The lack of any tutorials and the high amounts of outdated, Objective-C code made finding this information very challenging. I'd like to thank @RobNapier for the support on trying to find the answer to this riddle in the beginning. I'd also like to thank NotMyName for the reply on my post on the Apple Developer Forums.

我迟早会探索在 iOS 设备中使用它!

I will be exploring the usage of this in an iOS device more sooner than later!

这篇关于附近使用 Swift 3.0 的蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 21:56