LED control using Arduino and Android ( part 2 : Android apk )

In this Part we going see step by step how to making android apk.

Ghassen Ben Taher

Android: New Project
  • Open Android Studio and create a new Project:  File > New Project.
  • A pop up Windows will appear. Change the Application Name and Company Name:
  • Click next to choose the target of the application.
  • Click next and choose a Blank Activity.
  • Click next and rename the Activity Name to “DeviceList”.
  • Now click “finish” and the Project will be create
Android: Layout Part 1.

When the build is finished, a “Hello world!” screen will be open ,  to create the layout of the apk, we need to add :
  • TextView to display some hint to the user;
Click twice the TextView to change the text. A box will appear:
Text = The text to be displayed.
Id = the id of this widget.

  • Button to show the paired devices.
Click twice the Button to change the text. A box will appear:
Text = The text to be displayed.
Id = the id of this widget.

  • ListView to show the paired devices;

Now the main activity is finished, you can see that all widget used are shown on Components Tree.

Code source  activity_device_list.xml on GITHUB

Android: Class Code Part 1.

On the left side there’s a folder called “app “, open it and you’ll see other folder called “java”.
According to Android documents, an Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use-cases:

 To start an activity:

An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data.

  • To start a service:  

A Service is a component that performs operations in the background without a user interface. You can start a service to perform a one-time operation (such as download a file) by passing an Intent to startService(). The Intent describes the service to start and carries any necessary data.

  • To deliver a broadcast:  

A broadcast is a message that any app can receive. The system delivers various broadcasts for system events, such as when the system boots up or the device starts charging. You can deliver a broadcast to other apps by passing an Intent to sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().




We need to create na new activity. If you’re following this tutorial
step-by-step, you’re seeing anerror at:

Intent i = new Intent(DeviceList.this,ledControl.class);
The error happens because there’s no ledControl classin the package.Let’s create a new activity called ledControl


Android: Layout Part 2
.
  • Go to app > java > com.exemple.ghassenbentaher.androidbluetoothledcontrol
  • Right click,NewActivity > Blank Activity

Name it to ledControl and finish;
All over again, a “Hello world!” screen will be seen
This second layout will have three buttons,one TextView and a seekbar:

  • Turn On= Turns the LED On;
  • Turn Off= Turns the LED Off;
  • Disconnect= Closes Bluetooth Connection;
  • Indicator;
  • Brightness= control the brightness.
So Let’s add them:

Code source activity_led_control.xml on GITHUB
Android: Class Code Part 2

Android Manifest:

Every application must have an AndroidManifest.xml file (with precisely that name) in its root directory.
The manifest file presents essential information about your app to the Android system,information the system must have before itcan run any of the app’s code.
This apk uses Bluetooth Adapter and it is not available in emulator, you must test it in a running device, but before you have to add some users-permissions, otherwise the apk will crash.

In App folder, open manifests > AndroidManifest.xml
Add the following code to allow user-permision to use Bluetooth Device
  • <uses-permission android:name= “android.permission.BLUETOOTH_ADMIN”/>
  • <uses-permission android:name= “android.permission.BLUETOOTH”/>
  • Rebuild the project at: Build Menu > Rebuild Project
Now you can run it in your device.
you can see the full project on github .








Comments