如何通过CBL2CAPChannel发送和接收数据

时间:2018-12-19 16:29:06

标签: ios swift core-bluetooth l2cap

我正在尝试在2个iOS设备之间打开一个L2CAP通道,并双向传输数据。其中一台设备充当中央设备,另一台设备充当外围设备。

在外围:

我像这样发布L2CAPChannel:

func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
    if peripheral.state == .poweredOn {
        peripheral.publishL2CAPChannel(withEncryption: false)
    }
}

对加密尝试使用true和false。

然后,一旦发布频道,我就从didPublishL2CAPChannel委托方法获取PSM,并创建一个具有包含PSM作为其值的特征的服务,并开始发布它。

在中央:

我扫描外围设备,找到合适的外围设备,连接到外围设备,开始发现服务,然后在发现服务后就发现了特征。我找到特性,读取它的值并获得PSM。然后我这样做:

self.peripheral.openL2CAPChannel(psm)

然后,我在委托方法中调用该通道已打开并执行以下操作:

func peripheral(_ peripheral: CBPeripheral, didOpen channel: CBL2CAPChannel?, error: Error?) {
    guard error == nil else {
        print("Couldn't open channel. Error: \(error!.localizedDescription)")
        return
    }
    self.l2capChannel = channel
    self.l2capChannel?.inputStream.delegate = self
    self.l2capChannel?.outputStream.delegate = self
    print("L2CAP channel opened with \(peripheral.name ?? "unknown")")
}

此打印:

L2CAP channel opened with mrsta’s iPad

再次在外围侧:

我通过委托方法得到回叫:

func peripheralManager(_ peripheral: CBPeripheralManager, didOpen channel: CBL2CAPChannel?, error: Error?) {
    guard error == nil else {
        print("Couldn't open channel. Error: \(error!.localizedDescription)")
        return
    }
    self.l2capChannel = channel
    self.l2capChannel?.inputStream.delegate = self
    self.l2capChannel?.outputStream.delegate = self
    print("L2CAP channel opened")
}

此打印:

[CoreBluetooth] No central present! Creating a new object. This shouldn't happen.
L2CAP channel opened

到目前为止,似乎双方都打开了渠道。我只是想知道上面打印中的消息是什么...“没有中央礼物!...”

一段时间后,我开始在控制台中收到如下消息:

[CoreBluetooth] WARNING: Unknown error: 436
[CoreBluetooth] No known channel matching peer <CBPeripheral: 0x2829de120, identifier = 241BAA6F-0BFD-9F5A-1EC9-35A4FD246DF5, name = mrsta’s iPad, state = connected> with psm 192
[CoreBluetooth] WARNING: Unknown error: 431

我不知道这些是什么意思。有什么建议吗?

我还在两侧都实现了StreamDelegate方法:

func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
    print("Stream Event occurred: \(eventCode)")
    if eventCode == .hasSpaceAvailable {
        self.tryToWrite()
    }
}

但是从来没有调用上面的委托方法。我尝试这样写输出流(从中央的didOpen通道委托方法调用tryToWrite):

func tryToWrite() {
    let string = "Hello"
    let stringData = Data(from: string)
    let _ = stringData.withUnsafeBytes { write(stuff: $0, to: self.l2capChannel, withMaxLength: stringData.count) }
}

func write(stuff: UnsafePointer<UInt8>, to channel: CBL2CAPChannel?, withMaxLength maxLength: Int) {
    let result = channel?.outputStream.write(stuff, maxLength: maxLength)
    print("Write result: \(String(describing: result))")
}

结果是:

Write result: Optional(-1)

基于文档的意思是写失败。

请告诉我我想念什么?打开通道后会遇到哪些错误?正确的数据读写方式是什么?

1 个答案:

答案 0 :(得分:2)

我使用L2CAP,并且正在工作。我在两个“ didOpen”功能中所做的都是

self.l2capChannel = channel

self.l2capChannel?.inputStream.delegate = self 
self.l2capChannel?.inputStream.schedule(in: .main, forMode: .defaultRunLoopMode)
self.l2capChannel?.inputStream.open()

self.l2capChannel?.outputStream.delegate = self
self.l2capChannel?.outputStream.schedule(in: .main, forMode: .defaultRunLoopMode)
self.l2capChannel?.outputStream.open()

当我不再需要它们时,我关闭了它们

self.l2capChannel?.inputStream.close()
self.l2capChannel?.inputStream.remove(from: .main, forMode: .defaultRunLoopMode)

self.l2capChannel?.outputStream.close()
self.l2capChannel?.outputStream.remove(from: .main, forMode: .defaultRunLoopMode)

self.l2capChannel? = nil