I'm having trouble connecting my smartphone to my raspberry pi over bluetooth using an app.
My situation:
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.
My goal:
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.
My problem:
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.
My code:
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.
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<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 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();
}
}
}
}
}
}
When I click the button to connect, this is the output of android studio:
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)
Summarized:
So, to be able to connect with the raspberry pi from an android app, do I:
I would really like to know how to fix this problem, so any help would be appreciated.
You may use network or bluetooth to connect. You can try to connect your Raspberry PI to your router(either wired-connection or WIFI is ok) and then let your phone join the LAN. Then start a socket server or a http server on you PI and your android phone can send a packet or a request to PI after the button clicked.
It's really common and easy to use SSH to access Raspberry Pi through Internet like WiFi or Ethernet. However, if you don't have internet connection, bluetooth is your best friend to access to your raspberry pi.
Just press the Bluetooth icon, and select your phone Bluetooth. 4. After pairing the Bluetooth, add the python script directly into the Raspbian by typing the nano command and copy/paste the source code, or you can directly copy the run.py file. 6.
Similar to how you started out on this a bluetooth connection requires both device address and service UUID.
The device address (ex: 00:72:02:97:33:2C) can be obtained from paired devices (or by allowing for discovery), see Android example app for more on that.
The UUID (ex: 00001101-0000-1000-8000-00805F9B34FB) is typically defined on the server part where a Bluetooth service is running with a specific identifier.
Now if you want to avoid that last part and just want to communicate simple data you can rely on a default using the bluetooth serial port. From Android Bluetooth documentation:
Hint: If you are connecting to a Bluetooth serial board then try using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. However if you are connecting to an Android peer then please generate your own unique UUID.
The android sample BluetoothChat is a nice example on how to deal with 2 way communication between 2 android devices.
Android Bluetooth Documentation https://developer.android.com/guide/topics/connectivity/bluetooth.html
The Bluetooth Chat sample https://developer.android.com/samples/index.html. Note that you can check this project out by just selecting File > New > Sample Project and searching for Bluetooth Chat in Android Studio.
Dealing with starting such a service on a raspberry pi can be found using this Python example: http://blog.davidvassallo.me/2014/05/11/android-linux-raspberry-pi-bluetooth-communication/. Just one of the many samples available online. This example includes Android code.
If you want to implement such a thing on the raspberry Pi using C++ code I can recommended the documentation from http://people.csail.mit.edu/albert/bluez-intro/x502.html.
That link has example code for both server and client. Since this is using RFCOMM spec and default serial port UUID there is no need to specify any UUID.
If you look into how to connect to this from android you will likely end up finding the BlueTerm android app. This is an open source app so you can find all sources at https://github.com/johnhowe/BlueTerm/tree/master/src/es/pymasde/blueterm. There you can find the UUID for this serial port service:
private static final UUID SerialPortServiceClass_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
The complete BluetoothSerialService class is a good way of managing a bluetooth connection including handling messages back to UI.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With