Running your first Android application
Now that Android is installed in our UDOO board and that all development environments are configured, we can start to write and deploy our first Android application. The following is the default pattern of other developers when they start to dive into a new technology. We are going to write and deploy a simple Android application that prints Hello World!.
To bootstrap our first project, perform the following steps:
- In the main window of Android Studio, click on Start a new Android Studio project.
- In the Application name field, type
HelloWorld
; in Company domain, write your domain or example.com
if you don't have any at the moment. Then, click on Next. - In the form factors selection window, select Phone and Tablet and choose API 19: Android 4.4 (KitKat) in the Minimum SDK. Then, click on Next.
- In the add activity page, for the purpose of this hello world application, select a Blank Activity option and click on Next.
- In the Activity Options page, write HelloWorld in Activity Name and click on Finish.
Tip
In next chapters, we will create applications from scratch, so we have to bear in mind the previous steps because we are going to repeat the process multiple times in this book.
Now Android Studio will start to download all Gradle requirements to prepare our build system. When the process is completed, we get our first HelloWorld application.
Without writing any lines of code, we have already created a deployable application. Now we have to connect our UDOO board using a microUSB to the USB cable. If we take a look at the board, we see two different microUSB ports. The first one on the left, that we will use in the next chapters, connects our computer to the serial port of both processors, so we can use it to upload the Arduino program to the UDOO microcontroller or we can use it to access to the Android system shell. The activated communication for the serial port depends on the state of J18 jumper, whether it's plugged or not. Instead, the microUSB port on the right connects our computer to the i.MX 6 processor that runs Android and that we will use to upload our Android applications. You can find more information about the processor communication at the official UDOO website http://www.udoo.org/features/processors-communication/.
To connect our computer to the Android operating system for the application upload process, we need to use the right microUSB port colored in black in the following screenshot:
Like we do in a traditional Android application, we can click from the top menu on Run and then on Run app. At this point, we need to choose a running device, but unfortunately, our list of available devices is empty. This problem occurs because of how the internal communication between processors works.
After the boot time, the connection is enabled between the two processors and plugging the microUSB cable will produce no effect. This happens because Android doesn't use the internal UART serial port during the communication with Arduino. It uses the USB On-The-Go (OTG) bus that allows devices to act as host and let other components such as flash drive, mouse, keyboard, or Arduino, in this case, to connect through it.
The i.MX 6 processor is physically connected to the OTG bus while the other side of the bus is connected both to Arduino and to the external microUSB connector. The current active connection can be changed using a software piloted switch. When the external OTG port is enabled, Android can communicate with an external computer via microUSB port but it can't send back any data to onboard Arduino. On the contrary, when the external OTG port is disabled, Android can communicate with Arduino but the connection with the computer is interrupted.
The latter is our actual configuration and we need to switch the OTG port in order to enable the external communication with our computer to complete the application deployment. From Android, we have to go to the Settings menu and choose Developer options. There we need to select the External OTG port enabled checkbox. If the USB cable is connected, a popup will ask us to allow USB debugging. If this is our main computer, we may want to select Always allow from this computer and then click on OK. If this option is not checked, the popup will be shown every time we connect UDOO to our computer.
Note
Bear in mind that every time we need to deploy our Android application, we need to enable the external OTG port. On the contrary, when our application is deployed and we need Android to communicate with Arduino, we need to disable the external OTG port.
Now that our computer can see the UDOO board as a traditional Android device, we can try to deploy our application again. This time, in the Choose Device dialog, we can find a Freescale UDOO Android device. Select it and click on OK. Our first deployment is complete and now we can see the HelloWorld application on the connected monitor.