简体   繁体   English

IOBluetooth 的问题

[英]Issues with IOBluetooth

I have a few questions regarding IOBluetooth framework which are listed below.我对下面列出的 IOBluetooth 框架有一些疑问。

  1. I currently have one MacBook Pro (13-inch, 2018) running Big Sur v11.1 and when I call inquiries and attempt to find nearby devices it often fails and does not work consistently.我目前有一台运行 Big Sur v11.1 的 MacBook Pro(13 英寸,2018 年),当我打电话询问并尝试查找附近的设备时,它经常失败并且无法持续工作。 On the other hand, I have a second MacBook Pro (13-inch, 2015) running Mojave v10.14.6 which does the exact same functions and always works each time.另一方面,我有第二台运行 Mojave v10.14.6 的 MacBook Pro(13 英寸,2015 年),它具有完全相同的功能并且每次都能正常工作。 I have also been testing using blueutil command line tool: https://github.com/toy/blueutil as well as PyBluez: https://github.com/pybluez/pybluez and find that my second MacBook running Mojave always finds nearby devices while the MacBook running Big Sur has trouble doing so.我也一直在使用 blueutil 命令行工具进行测试: https://github.com/toy/blueutil以及 PyBluez: https://github.com/pybluez/pybluez并发现我的第二台运行 Mojave 的 MacBook 总是能找到附近的设备而运行 Big Sur 的 MacBook 则遇到了麻烦。 Do you know if this is because of potential updates to the framework or is there something wrong with my laptop running Big Sur?你知道这是因为框架的潜在更新还是我运行 Big Sur 的笔记本电脑有问题?

  2. I am trying to open L2CAPChannel from my first laptop to the second and vice versa but calling openL2CAPChannelSync on the IOBluetoothDevice object (which I have instantiated properly) seems to never return kIOReturnSuccess.我试图从我的第一台笔记本电脑打开 L2CAPChannel 到第二台笔记本电脑,反之亦然,但在 IOBluetoothDevice object(我已正确实例化)上调用 openL2CAPChannelSync 似乎永远不会返回 kIOReturnSuccess。 Am I doing something wrong here as well?我在这里也做错了吗? I attached a snippet of the code (in which I removed the addressString of my other device) I am using below.我附上了我在下面使用的代码片段(在其中我删除了我的其他设备的地址字符串)。

import IOBluetooth
import PlaygroundSupport

class ChannelDelegate : IOBluetoothL2CAPChannelDelegate {

    func l2capChannelOpenComplete(_ l2capChannel: IOBluetoothL2CAPChannel!, status error: IOReturn) {
        print("Channel Opened!")
    }

}

var remoteDevice = IOBluetoothDevice(addressString: ***deviceString***)
print((remoteDevice?.name ?? "nil") as String)

remoteDevice?.openConnection()
var connection = remoteDevice?.isConnected()
print(connection!)

var channelPtr: AutoreleasingUnsafeMutablePointer<IOBluetoothL2CAPChannel?>?
var success = remoteDevice?.openL2CAPChannelSync(channelPtr, withPSM: 0x0000, delegate: ChannelDelegate())
print(success == kIOReturnSuccess)

PlaygroundPage.current.needsIndefiniteExecution = true

In regards to the second point, I fixed the code and is pasted below.关于第二点,我修复了代码并粘贴在下面。 The issue was that Apple's documentation stated that the object is instantiated using the function call openL2CAPChannelSync but that is not the case.问题在于 Apple 的文档指出 object 是使用 function 调用openL2CAPChannelSync实例化的,但事实并非如此。 You need to instantiate the object first then pass a reference to the object you instantiated.您需要先实例化 object,然后传递对您实例化的 object 的引用。 Hope this saves people some time given how little examples there are on IOBluetooth API.希望这可以为人们节省一些时间,因为 IOBluetooth API 上的示例很少。

import IOBluetooth
import PlaygroundSupport

class ChannelDelegate : IOBluetoothL2CAPChannelDelegate {

    func l2capChannelOpenComplete(_ l2capChannel: IOBluetoothL2CAPChannel!, status error: IOReturn) {
        print("Channel Opened!")
    }

}

var remoteDevice = IOBluetoothDevice(addressString: ***deviceString***)
print((remoteDevice?.name ?? "nil") as String)

remoteDevice?.openConnection()
var connection = remoteDevice?.isConnected()
print(connection!)

var channel: IOBluetoothL2CAPChannel? = IOBluetoothL2CAPChannel()
var success = remoteDevice?.openL2CAPChannelSync(&channel, withPSM: 0x0000, delegate: ChannelDelegate())
print(success == kIOReturnSuccess)

PlaygroundPage.current.needsIndefiniteExecution = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM