I have a project that connects to a device via Bluetooth. It worked reliably enough, but now it interrupts the call to BluetoothSocket.connect() every time. (Well, I got it to connect once for thousands of attempts over a 4-hour period.) Most of the code was taken from the standard chat code sample in the API, with the exception of the general modification when receiving a BluetoothSocket :
Method m = device.getClass().getMethod( "createRfcommSocket", new Class[] { int.class }); tmp = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
Here's a method of interest that starts after a BluetoothSocket :
public void run() { setName("ConnectThread" + mSocketType);
Corresponding log entry (printed when an exception occurs when connect() called):
11-30 10: 23: 51.685: E / BluetoothChatService (2870): connection to ZYNO-700091 at 00: 06: 66: 42: 8E: 01 failed: reading failed, the connector may be closed, read ret: -1
This error has come from time to time. I have an aggressive reconnect system - it basically clogs the connection over and over until it connects, and if it ever disconnects, it clogs it again. Thus, it kills the connection thread and constantly starts from scratch. I thought that there might be a problem - maybe multi-threaded or, possibly, socket cleaning / initialization processing. However, if this is the case, I still expect the first connection attempt to succeed, since this system does not work until there is an unsuccessful connection attempt.
I looked into the source code , throwing an exception. The problem is that the underlying InputStream has no data. Of course, this is not quite the answer, just a step towards it. Why does the stream have no data?
I try not to forget about the potential problem. Am I getting a BluetoothSocket socket correctly? The fact that it was once an intermittent problem and now almost constant makes me suspect multithreading, but that a relatively simple topic in Java compared to C ++ is difficult to spoil if you know what you are doing. In addition, most of this code (in particular, parts related to thread synchronization) directly from the sample code.
The device on the other end is an integrated Bluetooth device, so there is no hope of debugging the problem for this purpose.
UPDATE =============================
It occurred to me that this could be due to updating the OS (I work on Galaxy Nexus phones - I have several tests). So I unpacked a new phone with 4.0.4 and it will work! So, we returned and tested on two original test phones running 4.2, expecting the failure that I saw all this time. Strange, now it works on these phones. I would like to say that I did something to do this work again, but I did not. I'm still puzzled, and now it is also suspicious that this thing will work when I really need it.
I wonder if there is any chance that somehow connecting using 4.0.4 could correctly establish the state of the server module, making it susceptible to 4.2 devices? Just a shot in the dark, I suppose ...
UPDATE 2 ============================
I found that unlocking and reconnecting will allow devices to connect. This is a workaround, but it's better than nothing.