Ios L2CAP COC error SocketStream write error [0x0]: 1 32

I'm currently trying to develop a l2cap demo application where I want to send data to a bluetooth chip over l2cap.

I'm able to send several packet from the iPhone to the bluetooth chip. The chip send back credits and the iPhone continue to send data.

After a moment suddenly the outputstream crash and rise a error that I'm not able to interpret.

here is the error raised :
/Users/13fmeyer/Documents/Screenshot 2024-05-07 at 15.15.37.png

Here is the section of the code that manage the write :

/Users/13fmeyer/Documents/Screenshot 2024-05-07 at 15.16.49.png

and the write method : /Users/13fmeyer/Documents/Screenshot 2024-05-07 at 15.17.20.png

Finally it close the act connection and stop the transmission.

Does somebody have any idea about what happen ?

Replies

here is the error raised : (bad copy)

write index 93 data_to_write 230 min_range 116157 max_range 117406 max_range - min_range 1249 stream status Optional(__C.NSStreamStatus) SocketStream write error [0x0]: 1 32 Write result: Optional(-1) stream error Optional(Error Domain=NSPOSIXErrorDomain Code=32 "Broken pipe" UserInfo={_kCFStreamErrorCodeKey=32, _kCFStreamErrorDomainKey=1}) WARNING: Unknown error: 722 centralmanager disconnect error : nil Stream Event occurred: NSStreamEvent(rawValue: 8) Output-Stream Optional(<__NSCFOutputStream: 0x303b08630>) input: ErrorOccurred: Optional("The operation couldn’t be completed. Broken pipe") stream status NSStreamStatus(rawValue: 7) catch error

Here is the section of the code that manage the write : func stream(_ aStream: Stream, handle eventCode: Stream.Event) { print("Stream Event occurred: (eventCode)") //if aStream == self.l2capChannel.inputStream { // print("Input-Stream") //} if aStream == self.l2capChannel.outputStream { print("Output-Stream (self.l2capChannel.outputStream.debugDescription)") }

    switch eventCode {
    case Stream.Event.openCompleted:
        print("Stream is open")
    case Stream.Event.hasSpaceAvailable:
        print("Stream hasSpaceAvailable")
        if aStream == self.l2capChannel.outputStream {
            print("Stream is outputStream")
            print("is running \(isRunning)")
            if isRunning {
                print("write index \(write_index)")
                print("data_to_write \(data_to_write)")
                if write_index < data_to_write {
                    let unsafe_pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(l2cap_payload_size))
                    var min_range = write_index*l2cap_payload_size
                    print("min_range \(min_range)")
                    var max_range = (write_index+1)*l2cap_payload_size
                    print("max_range \(max_range)")
                    print("max_range - min_range \(max_range-min_range)")
                    let temp_data = [UInt8](data_to_send[min_range...max_range])
                    //print("temp_data \(temp_data)")
                    unsafe_pointer.initialize(from: temp_data,count: l2cap_payload_size)
                    write_l2cap_packet(stuff: unsafe_pointer, to:self.l2capChannel, withMaxLength: self.l2cap_payload_size)
                    write_index += 1
                    unsafe_pointer.deallocate()
                }
                else {
                    isRunning = false
                }
            }
            
        }
    case Stream.Event.endEncountered:
        print("End encountered")
    case Stream.Event.hasBytesAvailable:
        //if self.l2capChannel.inputStream.hasBytesAvailable {
        print("stream hasBytesAvailable inputStream")
        //}
    case Stream.Event.errorOccurred:
        print("input: ErrorOccurred: \(String(describing: aStream.streamError?.localizedDescription))")
        print("stream status \(aStream.streamStatus)")
        print("catch error ")
    default:
        print("Unprocessed event, closing channel")
        closeChannel(l2capChannel)
    }
}

and the write method :

private func write_l2cap_packet(stuff: UnsafePointer<UInt8>, to channel: CBL2CAPChannel?, withMaxLength maxLength: Int) {
    print("stream status \(String(describing: channel?.outputStream.streamStatus))")
    let result = channel?.outputStream.write(stuff, maxLength: maxLength)
    print("Write result: \(String(describing: result))")
    if result == -1 {
        print("stream error \(String(describing: channel?.outputStream.streamError))")
    }
}