Android

We begin by creating an empty project in the Android Studio. We have already done this exercise in the last chapter, so we will not be covering it here again.

While creating the Android Project, please choose the Minimum Api Level(minsdkversion) to be 23 since it supports all the Api calls that we would be using in this section.

Once we have finished creating our empty project (we have chosen the name of AndroidBLEServiceExplorer for our project with a package name of packt.com.androidbleserviceexplorer as shown below), as a first step, we can define some simple user interface, which will keep us informed of what is going on:

  1. Configuring the UI: We will start by configuring the UI via the following steps. User interfaces in Android are primarily defined in xml files. If you have followed the project creation process exactly as defined in the last chapter, then, under the layout folder, there should be an activity_main.xml file as shown in the following screenshot(the code in the file might be different depending on the version of Android Studio. However, this should be of little concern since we are going to remove the code present in this file anyways):
Figure 3: Activity UI XML
  1. We will create a really simple layout to see the results and begin by stripping the code in activity_main.xml file. Firstly, add RelativeLayout as a root element of your UI:
        <RelativeLayout       
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:paddingBottom=
"@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=
"packt.com.androidbleserviceexplorer.
MainActivity">
</RelativeLayout>

A thorough understanding of Android's RelativeLayout is not necessary for this exercise, as there are plenty of resources out there which can help you in understanding the internals of RelativeLayout, the most popular being the Android developer website:https://developer.android.com/guide/topics/ui/layout/relative.html.

An important thing to keep in consideration while implementing the code mentioned above, the following line tools:context="packt.com.androidbleserviceexplorer.MainActivity" may throw an error if you directly copy the code as shown above and if your Project's package name is NOT packt.com.androidservicebleexplorer. Hence, please use the appropriate Package name in the aforementioned line.

  1. To add to our interface design, we will be creating two buttons to give us the ability to stop and start the scanning process. Now, we add two buttons to RelativeLayout by adding the following two elements as direct children(inside the RelativeLayout Tag) to the parent RelativeLayout:
        <Button android:layout_width="100dp" 
android:layout_height="50dp"
android:text="Scan"
android:id="@+id/StartScanButton"
android:layout_marginTop="40dp"
android:layout_alignParentBottom="true"/>

<Button android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="Stop Scanning"
android:id="@+id/StopScanButton"
android:layout_marginTop="40dp"
android:layout_alignParentBottom="true"
android:layout_alignRight=
"@+id/StartScanButton"/>
  1. Next, we will also need to see what devices were found and, hence, we will also add the code for a ListView, just below the code for Buttons:
        <ListView
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/StartScanButton"
android:id="@+id/deviceListView"
android:stackFromBottom="false"/>

Although we are adding the ListView below the buttons in the code, it will still appear above the buttons due to its placement instructions, which we provide in this line: android:layout_above="@+id/StartScanButton".

  1. Configuring the App Permissions: Since our UI is ready, we will now move on to configuring app permissions in the AndroidManifest.xml file.

On the Android operating system, each application runs in its own sandbox. If apps need to use any system or otherwise critical functions, then they need to explicitly request these permissions from the Android System via user intervention. The user can decide to grant these permissions or otherwise.

  1. We will need the permissions related to Bluetooth and Location for our app. Locate the AndroidManifest.xml file(which can be found under under the manifests folder located in the root/near the top of the Project) in the project explorer. Add the following lines to the AndroidManifest.xml directly inside the <manifest> tag:
        <uses-permission   android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
/>
<uses-permission android:name=
"android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name=
"android.permission.ACCESS_FINE_LOCATION" />

You must be wondering why the location permission is needed, the reason being that, Bluetooth Low Energy beacons can be used to get location information using BLE broadcast UUID data and an internet connection (for example, iBeacon and AltBeacon). Since this is possible and the data can be acquired via scan, a permission for location is required.

To read more about permissions related to Bluetooth Low Energy on the Android platform, please visit: https://developer.android.com/guide/topics/connectivity/bluetooth-le.html#setup
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.124.244