title: Transferring data with CoreBluetooth / L2CAP channel
body: <p>While trying to set a data transfer in <code>CoreBluetooth</code> using <code>L2CAP</code> channel.
  I am having problems. On the sending side I have this function firing on the tap of a button:</p>
  
  <pre><code>@objc func sendL2CAPInfo() {
      print(#function)
      let value = "Hello L2Cap Stream data...",
      data = Data(value.utf8)
      outPutStream.open()
      print("outPutStream -&gt; \(outPutStream.debugDescription)")
      let bytesWritten = data.withUnsafeBytes { outPutStream.write($0, maxLength: data.count) }
      print("bytesWritten = \(bytesWritten)")
  }
  </code></pre>
  
  <p>When it is executed I see this in the Xcode debugging console:</p>
  
  <pre><code>sendL2CAPInfo()
  outPutStream -&gt; Optional(&lt;__NSCFOutputStream: 0x282969b00&gt;)
  bytesWritten = 26
  </code></pre>
  
  <p>On the receiving side I have this function, also firing on the tap of a button:</p>
  
  <pre><code>@objc func receiveL2CAPInfo() {
      print(#function)
      let bufLength = 10
      var buffer = UnsafeMutablePointer&lt;UInt8&gt;.allocate(capacity: bufLength)
      inPutStream.open()
      let bytesRead = inPutStream.read(buffer, maxLength: bufLength)
      print("bytesRead = \(bytesRead)")
  }
  </code></pre>
  
  <p>Still on the receiving side I also have this code:</p>
  
  <pre><code>// 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")
      }
  }
  </code></pre>
  
  <p>When launching the receiving app (while the sending app is running) I see this in the Xcode debugging console:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>On the sending side, it seems that things go as expected, 26 bytes are written.
  On the other hand on the receiving side we do not see the 26 bytes coming in.</p>
  
  <p>What is wrong? I have never used <code>L2CAP</code> channel before, so I must certainly be doing something not good, but I have no idea what. Any recommendation on what to check or where to look at would be very much appreciated.</p>
  
  <p>.....
  After more digging into the subject, here is some more information.</p>
  
  <p>Here is the code for for the stream(_ :handle:) function:</p>
  
  <pre><code>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) &amp;&amp; (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")
      }
  }
  </code></pre>
  
  <p>Here is what can be seen in the debugger once the central app is started, before attempting to write anything:</p>
  
  <pre><code>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 &lt;CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected&gt; 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 &lt;CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected&gt; 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 &lt;CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected&gt; 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 &lt;CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected&gt; 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 &lt;CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected&gt; 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 &lt;CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected&gt; 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 &lt;CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected&gt; 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 &lt;CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected&gt; 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 &lt;CBPeripheral: 0x281bc4a00, identifier = D73B08A0-E9BB-483B-8078-4A7F992E7565, name = Michel’s iPad, state = connected&gt; 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
  </code></pre>