Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java InputStream.read(字节[]b)阻塞循环,don';t返回EOF(-1)_Java_Android_Bluetooth_Kotlin - Fatal编程技术网

Java InputStream.read(字节[]b)阻塞循环,don';t返回EOF(-1)

Java InputStream.read(字节[]b)阻塞循环,don';t返回EOF(-1),java,android,bluetooth,kotlin,Java,Android,Bluetooth,Kotlin,当我试图通过蓝牙从我的arduino接收消息时,inputStream.read()方法有一个小问题。我试图在EOF时获得-1字节。问题是,我不明白 read()在最后一次数据接收后获取块,并等待更多数据。作为 据我目前所知,它工作正常 我做了一些研究,发现在服务器(arduino)关闭他的流(outputstream?)之前,我无法获得EOF(-1)。 我想知道我正在朝着正确的方向理解这一点,如果是的话,那么如果我关闭arduino的流,下面代码中的循环将起作用呢 也许,最好是在数据末尾添加特

当我试图通过蓝牙从我的arduino接收消息时,inputStream.read()方法有一个小问题。我试图在EOF时获得-1字节。问题是,我不明白

read()在最后一次数据接收后获取块,并等待更多数据。作为 据我目前所知,它工作正常

我做了一些研究,发现在服务器(arduino)关闭他的流(outputstream?)之前,我无法获得EOF(-1)。 我想知道我正在朝着正确的方向理解这一点,如果是的话,那么如果我关闭arduino的流,下面代码中的循环将起作用呢

也许,最好是在数据末尾添加特殊符号

你们是如何解决这样的问题的?:)


这看起来只是一些不正确的逻辑。问:将
连接的
设置为
是什么?否则,这只是一个无限循环。(我看到了一个
connectionLost()
调用,但只有当出现
IOException
时才会发生这种情况……在您到达流末尾的情况下就不会发生这种情况。)问:什么是测试
bytes
的值是否为
-1
?这应该是无限循环,当我与设备断开连接时,设置了连接到<代码>的<代码>到<代码>错误。无论如何,这个循环都不起作用,因为它在inputStream.read()之后被阻塞<代码>字节
从不为-1。这就是问题所在。
read()
阻塞的原因是远程端已停止写入,但尚未关闭连接。远端应通过关闭来指示其已完成。当你解决了这个问题,你就可以处理无限循环问题了。
     override fun run() {
        Log.d(TAG, getLogString("BEGIN connected thread.", device))
        name = device.name + device.address
        connected = true

        val buffer = ByteArray(1024)
        var bytes: Int

        while (connected) {
            try {
                bytes = inputStream!!.read(buffer)

                if (bytes = -1) {
                   //Don't work
                   Log.d(TAG,"EOF")
                }
                val message = String(buffer, 0, bytes)
                handler.obtainMessage(BluetoothApi.MESSAGE_READ, message).sendToTarget()

                // For debug purposes.
                Log.d(TAG, getLogString("Received message: $message", device))

            } catch (e: IOException) {
                Log.e(TAG, getLogString("Disconnected.", device), e)
                connectionLost()
                break
            }
        }
    }