Lab 1.4: Indoor localization

Overview

We learned in lab 1.2 how to obtain accurate GPS location. One of the challenges that one might face is indoor localization because GPS signals can’t be detected.  While there are some triangulation techniques that gets rough location estimates based on wifi and base station data; reasonable indoor navigation, however, in not achievable with these techniques.

An interesting way to tackle this issue is to install small Bluetooth transmitter devices at fixed locations that transmit periodically Bluetooth beacons (or simply signals). Each Bluetooth device can be labeled or attached to a point of interest in a building. Your phone receives beacons from multiple devices at different intensities that are proportional to distances to transmitters. Such correlation can be used to get reasonable indoor location.

In this lab, we will use a device called Estimote and its android SDK to develop an Android app that discovers all Bluetooth beacons in the range and display their IDs along with their distances from the phone, RSSI, and signal power.

So, lets start building our application.

 

Create Android Studio Project

  • Create a new android project (name it Lab-1.4, for instance). Set minSdkVersion to 18.
  • Copy estimote-sdk-.jar to your application’s libs directory. Use file explorer/manager to locate libs directory inside the project’s directory if you can’t find it inside Android Studio.
  • Also, add the following line to dependencies

 

AndroidManifest.xml

Add following permissions in order to be able to scan for Bluetooth beacons.

Add this declaration so that this app is only usable on phones with Bluetooth Low Energy.

Please note that the above declarations should be added outside the Application tag (i.e. <application></application>) of your AndroidManifest.xml.

Now add the following service declaration inside the application tag. The service is responsible for scanning beacons:

 

Layout Design

We will create a simple layout.

  • In activity_main.xml, create a single ListView for displaying discovered beacons. Set its ‘anrdoid:id’ to “@+id/listView”.
    .

List is one of the most common UI patterns, which is being used extensively to display the collection of data elements in rows. In android ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source (the Bluetooth beacons in our case).

Adapter is basically bridge between UI components and the data source that fill data into UI Component. We will use a custom Adapter to make our customizable list view for nice display of beacons data.

First we need to know what data we want to display in the list? Let’s say we want to display Beacon’s ID, RSSI, Signal Power, and distance from phone.

Then we need to create a custom row layout for displaying above parameters in each row.

  • Create the second layout file under app=>res=>layout, name it “list_item.xml”, and add the following code to it.
  • You will need to download/copy beacon_gray.png to app=>res=>drawable or you will get an error message due to missing icon in ImageView tag.

 

Writing a custom adapter

We will now create custom Adapter to make our customizable list view using the custom row layout that we just created.

Create a new java class inside (app >> java >> ae.masdar.cis508.lab_14), call it DeviceListAdapter , and add the following code.

We can see in line-11 how our custom row layout is linked to the adapter. Whenever data is received from beacons, replaceWith() is called to insert data into the list. We will not go into further detail about list views and adapters. You should check out link1 and link2 to learn about them.

Now that we are ready with adapter and layout, we can complete the remaining part.

 

MainActivity.java

Dclare the following variables inside MainActivity class.

The first two lines define the region where Bluetooth beacons are deployed. BeaconManager (from Estimote SDK) provides the necessary interfaces between the beacons and Android devices. DeviceListAdapter is custom adapter that we just created.

Next add the following code to onCreate().

This sets up a listener for Bluetooth beacons. Whenever new beacons are discovered, adapter.replaceWith() is called to update the list of discovered beacons.

Now add the following function which is called on application launch to first check if Bluetooth LE is supported by the phone and whether or not Bluetooth is turned on. In case of sucess, BeaconManager strart scanning the area for beacons.

Finally, add the following functions to the class to close services and clean up whenever the application is closed.

 

Once you have added these functions to the main class, you are done. Install the app on your phone and you should be able to see the nearby beacons.

 

Lab Exercise 1.4

  1. The application that we just developed currently only notifies if Bluetooth service is turned off. The user has to manually turn it on. Change the application so that the application asks the user to turn the Bluetooth on if it’s turned off.
  2. Extend the application such that when you click on any beacon from the list, a new screen appears, displaying beacons’s properties (see fig).   exercise-1.4

Hints:

  • You will need to register a callback to be invoked when an item in the list has been clicked.
  • In createOnItemClickListener(), create an Intent to start a new activity (call it DetailActivity for example) and pass the position of the clicked item to the new activity.
  • Create a simple layout for the new activity (call it activity_detail for example) and update the manifest file accordingly. The layout should contain one textView for displaying status messages, and another textView for displaying beacon’s detail as shown in above figure.
  • In the new activity, fetch the beacon’s details and display it via textView.
  • Most of the contents for this lab is taken from the Estimote Android SDK demo. In fact, this exercise is stripped down version of the SDK demo. The objective of the exercise is to encourage you to interact with and understand the SDK.

Guidelines:

  • The exercise should be done individually.
  • Copy the final exercise files into “Lab-1.4” directory.
  • Add, commit, and push your changes to the remote server
  • The deadline is before the next lab.

 

References

  • https://github.com/Estimote/Android-SDK/>
  • http://estimote.com/api/>
  • http://estimote.com/>