Part 3: Bluetooth communications with 2 Arduino devices, using App Inventor
Please start with “Part 1: Basic Bluetooth communications using App Inventor” to learn how to configure, set up and program an App Inventor app that communicates over Bluetooth between two Android devices. Then, read “How to connect App Inventor apps to Arduino using Bluetooth” before going through this tutorial!
- If you know nothing about Bluetooth start with: Part 1 and Part 2
- If you know about Bluetooth and App Inventor, then start with: “How to connect App Inventor apps to Arduino using Bluetooth“
Then continue with this tutorial.
This tutorial shows how an App Inventor app can communicate with 2 (or more) Arduino boards and Bluetooth devices simultaneously. These instructions assume you are familiar with the code and hardware presented in Part 1 and Part 2 and “How to connect App Inventor apps to Arduino using Bluetooth“. This tutorial uses the same Arduino source code as in that tutorial.
A follow up tutorial will show how to simplify some of this code for supporting multiple Bluetooth devices.
Bluetooth is a short range, low power, limited speed wireless communications technology. The original Bluetooth technology provided a serial communications link between two paired devices (as compared to an individual data packet sent between up to n devices using the much newer Bluetooth LE – see here and here for information on Bluetooth LE).
Arduino is a microcontroller board for building hardware projects. You can write software for Arduino using a programming language similar to the C++ programming language.
The code used in these examples has been tested with some specific Bluetooth modules connected to Arduino. These include the JY-MCU (Amazon (Prime), Amazon (non-Prime) and also some HC-05 and HC-06 based Bluetooth modules.
- Read the prior tutorials (Part 1 and Part 2 and “How to connect App Inventor apps to Arduino using Bluetooth“)
- Build two Arduino boards each with an appropriate Bluetooth module as described in the prior tutorial.
- Compile and load the Arduino software in to each of the Arduino boards.
- Test and confirm that your basic LED lights flash for the original, single Bluetooth connection case.
- Then, with two working boards, continue to this tutorial.
User Interface View
The original app supported just one device, so there was just a single “Connect” and “Disconnect” button. This version demonstrates how to connect more than one Bluetooth device so we need separate buttons for each device. Similarly, we must add a second status and data sending item to the screen:
Before running this app, be sure to use Android | Settings | Bluetooth to “pair” your Bluetooth devices with Android.
Then, run the app and select Connect to Device 1. This displays a list of available Bluetooth devices in the vicinity. Select your specific Bluetooth device for the connection. Do this for both Bluetooth devices.
Once connected, you can send some simple commands to the Arduino board. Commands are very simple – a single number – to tell the Arduino to do something (this confirms that the Bluetooth link is working). If we enter a single digit 1 and then press Send Numeric 1, the Arduino board will send back 2 bytes of data which will then be displayed on the app screen. If we enter a single digit 4 and then press Send Numeric 1, a value of 4 is transmitted over Bluetooth to the Arduino board, which responds by flashing the externally connected LED.
Because the text box for data entry has its property set to NumbersOnly, a pop up numeric keypad displays when entering data, rather than the usual Android text keyboard.
Video Demonstration this App
I created a short video showing this app in operation. There are two versions of the video – one is standard 2D format and the other is in VR 3D format for viewing on Google Cardboard-like viewers used with smart phones to watch VR videos.
2D (normal) version: https://youtu.be/BU2gIAxbY_o
VR 3D SBS version: https://www.youtube.com/watch?v=UJIggzZgld4
That version is in 3D, for viewing with VR 3D viewers or 3D TVs or monitors.
The original Bluetooth Client for Arduino app has been updated to add a second Bluetooth device. As you can see, “Connect to device 1” has a matching “Connect to Device 2”. The Link status line has been split into status for device 1 and device 2. There are now two Disconnect buttons and to sets of data inputs and two Send buttons.
At the bottom, a second “non-visible” Bluetooth component has been added to the app. For each active Bluetooth link, we need to have a separate Bluetooth component. If our app had 4 Bluetooth links, then we would add a total of 4 Bluetooth components. For this app, we only need two Bluetooth components.
The components list shows how sample app in the prior tutorial has been modified by adding a second ListPicker, Disconnect, and two Send buttons instead of one.
For txtNumericData1 and txtNumericData2, I have checked the NumbersOnly option. This prevents the user from entering non-numeric data by displaying a numeric keypad for data entry.
The original code is most duplicated. For example, we take the original lpConnect1 event handlers, which looked like this:
And make a copy of those blocks, changing the elements to reference lpConnect2 and BluetoothClient2 instead of lpConnect1 and BluetoothClient1. We also duplicate the event handler for btnDisconnect so that we have one for btnDisconnect1 and btnDisconnect2.
The original App Inventor blocks for sending to the single Bluetooth device are also duplicated. The original looked like this:
We copy those components and rename to btnSendNumeric2, and change BluetoothClient1 to BluetoothClient2.
Data is read from the Bluetooth devices inside a Timer event (see the original tutorial for information on this).
Whereas the original read only from BluetoothClient1, you can see below that this code has been duplicated and changed to read data also from BluetoothClient2.
All we have done to work with two devices is to add a second Bluetooth device (BluetoothClient2) and duplicated all of the original code so we now have the same code in place for BluetoothClient1 and BluetoothClient2, plus duplicate user interface code.
Another tutorial shows how the above code can be simplified by using references to components and putting common or duplicate code into a single procedure.
Please see Part 1 and Part 2: Storing and accessing user interface components as variables for how to do this simplification. Part 2 modifies the above Bluetooth code to make it simpler.
Download source code: BTArduino_Client_2Devices.aia
Please see “How to connect App Inventor apps to Arduino using Bluetooth“for the Arduino source code.
A future tutorial will show some simplifications that can be made to this code using an App Inventor trick that is not well known. But the trick applies to many types of app programs.
(Update: That tutorial is now online – see Part 1 and Part 2: Storing and accessing user interface components as variables)
Frequently Asked Questions
How many Bluetooth devices can be connected simultaneously?
I have done 3 so far and am working on a project that will connect 6 external devices, eventually. The Bluetooth specification supports up to 7 simultaneous paired devices. You also need to contend with other limits, such as the bandwidth available for data being split among all the devices, the speed or performance of the Android device, Android memory limits and other factors. You also need to first pair each of the Bluetooth devices using Android’s Settings | Bluetooth options.
Bluetooth LE will work better for supporting many external devices. However, compatible Bluetooth LE hardware only exists on the newest phones, at this time. Bluetooth LE (the LE means “low energy”) is designed for many “Internet of Things” devices all communicating with other devices and the outside world.
Can I connect my App Inventor app on Android to a Bluetooth speaker or headphone?
You might be able to connect to the device but you will probably not be able to send data to the device to have it play sounds or music. To send music to the device, you would need to know the format of the data that the device expects for the incoming music data. Communications between devices runs according to a set of rules, known as a protocol. A protocol describes the sequence of communication between devices. For example, a protocol between two people might start like
Person A: Hello!
Person B: Hello, how are you?
Person A: I’m fine.
While people can communicate informally, devices talk back and forth using a well defined sequence of statements (the protocol). If we do not know the protocol of the device, we can not talk to the device.
Can I use Bluetooth part XYZ?
Best we can say is “give it a try!”. We can only note which parts we have used and which worked successfully in our testing.
In addition to hundreds of posts and examples on this blog, I have several e-books available from Google Books, Amazon and other outlets. The books cover topics that are far too large to cover in blog posts, especially for Volume 3 – Databases and Files and Volume 4 – Graphics, Animation and Charts. Volumes 1 and 2 are intended for those just starting out with App Inventor.
Visit the books page for details on each book, sample chapters, where to buy – all are inexpensive.
Also, use the Search box in the left column of this web site to look for other examples to help you with your App Inventor programming!