我有一些关于IOBluetooth框架的问题,下面列出了这些问题。
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发布于 2021-01-02 01:42:03
关于第二点,我修复了代码,并粘贴在下面。问题是,苹果的文档称,该对象是使用函数调用openL2CAPChannelSync实例化的,但事实并非如此。您需要先实例化对象,然后传递对实例化对象的引用。希望这能为人们节省一些时间,因为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 = truehttps://stackoverflow.com/questions/65525019
复制相似问题