HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 使用Swift 3.0附近的蓝牙设备大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找一种方法来以编程方式列出我的设备找到的任何附近的蓝牙设备(可发现).我无法在 Swift 3.0中找到有关执行此调用的任何信息或教程.这篇 Q-A post讨论了使用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),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 @R_@R_772_11287@_8337@hen the inquiry has started.
    }
    func deviceInquiryDeviceFound(_ sender: IOBluetoothDeviceInquiry,device: IOBluetoothDevicE) {
        print("\(device.addressString!)")
    }
    func deviceInquiryComplete(_ sender: IOBluetoothDeviceInquiry!,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错误,只是为了得知我的应用程序在调查完成后会崩溃.

正如Apple的API Reference所述:

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

最后的说明

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

我将在iOS设备中更早地探索这种用法

大佬总结

以上是大佬教程为你收集整理的ios – 使用Swift 3.0附近的蓝牙设备全部内容,希望文章能够帮你解决ios – 使用Swift 3.0附近的蓝牙设备所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。