What to do
We will create a mobile program that will connect to the wireless module of our “robot” and send the command. The sketch will receive this command and enable or disable one of the LEDs installed on the Arduino.
What we will use:
- Arduino Uno.
- Bluetooth Bee.
- Arduino Wireless Shield.
- Green and red LED lights.
- Breadboard Half.
- Male to male wires.
Creating the mobile application
The application development for Android is performed in the Android Development Tools. It can be downloaded from the Google Play. After downloading and installing, run the ADT. After that, you will need to download the necessary version of the Android SDK. Choose the same version that your smartphone has. Our old phone that we will use has the version 2.3.3, so we will choose it. Press «Install_Packages» to run the installation process.
After finishing the previous step, we can start creating the program. Click «File->New->Android Applications Project» and specify the appropriate values.
Application Name is the name of the program; it will be displayed in the Google Play.
Project Name is the name of the project in the development environment.
Package Name is the id of the app. It is created using the following method: the name of your site backwards + the name of the program.
In the remaining three pull-down lines, you will need to choose the version of the Android gadget that you use.
In the «Theme», choose «none», to get rid of compatibility problems between different versions of the SDK.
Press «Next» to go to the next step.
Uncheck the «Create custom icon» box so as not to waste time to create the icon of your application. Press «Next».
In a new window, you can see the «Activity» window. Choose the «Blank_activity» to start from scratch. Press «Next».
Our application will have only one «Activity» window, so in the new window you can leave everything without changes and choose «Finish».
At this stage, the application is created.
The debugging can be performed on the real device or on the emulator. Let us analyze the emulator option and configure it.
To do this, press «Window ->Android_Virtual_Device_Manager» and click «New». Fill the form in the new window. The values you will specify will affect the amount of recourses that will be given to the virtual phone by the emulator. Next, press «OK».
In the AVDM window, press «Start» to run the emulator. This procedure can take several minutes depending on the power of the PC. After this, you will see the desktop of the smartphone.
Filling in the Activity
In this window, we will see 2 buttons: «turn on the red LED» and «turn on the green LED». To add them, open the «res/layout/activity_main.xml» in the «Package Explorer» panel.
Drag two «ToggleButtons» to the form. After that, open the «activity_main.xml» tab. You will see the following code:
Let us change the names of the components to make them more understandable. You can change the android:id fields in the following way:
In addition, you can add the captions, change the size and the color of the text. The final code will look like:
These changes can also be performed in graphic mode by choosing the «Outline/Properties» tab.
The first launch
Open the launch settings by clicking «Run ->Run Configurations» Then, press the «Android Application» on the left side. You will see the «New configuration» window. In the right part of the window, select the «Target» tab and press «Launch_on_all_compatible_devices/AVD».
Press «Apply», and then click «Run». The program will be opened in the emulator.
At this stage, we can only test the appearance. The only remaining thing is to write the click processor.
Writing the code for Android
If you know, each application notifies the system about the rights that have to be granted for this program. The list of rights can be found in the manifest file called «AndroidManifest.xml». In this file, we will need to specify that we want to use Bluetooth. We will need to add a couple of extra lines to the file.
Writing the main code
Open the file «MainActivity.java». It already has a code.
We need to add these commands:
- Bluetooth activation.
- Button presses processing.
- Sending the information about the pressed button.
We are going to send 1 byte with the two-digit number to the Arduino. The first number is the number of pin, which diodes will be connected to. The second one is the state of the diode (1 – active, 0 – inactive).
The command number is calculated using the following method: if you press the red button, the number will be 60 (for the red light, we will choose the sixth pin of Arduino). 1 or 0 will be added depending on the state of the diode.
The situation is the same for the green button, but the number will be 70 instead of 60 (we will connect it to the 7th pin). As a result, we receive four possible commands: 70, 71, 60, 61.
The code, which implements everything written above, will look like this:
Writing the sketch
Bluetooth module receives data through the UART at 9600 bit/s. The wireless module is immediately ready to work, so it is not necessary to configure it. However, the sketch has to be able to:
- Receive data through UART.
- Activate the necessary diode depending on the generated code.
The features of the sketch uploading
To connect the Bluetooth-bee to the controller, we will use the same pins (1 and 0) that we used for the firmware. The «SERIAL_SELECT» slider on the «Wireless_Shield» has to be switched to the «USB» state. After firming, it will be necessary to switch it to the «MICRO» state.
The received result
As a result, we will receive such a picture.
In this article, we described the creation of a small Bluetooth robot control application for Android, which lights the LEDs via Bluetooth after the command.
It is the easiest thing you can do with this chip. Arduino is convenient because it can be a base for creating a wide arrangement of different robots with limited functions without any soldering. You will need only to connect the desired modules. If you have a basic knowledge of programming, you can create your own application for more complex systems, for example, Bluetooth rc car for Android. It will allow controlling the radio-controlled car or any other device that you will design by yourself from your smartphone.[rBlock name=after_video return=1]