I am unable to transmit an entire file using WiFi-Direct. The file sender is indicating that the entire file has been copied over to the socket output stream. The file receiver is only receiving roughly half of the file.

I looked at the contents of both the original file and the contents of the file storing the received data, and found the receiver is only receiving pieces of the original file. For example, it would receive bytes 0-100, and then it would jump to byte 245-350.

Why is the receiver only receiving bits and pieces of the file, rather than the entire file?

File Receiving Logic

private class FileReceiveThread(val channel: Channel) : TransmissionThread() { private var mFileName: String = "" private var mFileSize: Long = 0L private var mBytesReceivedTotal = 0L override fun run() { try { Timber.d("File receive thread running: fileSize=$mFileSize, fileName=$mFileName") val outputFile = File.createTempFile("file", "") val fileOutput = outputFile.outputStream() val channelInput = channel.getInputStream().unwrap() val inputBuffer = ByteArray(FILE_TX_BUFFER_SIZE) var bytesReceived = channelInput.read(inputBuffer) while (bytesReceived > 0) { fileOutput.write(inputBuffer) mBytesReceivedTotal += bytesReceived Timber.d("Received $mBytesReceivedTotal total bytes") bytesReceived = channelInput.read(inputBuffer) } onTransmitComplete?.invoke() } catch (e: Exception) { e.printStackTrace() } } fun start(filename: String, size: Long) { mFileName = filename mFileSize = size start() } }

File Sending Logic