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

StackOverflow https://stackoverflow.com/questions/22691046

سؤال

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. The first time connect() worked fine, but since I couldn't disconnect properly, the second(and following) connect hanged.

Second problem I had, was that it seemed after couple reads, the Thread was blocked and waited on the read() call.

I was closing the BluetoothSocket properly, and made sure all finally clauses are rightly written. Everytime I debugged application the situation was analogous on 3 different Android devices.

(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.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top