I'm connecting to an RN42 bluetooth module which does a standard rfcomm protocol with the standard SPP UID.
The problem is that connect times out sometimes and sometimes it doesn't. How to 'reproduce': 1) do a standard connect() to get a socket 2) connect() times out and throws an exception 3) wait a bit (1 - 10 sec) 4) do another connect() and it works It doesn't matter if it's a secure, insecure or the secret hidden API call to connect() without uuid. Sometimes it works straight away and then perhaps for a day but the API certainly "feels" when I present the program to a potential customer and it decides not to connect. ;) Here is my code which mininises it by essentially just trying connect() a couple of times. Ugly, but it works sort of. Adding a delay, trying to connect again and usually it works at the 2nd attempt but sometimes even all attempts fail. Then I restart the program and it works at the 1st or 2nd attempt. I know that loads of people report the same problem and some have theories that it's only on an HTC but not on other devices etc. I can confirm the problem is on both a Nexus 7 and on a Sony Z3. Both run 6.0.1 and API 23. Any feedback would be very welcome so that I can get rid of this cascase of connects. Best /Bernd if (bluetoothDevice == null) return; // Get a BluetoothSocket to connect with the given BluetoothDevice try { mmSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(uuid); } catch (Exception ex) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Could not get rfComm socket:", ex); } try { mmSocket.close(); } catch (Exception closeExeption) { } ; mmSocket = null; messageListener.haveMessage(MESSAGE_ERROR); return; } if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Got rfComm socket!"); } try { sleep(100); } catch (Exception esleep) { } ; if (mmSocket != null) { try { if (mmSocket != null) { mmSocket.connect(); } } catch (IOException connectException) { // connection failed messageListener.haveMessage(MESSAGE_RETRY); try { mmSocket.close(); } catch (IOException e1) {} // let's just wait a bit try { sleep(100); } catch (InterruptedException e1) {} // let's try to connect try { if (mmSocket != null) { mmSocket.connect(); } } catch (IOException e2) { try { sleep(100); } catch (InterruptedException e3) { } try { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Last resort: we try the hidden API"); } mmSocket.close(); mmSocket = null; Method createMethod = bluetoothDevice.getClass(). getMethod("createInsecureRfcommSocket", new Class[]{int.class}); mmSocket = (BluetoothSocket) createMethod.invoke(bluetoothDevice, 1); } catch (Exception e) { if (Log.isLoggable(TAG, Log.ERROR)) { Log.e(TAG, "Could not get non-UUID based bluetooth socket!", e); } mmSocket = null; return; } try { sleep(100); } catch (InterruptedException e3) { } try { if (mmSocket != null) { mmSocket.connect(); } } catch (IOException e4) { try { mmSocket.close(); mmSocket = null; } catch (IOException e) { } connectionEstablished = false; fatalError = true; if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Could not establish connection to Attys: " + e4.getMessage()); } messageListener.haveMessage(MESSAGE_ERROR); return; } } } connectionEstablished = true; if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Connected to socket!"); } } } /Bernd -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. To post to this group, send email to android-developers@googlegroups.com. Visit this group at https://groups.google.com/group/android-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/0131b190-f15a-437a-9ce2-edf08b6d1251%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.