How to connect to Raspberry pi with an android app over bluetooth
<p>I'm having trouble connecting my smartphone to my raspberry pi over bluetooth using an app.</p>

<p><strong>My situation:</strong></p>

<p>I'm developing a bluetooth controllable application based on a raspberry pi. I'm able to connect to the raspberry pi over bluetooth and send and receive data over an RFCOMM socket using the app "bluetooth terminal". The raspberry pi is constantly listening for RFCOMM connection.</p>

<p><strong>My goal:</strong></p>

<p>I want to develop an app in which the user can connect with the raspberry pi over bluetooth. The app should open the RFCOMM socket so it can communicate with the pi.</p>

<p><strong>My problem:</strong></p>

<p>My app is not able to connect to the raspberry pi and since i don't know the UUID of the raspberry pi, i think that might be the problem. </p>

<p><strong>My code:</strong></p>

<p>I'm quite new to java programming so correct me if you see anything strange. This is the method which i'm trying to connect with.</p>

<pre><code>        public void BTConnect() {

    final UUID MY_UUID = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    BluetoothSocket socket = null;
    String RPi_MAC = "XX:XX:XX:XX:XX:XX";

    Set&lt;BluetoothDevice&gt; pairedDevices = mBluetoothAdapter.getBondedDevices();
    // If there are paired devices
    if (pairedDevices.size() &gt; 0) {

        // Loop through paired devices
        for (BluetoothDevice device : pairedDevices) {
            if (device.getAddress().equals(RPi_MAC)) {
                try {
                    socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
                } catch (IOException e0) {
                    Log.d("BT_TEST", "Cannot create socket");
                    e0.printStackTrace();
                }

                try {
                    socket.connect();
                } catch (IOException e1) {
                    try {
                        socket.close();
                        Log.d("BT_TEST", "Cannot connect");
                        e1.printStackTrace();
                    } catch (IOException e2) {
                        Log.d("BT_TEST", "Socket not closed");
                        e2.printStackTrace();
                    }
                }
            }
        }
    }
}
</code></pre>

<p>When I click the button to connect, this is the output of android studio:</p>

<pre><code>   W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
   D/BT_TEST: Cannot connect
   W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
   W/System.err:     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:599)
   W/System.err:     at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:610)
   W/System.err:     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:333)
   W/System.err:     at com.example.gebruiker.soundslikepi.MainActivity.BTConnect(MainActivity.java:80)
   W/System.err:     at com.example.gebruiker.soundslikepi.MainActivity$1.onClick(MainActivity.java:39)
   W/System.err:     at android.view.View.performClick(View.java:4856)
   W/System.err:     at android.view.View$PerformClick.run(View.java:19956)
   W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
   W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
   W/System.err:     at android.os.Looper.loop(Looper.java:211)
   W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5373)
   W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
   W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
   W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
   W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
</code></pre>

<p><strong>Summarized:</strong></p>

<p>So, to be able to connect with the raspberry pi from an android app, do I:</p>

<ul>
<li>Need to know what the UUID of the raspberry pi is, if so, how do i find out?</li>
<li>Need to use a different approach in my android app?</li>
</ul>

<p>I would really like to know how to fix this problem, so any help would be appreciated.</p>