android - Kotlin readBytes() 永远不会完成

标签 android sockets stream kotlin bluetooth-socket

我只是想通过蓝牙套接字写入和读取,但我的 readBytes 调用没有完成。我认为这很简单,但也许我只是使用了错误类型的流或其他东西。截至目前,我的代码只是以字节形式发送少量文本。这是占位符代码,将替换为通过流写入和读取文件的代码。这是我的接收线程:

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}")
}
}

这是我的发送线程:

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")
}

}

数据已成功发送,BluetoothService.log("data sent") 调用已到达并显示在运行发送线程的设备的 logcat 中。对于运行接收线程的设备,记录“准备读取数据”消息,但从不记录 data read: "$bytes message"

如何完成对 inStream.readBytes() 的调用?

编辑:我收到的新错误消息:

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)

最佳答案

如果您查看 Kotlin 扩展函数的源代码 readBytes您会看到它在 InputStream.read(buffer) > 0 时开始循环。根据documentation :

This method blocks until input data is available, end of file is detected, or an exception is thrown.

此循环的第二次迭代会卡住您的程序。 所以不要使用方法readBytes。只需读取(缓冲区)。 例如 - https://developer.android.com/guide/topics/connectivity/bluetooth.html#ManagingAConnection

更新:

现在 ReceiveThread 可以从流中接收一条消息。但是你的蓝牙 socket 很近。所以你的问题是在设置蓝牙连接和 inStream 的初始化。也许你也有错误的发送部分。

关于android - Kotlin readBytes() 永远不会完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47524148/

相关文章:

android - Gradle:错误:程序类型已存在:androidx.activity.R$attr

android - 无法解析AndroidJunit4

java - 如何从字符串中找到匹配的单词以及百分比值?

android - ViewPager fragment - 共享元素转换

java - 为什么在 Java 中打印 '\0' 会使该行的其余部分无效?

c++ - 读取char后如何清除cin缓冲区

stream - 概念 : Channel vs. 流

javascript - 来自 Android 和 IOS 客户端的 Ratchet 套接字

php - PHP Socket Client仅发送和接收一条消息

C 在不定义套接字的情况下发送套接字