I\'m having trouble connecting my smartphone to my raspberry pi over bluetooth using an app.
My situation:
I\'m developing a bluetooth controlla
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.
you don't need to know the uuid, you have to set the uuid you want to use on both client and server
I had faced a similar problem while connecting to a raspberrypi3 bluetooth via my android app. First execute the following commands one by one on your pi command prompt:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install bluetooth blueman bluez
sudo reboot
sudo apt-get install python-bluetooth
After the execution of the above commands pair your app's bluetooth with pi's bluetooth. Now you need to add the SP profile to the Pi. Edit this file:
Execute the following command on the pi command prompt:
sudo nano /etc/systemd/system/dbus-org.bluez.service
A file will open and add the compatibility flag, ' -C', at the end of the 'ExecStart=' line. Add a new line after that to add the SP profile. The two lines should look like this:
ExecStart=/usr/lib/bluetooth/bluetoothd -C
ExecStartPost=/usr/bin/sdptool add SP
Save and reboot your pi and this should work.
I think linux use bluetooth need specify the connect port.
try {
Method m = remoteDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
try {
BluetoothSocket tmp = (BluetoothSocket)m.invoke(remoteDevice, 1);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}