使用 CoreBluetooth / L2CAP 通道传输数据
Transferring data with CoreBluetooth / L2CAP channel
同时尝试使用 L2CAP
通道在 CoreBluetooth
中设置数据传输。
我有问题。在发送端,我点击一个按钮就触发了这个功能:
@objc func sendL2CAPInfo() {
print(#function)
let value = "Hello L2Cap Stream data...",
data = Data(value.utf8)
outPutStream.open()
print("outPutStream -> \(outPutStream.debugDescription)")
let bytesWritten = data.withUnsafeBytes { outPutStream.write([=10=], maxLength: data.count) }
print("bytesWritten = \(bytesWritten)")
}
执行后,我在 Xcode 调试控制台中看到:
sendL2CAPInfo()
outPutStream -> Optional(<__NSCFOutputStream: 0x282969b00>)
bytesWritten = 26
在接收方我有这个功能,也可以通过点击按钮触发:
@objc func receiveL2CAPInfo() {
print(#function)
let bufLength = 10
var buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufLength)
inPutStream.open()
let bytesRead = inPutStream.read(buffer, maxLength: bufLength)
print("bytesRead = \(bytesRead)")
}
还在接收端我也有这个代码:
// StreamDelegate protocol implementation.
func stream(_ aStream: Stream,
handle eventCode: Stream.Event) {
print(#function)
if aStream == inPutStream {print("Input-Stream")}
if aStream == outPutStream {print("Output-Stream")}
switch eventCode {
case Stream.Event.hasBytesAvailable:
print("Stream.Event.hasBytesAvailable")
receiveL2CAPInfo()
case Stream.Event.hasSpaceAvailable:
print("Stream.Event.hasSpaceAvailable")
case Stream.Event.openCompleted:
print("Stream.Event.openCompleted")
case Stream.Event.endEncountered:
print("Stream.Event.endEncountered")
case Stream.Event.errorOccurred:
print("Stream.Event.errorOccurred")
default:
print("Stream.Event.SOME-OTHER-THING-HAPPENED")
}
}
启动接收应用程序时(发送应用程序是 运行),我在 Xcode 调试控制台中看到:
centralManagerDidUpdateState
centralManager(_:didDiscover:advertisementData:rssi:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-17 13:16:37.168700+0900 CBL2CAPCh_Central[857:127305] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
stream(_:handle:)
Input-Stream
Stream.Event.hasBytesAvailable
receiveL2CAPInfo()
bytesRead = 0
stream(_:handle:)
Input-Stream
Stream.Event.endEncountered
在发送端,似乎一切顺利,写入了26个字节。
另一方面,在接收方,我们没有看到 26 个字节进入。
怎么了?我以前从未使用过 L2CAP
频道,所以我肯定做了一些不好的事情,但我不知道是什么。非常感谢任何有关检查内容或查看位置的建议。
.......
在深入研究该主题后,这里有更多信息。
这里是 stream(_ :handle:) 函数的代码:
func stream(_ aStream: Stream,
handle eventCode: Stream.Event) {
print(#function)
if aStream == inPutStream {print("Input-Stream")}
if aStream == outPutStream {print("Output-Stream")}
if (aStream != inPutStream) && (aStream != outPutStream)
{print("Some-other-Stream")}
switch eventCode {
case Stream.Event.hasBytesAvailable:
print("Stream.Event.hasBytesAvailable")
case Stream.Event.hasSpaceAvailable:
print("Stream.Event.hasSpaceAvailable")
case Stream.Event.openCompleted:
print("Stream.Event.openCompleted")
case Stream.Event.endEncountered:
print("Stream.Event.endEncountered")
case Stream.Event.errorOccurred:
print("Stream.Event.errorOccurred")
if let error = aStream.streamError {
print("Error:\n\t\(error.localizedDescription)")
}
default:
print("Stream.Event.SOME-OTHER-THING-HAPPENED")
}
}
中央应用程序启动后,在尝试编写任何内容之前,在调试器中可以看到以下内容:
centralManagerDidUpdateState
centralManager(_:didDiscover:advertisementData:rssi:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:32:14.840892+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
stream(_:handle:)
Input-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Output-Stream
Stream.Event.hasSpaceAvailable
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:32:28.521320+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
stream(_:handle:)
Input-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Output-Stream
Stream.Event.hasSpaceAvailable
2019-01-18 13:32:28.523746+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:32:38.948090+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
2019-01-18 13:32:38.949889+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:32:49.278142+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
2019-01-18 13:32:49.280093+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:32:59.739338+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
2019-01-18 13:32:59.741111+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:33:10.567965+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
2019-01-18 13:33:10.569722+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:33:21.404162+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
2019-01-18 13:33:21.406243+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:33:32.147567+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
2019-01-18 13:33:32.148884+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:33:42.788001+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
2019-01-18 13:33:42.789921+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:33:53.734140+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
2019-01-18 13:33:53.736063+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
2019-01-18 13:34:04.231561+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 436
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
2019-01-18 13:34:14.861939+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 436
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
2019-01-18 13:34:25.472075+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 436
与其尝试在点击按钮时打开流,不如在连接频道时打开流。然后,您可以使用流委托 hasBytesAvailable
事件来确定有字节要读取。
在当前 运行 循环中安排流很重要。
例如,要在 CBCentral
上打开流:
func peripheral(_ peripheral: CBPeripheral, didOpen channel: CBL2CAPChannel?, error: Error?) {
if let error = error {
print("Error opening l2cap channel - \(error.localizedDescription)")
return
}
guard let channel = channel else {
return
}
print("Opened channel \(channel)")
self.channel = channel
channel.inputStream.delegate = self
channel.outputStream.delegate = self
print("Opened channel \(channel)")
channel.inputStream.schedule(in: RunLoop.current, forMode: .default)
channel.outputStream.schedule(in: RunLoop.current, forMode: .default)
channel.inputStream.open()
channel.outputStream.open()
}
然后处理接收到的数据:
func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
switch eventCode {
case Stream.Event.openCompleted:
print("Stream is open")
case Stream.Event.endEncountered:
print("End Encountered")
case Stream.Event.hasBytesAvailable:
print("Bytes are available")
if let iStream = aStream as? InputStream {
let bufLength = 1024
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufLength)
let bytesRead = iStream.read(buffer, maxLength: bufLength)
print("bytesRead = \(bytesRead)")
if let string = String(bytesNoCopy: buffer, length: bytesRead, encoding: .utf8, freeWhenDone: false) {
print("Received data: \(string)")
}
}
case Stream.Event.hasSpaceAvailable:
print("Space is available")
case Stream.Event.errorOccurred:
print("Stream error")
default:
print("Unknown stream event")
}
}
我的完整示例可在此处获得:https://github.com/paulw11/L2CapDemo
同时尝试使用 L2CAP
通道在 CoreBluetooth
中设置数据传输。
我有问题。在发送端,我点击一个按钮就触发了这个功能:
@objc func sendL2CAPInfo() {
print(#function)
let value = "Hello L2Cap Stream data...",
data = Data(value.utf8)
outPutStream.open()
print("outPutStream -> \(outPutStream.debugDescription)")
let bytesWritten = data.withUnsafeBytes { outPutStream.write([=10=], maxLength: data.count) }
print("bytesWritten = \(bytesWritten)")
}
执行后,我在 Xcode 调试控制台中看到:
sendL2CAPInfo()
outPutStream -> Optional(<__NSCFOutputStream: 0x282969b00>)
bytesWritten = 26
在接收方我有这个功能,也可以通过点击按钮触发:
@objc func receiveL2CAPInfo() {
print(#function)
let bufLength = 10
var buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufLength)
inPutStream.open()
let bytesRead = inPutStream.read(buffer, maxLength: bufLength)
print("bytesRead = \(bytesRead)")
}
还在接收端我也有这个代码:
// StreamDelegate protocol implementation.
func stream(_ aStream: Stream,
handle eventCode: Stream.Event) {
print(#function)
if aStream == inPutStream {print("Input-Stream")}
if aStream == outPutStream {print("Output-Stream")}
switch eventCode {
case Stream.Event.hasBytesAvailable:
print("Stream.Event.hasBytesAvailable")
receiveL2CAPInfo()
case Stream.Event.hasSpaceAvailable:
print("Stream.Event.hasSpaceAvailable")
case Stream.Event.openCompleted:
print("Stream.Event.openCompleted")
case Stream.Event.endEncountered:
print("Stream.Event.endEncountered")
case Stream.Event.errorOccurred:
print("Stream.Event.errorOccurred")
default:
print("Stream.Event.SOME-OTHER-THING-HAPPENED")
}
}
启动接收应用程序时(发送应用程序是 运行),我在 Xcode 调试控制台中看到:
centralManagerDidUpdateState
centralManager(_:didDiscover:advertisementData:rssi:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-17 13:16:37.168700+0900 CBL2CAPCh_Central[857:127305] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
stream(_:handle:)
Input-Stream
Stream.Event.hasBytesAvailable
receiveL2CAPInfo()
bytesRead = 0
stream(_:handle:)
Input-Stream
Stream.Event.endEncountered
在发送端,似乎一切顺利,写入了26个字节。 另一方面,在接收方,我们没有看到 26 个字节进入。
怎么了?我以前从未使用过 L2CAP
频道,所以我肯定做了一些不好的事情,但我不知道是什么。非常感谢任何有关检查内容或查看位置的建议。
....... 在深入研究该主题后,这里有更多信息。
这里是 stream(_ :handle:) 函数的代码:
func stream(_ aStream: Stream,
handle eventCode: Stream.Event) {
print(#function)
if aStream == inPutStream {print("Input-Stream")}
if aStream == outPutStream {print("Output-Stream")}
if (aStream != inPutStream) && (aStream != outPutStream)
{print("Some-other-Stream")}
switch eventCode {
case Stream.Event.hasBytesAvailable:
print("Stream.Event.hasBytesAvailable")
case Stream.Event.hasSpaceAvailable:
print("Stream.Event.hasSpaceAvailable")
case Stream.Event.openCompleted:
print("Stream.Event.openCompleted")
case Stream.Event.endEncountered:
print("Stream.Event.endEncountered")
case Stream.Event.errorOccurred:
print("Stream.Event.errorOccurred")
if let error = aStream.streamError {
print("Error:\n\t\(error.localizedDescription)")
}
default:
print("Stream.Event.SOME-OTHER-THING-HAPPENED")
}
}
中央应用程序启动后,在尝试编写任何内容之前,在调试器中可以看到以下内容:
centralManagerDidUpdateState
centralManager(_:didDiscover:advertisementData:rssi:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:32:14.840892+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
stream(_:handle:)
Input-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Output-Stream
Stream.Event.hasSpaceAvailable
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:32:28.521320+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
stream(_:handle:)
Input-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Output-Stream
Stream.Event.hasSpaceAvailable
2019-01-18 13:32:28.523746+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:32:38.948090+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
2019-01-18 13:32:38.949889+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:32:49.278142+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
2019-01-18 13:32:49.280093+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:32:59.739338+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
2019-01-18 13:32:59.741111+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:33:10.567965+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
2019-01-18 13:33:10.569722+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:33:21.404162+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
2019-01-18 13:33:21.406243+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:33:32.147567+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
2019-01-18 13:33:32.148884+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:33:42.788001+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
2019-01-18 13:33:42.789921+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
2019-01-18 13:33:53.734140+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 431
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
2019-01-18 13:33:53.736063+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] No known channel matching peer <CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected> with psm 192
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
2019-01-18 13:34:04.231561+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 436
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
2019-01-18 13:34:14.861939+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 436
centralManager(_:didDisconnectPeripheral:error:)
centralManager(_:didConnect:)
peripheral(_:didOpen:error:)
stream(_:handle:)
Some-other-Stream
Stream.Event.hasSpaceAvailable
stream(_:handle:)
Output-Stream
Stream.Event.openCompleted
stream(_:handle:)
Input-Stream
Stream.Event.openCompleted
stream(_:handle:)
Some-other-Stream
Stream.Event.hasBytesAvailable
2019-01-18 13:34:25.472075+0900 CBL2CAPCh_Central[571:80355] [CoreBluetooth] WARNING: Unknown error: 436
与其尝试在点击按钮时打开流,不如在连接频道时打开流。然后,您可以使用流委托 hasBytesAvailable
事件来确定有字节要读取。
在当前 运行 循环中安排流很重要。
例如,要在 CBCentral
上打开流:
func peripheral(_ peripheral: CBPeripheral, didOpen channel: CBL2CAPChannel?, error: Error?) {
if let error = error {
print("Error opening l2cap channel - \(error.localizedDescription)")
return
}
guard let channel = channel else {
return
}
print("Opened channel \(channel)")
self.channel = channel
channel.inputStream.delegate = self
channel.outputStream.delegate = self
print("Opened channel \(channel)")
channel.inputStream.schedule(in: RunLoop.current, forMode: .default)
channel.outputStream.schedule(in: RunLoop.current, forMode: .default)
channel.inputStream.open()
channel.outputStream.open()
}
然后处理接收到的数据:
func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
switch eventCode {
case Stream.Event.openCompleted:
print("Stream is open")
case Stream.Event.endEncountered:
print("End Encountered")
case Stream.Event.hasBytesAvailable:
print("Bytes are available")
if let iStream = aStream as? InputStream {
let bufLength = 1024
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufLength)
let bytesRead = iStream.read(buffer, maxLength: bufLength)
print("bytesRead = \(bytesRead)")
if let string = String(bytesNoCopy: buffer, length: bytesRead, encoding: .utf8, freeWhenDone: false) {
print("Received data: \(string)")
}
}
case Stream.Event.hasSpaceAvailable:
print("Space is available")
case Stream.Event.errorOccurred:
print("Stream error")
default:
print("Unknown stream event")
}
}
我的完整示例可在此处获得:https://github.com/paulw11/L2CapDemo