This demo shows how to setup a simple bluetooth server in a Raspberry Pi so an Android Phone can connect to it.
This demo assumes:
- basic Linux (*nix also works too) command line knowledge
- Python knowledge
This demo was tested using the Raspberry Pi 3 that comes with pi-bluetooth pre-loaded and working 'out of the box'. If you are using an older version set up pi-bluetooth before following this demo, multiple resources can be found online. Depending on your setup the following command should work:
sudo apt-get install pi-bluetooth
This demo uses Bluez, Linux's Bluetooth protocol stack, we'll be using PyBluez, a Python API for accesing the bluetooth resources using the bluez protocol.
install bluez
sudo apt-get install bluetooth bluez
install pyBluez
sudo apt-get install bluez python-bluez
Make your device discoverable
sudo hciconfig hci0 piscan
Configure your device name
sudo hciconfig hci0 name 'Device Name' [change your device name to something else you fancy]
run the inquiry example:
sudo python /usr/share/doc/python-bluez/examples/simple/inquiry.py
The example may also be found here.
run the rfcomm-server example:
sudo python /usr/share/doc/python-bluez/examples/simple/rfcomm-server.py
The example may also be found here. After running the script the server will be waiting for an incoming connection.
There are several applications in the Google Play Store for bluetooth connectivity that may work. We'll be using BlueTerm as it supports the RFCOMM bluetooth protocol.
Steps:
- Download BlueTerm
- Scan for devices, if you've done everything correctly the Raspberry Pi should show up
- Connect
- Success! Send messages back and forth!
This demo would not have been possible thanks to the following posts and online resources:
Although this repo intends to be a summary of these resources, if you are stuck it might be useful to check them out.
Traceback (most recent call last):
File "/usr/share/doc/python-bluez/examples/simple/rfcomm-server.py", line 20, in <module>
profiles = [ SERIAL_PORT_PROFILE ],
File "/usr/lib/python2.7/dist-packages/bluetooth/bluez.py", line 176, in advertise_service
raise BluetoothError (str (e))
bluetooth.btcommon.BluetoothError: (2, 'No such file or directory')
Possible fixes
- make sure you are using sudo when running the python script
- make sure you have the serial profile loaded. How to enable the serial profile.
- Yor own fix? Make a pull request!
If something didn't work for you, you had to implement a weird fix or hack, or you think something could be explained better (maybe you found a typo, missing comma, or some weird wording (this is very much likely)) feel free to make a pull request!