Introduction: DragonBoard410c: Communicating With an App for Android Via Bluetooth LE
In this project, we show you how to set up a communication via Bluetooth Low Energy (BLE) using an Android application as a Bluetooth peripheral and a DragonBoard410c as a central using a Python program. This setup allows to connect and send text messages between multiple Android devices, creating a network that is managed by the DragonBoard. That's pretty good to simulate the communication between the DragonBoard and other devices such as sensors.
In the first step, we show you how to download and install the Android application.
Step 1: Downloading and Installing the Android App
There are two ways to install the app:
- You can just download the APK and install it directly to your device, or
- You can download the entire project and install it with Android Studio. This option is useful when you want to check or edit the source code of the application (on the next step we will explain the code for you).
The app, named BLEChat, was designed to be as simple as possible, consisting of only two buttons and a text field which will be explained in detail in the next steps. We guarantee compatibility with Android 5.0 or later.
Step 2: Using the BLEChat
To better understand how the app works, we must first understand the basics of Bluetooth Low Energy. This technology uses GATT (Generic Attribute) to create a hierarchical data structure to connect BLE devices. It consists of a Profile, which contains Services, and each of these services contain Characteristics. And using these characteristics we transmit data between the App and the DragonBoard. When we create a characteristic, we attribute to them an identifier called UUID to differentiate them from other characteristics in the same service. To transmit data, we just change the value of a characteristic, attributing the desired data set to it. For more information, you can read the official documentation of Bluetooth Low Energy.
So, we created three characteristics on the application:
- The first one is called mSentDataCharacteristic, and it is the one we use to send data to the DragonBoard.
- The second is called mReceivedDataCharacteristic, and it is the one we use to read the data sent from the DragonBoard.
- The third one is called mBLEChatInfoCharacteristic, and it shows the name of the app, which we use to identify in the python program of the DragonBoard if the connection request is coming from the app.
The rest is simple: when we click on the blue arrow in the bottom of the screen, the text typed in the text field is stored in the mSentDataCharacteristic value and notified, so the DragonBoard can know when there's something new to read from this characteristic. When a value is received from the DragonBoard in the mReceivedDataCharacteristic, it automatically updates the chat with the text received. The red button just erases all the text on the screen.
If you want to know more how the code works, you can find more detail in its comments.
And now we are done with the app! The next steps explain how the DragonBoard program works.
Step 3: Python Requirements
To run our Python program, PyGatt and PyBluez libraries are required. To install them, type these commands in the terminal:
$ sudo apt-get update $ sudo apt-get install python-pip python-dev ipython
$ sudo apt-get install bluetooth libbluetooth-dev $ sudo pip install pybluez $ sudo pip install pygatt $ sudo apt-get install libdbus-1-dev libdbus-glib-1-dev libglib2.0-dev libical-dev libreadline-dev libudev-dev libusb-dev make
$ mkdir -p work/bluepy cd work/bluepy wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.32.tar.xz tar xvf bluez-5.32.tar.xz
$ cd bluez-5.32 ./configure --disable-systemd make
$ sudo make install
$ sudo reboot
Step 4: Executing Python Program
After installing the libraries, copy the Python program to the Dragonboard. Open the terminal and run the code using the command below in the directory you pasted the file:
$ python PythonProgram.py
Step 5: Explanation About Features of the Python Program
When the program is started, it initiates a scan for devices that uses BLE and shows on the DragonBoard all devices found with their respective MAC addresses. It is necessary to open the BLEChat on your smartphone before start the scan, otherwise, it won't be recognised.
First, the program tries to connect using public MAC, normally used by Android 5 or lower. If it succeeds, the program tries to connect to the next device on the list. If it doesn't, it tries to connect using random MAC, normally used by Android 6 or higher. Only connection from the app is allowed and, to check it if the BLE device is a smartphone with the app installed, the program compares the string in the mBLEChatInfoCharacteristic.
After running the program and connecting to all devices which the connection was possible, a list of all connected smartphones is sent to the application, indicated by a number and its MAC address. This list is useful when the user wants to know the exact device that the data is being sent. This number is an identifier we use to choose which device will receive the data.
When you send messages from the application to DragonBoard, you can use this communication protocol we made:
- If the first two digits received on the DragonBoard are "DB", the board will send the string to all connected devices (except the prefix).
- If the first two digits received are "D" followed by a number (the identifier mentioned above), the board will send the string to the device corresponding to that number (except the prefix).
- If there is no prefix on the received data, the string is just received on DragonBoard.
By sending the "exit" string from any connected device, the Python program closes.
Step 6: Conclusion
So, with our project, you can exchange messages between Android smartphones and the DragonBoard via Bluetooth Low Energy, creating a web of interconnected devices managed by the DragonBoard.
We have a be nice policy.
Please be positive and constructive.