dyl*_*lan 10 macos bluetooth iobluetooth ios swift
我正在寻找一种方法来以编程方式列出我的设备找到的任何附近的蓝牙设备(可发现).我无法在Swift 3.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
Run Code Online (Sandbox Code Playgroud)
以下代码在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
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
我最初面临的问题是试图访问信息,因为调查仍在进行中.
当我访问它时,在很多不同的场合,我的操场会挂起,我将被迫强制退出Xcode.app和com.apple.CoreSimulator.CoreSimulatorServiceActivity Monitor.我让自己相信这只是一个Playground错误,只是为了得知我的应用程序在调查结束后会崩溃.
正如Apple的API Reference所述:
重要说明:请勿在委托方法或使用此对象时在设备上执行远程名称请求.如果您希望在设备上执行自己的远程名称请求,请在停止此对象后执行这些请求.如果您不注意此警告,则可能会使您的进程陷入僵局.
这完全解释了我的问题.而不是直接询问方法中的IOBluetoothDevice信息sender.foundDevices()(我认为可能没有更新......?)我只是使用函数内置的参数来提及它确实是一个IOBluetoothDevice对象,并且只是要求该信息是打印.
我希望我IOBluetooth在Swift中使用时创建的这个Q/A可以帮助其他有需要的人.缺乏任何教程和大量过时的Objective-C代码使得查找此信息非常具有挑战性.我要感谢@RobNapier 在一开始就试图找到这个谜题的答案的支持.我也想感谢NotMyName对我的答复后对苹果开发者论坛.
我将在iOS设备中更快地探索这种用法!
| 归档时间: |
|
| 查看次数: |
3669 次 |
| 最近记录: |