Controlling Raspberry Pi GPIO using Android App over Bluetooth

Raspberry Pi is very popular for IoT projects because of its seamless ability of wireless communication over internet. Raspberry Pi 3 has inbuilt Wi-Fi and Bluetooth, and Bluetooth is a very popular wireless communication Protocol. So today we are going to Control Raspberry Pi GPIO Pin through an Android app using Bluetooth.

Here we are using Raspberry 2 Pi Model B which don’t have inbuilt Bluetooth, so we are using a simple USB Bluetooth dongle. Apart from that we only need a resistor (220R) and a LED to demonstrate the GPIO controlling. Here we are using RFCOMM Bluetooth protocol for wireless communication.

Programming for Bluetooth in Python follows the socket programming model and communications between the Bluetooth devices is done through RFCOMM socket. RFCOMM (Radio Frequency Communication) is a Bluetooth Protocol which provided emulated RS-232 serial ports and also called as Serial Port Emulation. Bluetooth serial port profile is based on this protocol. RFCOMM is very popular in Bluetooth applications because of its wide support and publically available API. It is bound to L2CAP protocol.

We have also used Bluetooth module HC-06 in our previous project: Voice controlled LEDs using Raspberry Pi. Also check our previous Raspberry Pi Projects along with some good IoT Projects.

Installing Required Packages for Bluetooth Communication:

Before start, we need to install some softwares for setting up Bluetooth communication in Raspberry Pi. You should have a Raspbian Jessie installed memory card ready with Raspberry Pi. Check this article to install the Raspbian OS and getting started with Raspberry Pi. So now we first need to update the Raspbian using below commands:

sudo apt-get update
sudo apt-get upgrade

Then we need to install few Bluetooth related packages:

sudo apt-get install bluetooth blueman bluez

Then reboot the Raspberry Pi:

sudo reboot

BlueZ is a open source project and official Linux Bluetooth protocol stack. It supports all the core Bluetooth protocols and now become part of official Linux Kernel.

Blueman provides the Desktop interface to manage and control the Bluetooth devices.

Finally we need python Library for Bluetooth communication so that we can send and receive data through RFCOMM using Python language:

sudo apt-get install python-bluetooth

Also install the GPIO support libraries for Raspberry Pi:

sudo apt-get install python-rpi.gpio

Now we are done with installing required packages for Bluetooth communication in Raspberry Pi.

Pairing Devices with Raspberry Pi over Bluetooth:

Pairing Bluetooth Devices, like mobile phone, with Raspberry Pi is very easy. Here we have paired our Android Smart phone with Raspberry Pi. We have previously installed BlueZ in Pi, which provides a command line utility called “bluetoothctl” to manage our Bluetooth devices. But before that, connect your USB Bluetooth dongle with Raspberry Pi and check that whether it is detected or not, by using below command:

lsusb

detecting-usb-bluetooth-dongle-in-raspberry-pi

Now open the bluetoothctl utility by below command:

sudo bluetoothctl

You can check all the commands of bluetoothctl utility by typing ‘help’. For now we need to enter below commands in given order:

[bluetooth]# power on
[bluetooth]# agent on
[bluetooth]# discoverable on
[bluetooth]# pairable on
[bluetooth]# scan on

After the last command “scan on”, you will see your Bluetooth device (Mobile phone) in the list. Make sure that your mobile has Bluetooth turned on and visible by nearby devices. Then copy the MAC address of you device and pair it by using given command:

pair <address of your phone>

Then you will be prompted for Passcode or Pin in your Terminal console then type passcode there and press enter. Then type the same passcode in your mobile phone when prompted and you are now successfully paired with Raspberry Pi. We have also explained this whole process in our Video given at the end.

Read More:  Controlling Raspberry Pi GPIO using Android App over Bluetooth


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top