简体   繁体   English

应用程序挂在BluetoothSocket.connect()上,挂在InputStream.read()上,connect()后无法断开连接

[英]Application hangs on BluetoothSocket.connect(), hangs on InputStream.read(), can't disconnect after connect()

I've implemented test application connecting android phone to medical equipment for my company, and I couldn't figure out why I cannot connect to the bluetooth device the second time. 我已经为我的公司实现了将Android手机连接到医疗设备的测试应用程序,但我不知道为什么我第二次无法连接到蓝牙设备。 The first time connect() worked fine, but since I couldn't disconnect properly, the second(and following) connect hanged. 第一次connect()工作正常,但是由于我无法正确断开连接,因此第二次(及以后)连接被挂起。

Second problem I had, was that it seemed after couple reads, the Thread was blocked and waited on the read() call. 我遇到的第二个问题是,似乎在夫妇读取之后,线程被阻塞并等待read()调用。

I was closing the BluetoothSocket properly, and made sure all finally clauses are rightly written. 我正在正确关闭BluetoothSocket,并确保正确编写了所有finally子句。 Everytime I debugged application the situation was analogous on 3 different Android devices. 每次我调试应用程序时,情况在3种不同的Android设备上都是类似的。

(Solution to my problem below, and hopefully it will save some countless hours for someone) (下面解决我的问题的方法,希望它可以为某人节省很多时间)

Paradoxaly, there was no problem at all with the code. 矛盾的是,代码根本没有问题。 The problem (although not exactly sure) must have been somewhere in between, but which exposed itself when I ran application in debbug mode. 问题(尽管不能完全确定)一定是介于两者之间,但是当我在调试模式下运行应用程序时,问题就暴露了出来。 Once i change my testing method to logcat one, and ran in release mode, everything worked as expected, so the read() calls were not blocking, but reading constantly, and BluetoothSocket.close() worked as expected. 一旦我将测试方法更改为logcat one,并在发布模式下运行,一切都按预期工作,因此read()调用不会阻塞,而是不断读取,并且BluetoothSocket.close()会按预期工作。

Seems that my bluetooth device was firing bytes into the stream so fast, that in debug mode something could not handle that (although like I mentioned above, am not completely sure what is the cause for that). 似乎我的蓝牙设备向流中发射字节的速度如此之快,以至于在调试模式下某些东西无法处理(尽管像我上面提到的那样,不确定是什么原因造成的)。

So to summarize, if someone will have same problems as me above, do not debug this situation, but run with Release mode, and if you need to test something, use logcat in this case. 综上所述,如果有人遇到与我相同的问题,请不要调试这种情况,而要以发布模式运行,如果需要测试,请在这种情况下使用logcat。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM