蓝牙InputStream.read()不返回数据,并永远块 [英] Bluetooth InputStream.read() doesn't return Data and Blocks forever

查看:778
本文介绍了蓝牙InputStream.read()不返回数据,并永远块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题与Android的蓝牙东西。
当我打电话

I've got some problems with Android Bluetooth stuff. When I call

bytes = mmInStream.read(buffer);

这通常工作,因为它应该。
在猫B15智能手机但是,read方法有时块永远,即使连接仍在运行和数据应到达。
我暂时由该code解决了这个问题:

It usually works as it should. On the Cat B15 smartphone however, the read method sometimes blocks forever, even though the connection is still running and data should be arriving. I have temporarily solved the problem by this code:

while (true) {

    int available = 0;

    try {
        available = mInStream.available();
    } catch (IOException e) {}

    if (available > 0) {
        try {
            bytes = mInStream.read(buffer);
            ioExceptionsCounter = 0;
            // [send the obtained bytes to the UI activity]
            // ...............
        } catch (IOException e) {
            ++ioExceptionsCounter;
            if (ioExceptionsCounter >= 4) {
                break;
            }
        }
    }

    try {
        Thread.sleep(10);
    } catch (InterruptedException e) {}
}

我不认为ioExceptionsCounter确实是必要的​​,但有一些抱怨,有时断开没有道理,所以我想过有IOException异常可能不足以关闭连接。

I dont think that the ioExceptionsCounter is really necessary but there was some complaints that it sometimes disconnects without reason so I thought one IOException might not be enough to close the connection.

我真的不喜欢这个,它使用轮询。现在它确实工作猫的手机,但它并没有让我高兴的是,所有其他的设备现在执行这个丑陋的code。

What I really don't like about this is that it uses polling. It does work now on the Cat phone but it doesn't make me happy that all the other devices now execute this ugly code.

你有任何想法,这可能是为什么?这只是手机的一个bug?顺便提一下它运行ICS但绝对是设备特定的。

Do you have any ideas why this could be? Is this just a bug of the phone? By the way it runs ICS but it is definitely device specific.

推荐答案

我倾向于认为你遇到一个特定的硬件错误。

I'm inclined to think that you are encountering a hardware-specific bug.

各种 InputStream.read()方法记录在案,以阻塞,直到至少有一个字节被读取,或者检测到流的末尾,或发生错误。如果阅读()有时块永远为你而倘若没有字节可当它第一次被调用,那么这绝对是您自己的code之外的错误。

The various InputStream.read() methods are documented to block until at least one byte is read, or the end of the stream is detected, or an error occurs. If the read() sometimes blocks forever for you in the event that no bytes are available when it is first invoked, then that's definitely a bug outside your own code.

此外,这是非常值得怀疑忽略任何数量的 IOException异常 S,无论是从用()阅读()。流抛出异常后,您不能确信的东西,你以后可能会设法从中读取数据的完整性。我通常会想到这样的尝试在进一步阅读也扔 IOException异常秒。如果您收到虚​​假 IOException异常 S于猫B15,这样简单地重试你的阅读()成功获取正确的数据,那么这也是一个错误(同样一个也许另一面)。

Also, it's highly questionable to ignore any number of IOExceptions, either from available() or from read(). After the stream throws an exception you cannot be confident of the integrity of anything you afterward might manage to read from it. I would normally expect such attempts at further reading also to throw IOExceptions. If you are getting spurious IOExceptions on the Cat B15, such that simply retrying your read() successfully obtains the correct data, then that is also a bug (maybe another facet of the same one).

这篇关于蓝牙InputStream.read()不返回数据,并永远块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆