I am only trying to write and read across a Bluetooth Socket but i my readBytes call is not completing. I think that this is very simple but maybe i am just using the wrong type of stream or something. As of right now my code is just sending a small amount of text as bytes. This is placeholder code that will be replaced with code that writes and reads a file over the stream. Here is my Receive Thread:
class ReceiveThread(val inStream:BufferedInputStream):Thread() {
var bytes:ByteArray? = null
override fun run() {
BluetoothService.log("Begin ${BTS_Constants.THREAD_RECEIVE}")
BluetoothService.log("preparing to read data")
name = BTS_Constants.THREAD_RECEIVE
//here is my new code
inStream.use {
do{
count++
BluetoothService.log("read loop round $count")
byteList.add(it.read() as Byte)
}while (it.available()>0)
}
BluetoothService.log("data read: ${byteList.get(0) as Char}")
}
}
and here is my Send Thread:
class SendThread(val outStream:BufferedOutputStream, val file:File? = null):Thread(){
var bytes:ByteArray? = null
override fun run() {
BluetoothService.log("Begin : ${BTS_Constants.THREAD_SEND}")
name = BTS_Constants.THREAD_SEND
bytes = "hello testing".toByteArray()
try{
outStream.use {
it.write(bytes)
it.flush()
}
}catch (ioe:IOException){
}
BluetoothService.log("data sent")
}
}
the data is successfully sent, and the BluetoothService.log("data sent")
call is reached and displayed in the logcat for the device that is running a Send Thread. For the device running the Receive Thread logs the "Preparing to read data" message, but never logs the data read: "$bytes message"
.
How can i make this call to inStream.readBytes() complete?
Edit: new Error message that i am receiving:
11-27 23:45:29.298 16253-16413/com.example.zemcd.fileblue E/AndroidRuntime: FATAL EXCEPTION: RECEIVE
Process: com.example.zemcd.fileblue, PID: 16253
java.io.IOException: bt socket closed, read return: -1
at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:588)
at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
at com.example.zemcd.fileblue.ReceiveThread.run(BluetoothService.kt:230)