Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Service discovery failed" from Android Bluetooth Insecure Rfcomm
    text
    copied!<p><strong>Does anyone know how to create an insecure RFCOMM connection between 2 Android devices at API level 2.3.3 while using an arbitrarily declared service name?</strong> (not random or changing service name, just a service name that I define myself)</p> <h2>Details</h2> <p>I am trying to create an insecure Rfcomm connection between 2 Android devices: Droid X2 and an Asus Transformer. I am assuming that both of these devices have functionality at the level of Android 2.3.3 to actually gain the ability to use insecure Rfcomm.</p> <p>When I try to create the Bluetooth connection as described <a href="https://stackoverflow.com/questions/5308373/how-to-create-insecure-rfcomm-socket-in-android">here</a>, using the now public createInsecureRfcommSocketToServiceRecord() and listenUsingInsecureRfcommWithServiceRecord(SERVICE, UUID), I get a reported:</p> <pre><code>java.io.IOException: Service discovery failed at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:377) at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:201) at com.s4d.bluenomad.DeviceConnections$ClientThread.run(DeviceConnections.java:406) </code></pre> <p>I found a <a href="https://stackoverflow.com/questions/8282481/android-bluetooth-connection-secure-insecure">related question</a> where someone creating a normal connection was getting this error and used reflection to invoke a private method. However, I have no idea what private method would now correspond to initiating an "insecure" connection. I tried using the solution proposed in that related question, but I am asked by Android to pair the devices which is exactly what I need to avoid. I really do need the insecure approach.</p> <p>I even tried a combination of the official and hacked solutions outlined <a href="https://stackoverflow.com/questions/8282481/android-bluetooth-connection-secure-insecure">here</a></p> <h2>Relevant Code Snippets</h2> <p><strong>Creating ServerThread To Listen For Connections</strong></p> <pre><code>Log.i(TAG, "Constructing a ServerThread"); // Use a temporary object that is later assigned to serverSocket, // because serverSocket is final BluetoothServerSocket tmp = null; try { // MY_UUID is the app's UUID string, also used by the client code tmp = btAdapter.listenUsingInsecureRfcommWithServiceRecord(SERVICE_NAME, SERVICE_UUID); Log.i(TAG,"Started listening on insecure RFCOMM channel for service requests for: " + SERVICE_NAME); } catch (IOException e) { } serverSocket = tmp; </code></pre> <p><strong>ServerThread Listening for Connections</strong></p> <pre><code>BluetoothSocket socket; while(true) { try { socket = serverSocket.accept(); } catch( IOException e) { break; } if( socket != null ) { Log.i(TAG, "Received new socket connection requesting service: " + SERVICE_NAME); } else { Log.i(TAG, "Socket connection attempted, but socket received is NULL."); } } </code></pre> <p><strong>Creating ClientThread to Initiate Connections</strong></p> <pre><code>Log.i(TAG, "Constructing a ClientThread"); BluetoothSocket tmp = null; try { // MY_UUID is the app's UUID string, also used by the server code tmp = device.createInsecureRfcommSocketToServiceRecord(SERVICE_UUID); Log.i(TAG,"Created client socket on insecure RFCOMM channel for service requests for: " + SERVICE_NAME); } catch (IOException e) { Log.i(TAG, "Failed to createInsecureRfcommSocket() against MAC: " + device.getAddress()); } clientSocket = tmp; </code></pre> <p><strong>ClientThread Connecting to Server</strong></p> <pre><code>try { clientSocket.connect(); } catch( final IOException e) { DeviceConnections.this.runOnUiThread(new Runnable() { @Override public void run() { console.append("Client unable to connect to service."); Log.i(TAG, "Client socket unable to connect() to: " + clientSocket.getRemoteDevice().getAddress()); e.printStackTrace(); } }); } </code></pre> <p>I do get the log output "Client socket unable to connect() to: [MY_MAC_ADDRESS]", then I get the stacktrace for the "Service discovery failed" exception.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload