└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # raspberry-bluetooth-demo 2 | 3 | This demo shows how to setup a simple bluetooth server in a Raspberry Pi so an Android Phone can connect to it. 4 | 5 | ## Prerequisites 6 | This demo assumes: 7 | * basic Linux (*nix also works too) command line knowledge 8 | * Python knowledge 9 | 10 | 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: 11 | 12 | ``` 13 | sudo apt-get install pi-bluetooth 14 | ``` 15 | 16 | This demo uses Bluez, Linux's Bluetooth protocol stack, we'll be using [PyBluez](https://github.com/karulis/pybluez), a Python API for accesing the bluetooth resources using the bluez protocol. 17 | 18 | ## Installation 19 | 20 | install bluez 21 | ``` 22 | sudo apt-get install bluetooth bluez 23 | ``` 24 | 25 | install pyBluez 26 | ``` 27 | sudo apt-get install bluez python-bluez 28 | ``` 29 | 30 | ## Setup your Raspberry Pi 31 | 32 | Make your device discoverable 33 | ``` 34 | sudo hciconfig hci0 piscan 35 | ``` 36 | Configure your device name 37 | ``` 38 | sudo hciconfig hci0 name 'Device Name' [change your device name to something else you fancy] 39 | ``` 40 | 41 | ## Scanning for devices 42 | run the inquiry example: 43 | ``` 44 | sudo python /usr/share/doc/python-bluez/examples/simple/inquiry.py 45 | ``` 46 | 47 | The example may also be found [here](https://github.com/karulis/pybluez/blob/master/examples/simple/inquiry.py). 48 | 49 | 50 | ## Running the Bluetooth Server 51 | run the rfcomm-server example: 52 | ``` 53 | sudo python /usr/share/doc/python-bluez/examples/simple/rfcomm-server.py 54 | ``` 55 | 56 | The example may also be found [here](https://github.com/karulis/pybluez/blob/master/examples/simple/rfcomm-server.py). After running the script the server will be waiting for an incoming connection. 57 | 58 | ## Connecting from an Android Phone 59 | There are several applications in the Google Play Store for bluetooth connectivity that may work. We'll be using [BlueTerm](https://play.google.com/store/apps/details?id=es.pymasde.blueterm&hl=en) as it supports the RFCOMM bluetooth protocol. 60 | 61 | __Steps:__ 62 | 1. Download [BlueTerm](https://play.google.com/store/apps/details?id=es.pymasde.blueterm&hl=en) 63 | 2. Scan for devices, if you've done everything correctly the Raspberry Pi should show up 64 | 3. Connect 65 | 4. Success! Send messages back and forth! 66 | 67 | ## Credits 68 | This demo would not have been possible thanks to the following posts and online resources: 69 | * [Android Linux / Raspberry Pi Bluetooth communication](http://blog.davidvassallo.me/2014/05/11/android-linux-raspberry-pi-bluetooth-communication/) 70 | * [Raspberry Pi 3 Bluetooth Setup](https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=138145) 71 | 72 | Although this repo intends to be a summary of these resources, if you are stuck it might be useful to check them out. 73 | 74 | ## Known Issues 75 | 76 | ``` 77 | Traceback (most recent call last): 78 | File "/usr/share/doc/python-bluez/examples/simple/rfcomm-server.py", line 20, in 79 | profiles = [ SERIAL_PORT_PROFILE ], 80 | File "/usr/lib/python2.7/dist-packages/bluetooth/bluez.py", line 176, in advertise_service 81 | raise BluetoothError (str (e)) 82 | bluetooth.btcommon.BluetoothError: (2, 'No such file or directory') 83 | ``` 84 | __Possible fixes__ 85 | * make sure you are using sudo when running the python script 86 | * make sure you have the serial profile loaded. [How to enable the serial profile](https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=133263). 87 | * Yor own fix? Make a pull request! 88 | 89 | ## Important Note! 90 | 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! 91 | --------------------------------------------------------------------------------