IOException: read failed, socket might closed - Bluetooth on Android 4.3

后端 未结 16 2103
天涯浪人
天涯浪人 2020-11-22 04:08

Currently I am trying to deal with a strange Exception when opening a BluetoothSocket on my Nexus 7 (2012), with Android 4.3 (Build JWR66Y, I guess the second 4.3 update). I

16条回答
  •  礼貌的吻别
    2020-11-22 04:30

    I had the same symptoms as described here. I could connect once to a bluetooth printer but subsequent connects failed with "socket closed" no matter what I did.

    I found it a bit strange that the workarounds described here would be necessary. After going through my code I found that I had forgot to close the socket's InputStream and OutputSteram and not terminated the ConnectedThreads properly.

    The ConnectedThread I use is the same as in the example here:

    http://developer.android.com/guide/topics/connectivity/bluetooth.html

    Note that ConnectThread and ConnectedThread are two different classes.

    Whatever class that starts the ConnectedThread must call interrupt() and cancel() on the thread. I added mmInStream.close() and mmOutStream.close() in the ConnectedTread.cancel() method.

    After closing the threads/streams/sockets properly I could create new sockets without any problem.

提交回复
热议问题