In this exercise we will use Android Studio to create a single screen Android UI app that will access the sensor class to get the number of established sensors on a device. Don’t worry if you don’t have any sensors, It’s just that its more interesting than doing yet another “Hello World”. My system didn’t have any sensors enabled.

 

Overview

Whilst most newbie tutorials focus upon developing UI capabilities, at embedded101.com we are interested in hardware. To access the sensors library, we need to add the classes

  • android.hardware.Sensor
  • android.hardware.SensorManager

 

  • Target System: DragonBoard 410c, in factory condition (running Android ..)
  • Development System: Windows 10, Version 1511
  • Development Environment: Android Studio, installation covered in previous b;log.
  • Deploy/Debug connectivity: ADB over USB (ADB setup also covered in previous blog)

 

Summary:

We will create a single screen app with three Large TextViews and one button.

The second TextView displays the number of sensors when the button is pressed.

 

HowTo:

  • In Android Studio, create a new project:
    - Tablet- Phone
    - Choose the Empty template
  • Examine the java and res- Layout trees when the project is created. In both focus upon the Main Activity. This is where we will be coding.

Comment. For Windows programmers, each activity is a page (or form) and the java code is the code behind.

image

 

Lets start with the UI.

  • Open res/layout/main_activity.xml
  • View it in Design and Text mode

Comment: For XAML developers, see the similarities.

 

  • In Design mode, add three new Extra Large TextViews all of the same line.

In Text mode, modify them as follows:

  • Remove the original TextView (first one)
  • textview1: Set its text to “There are “
  • textView2: Set its text to “137”
  • textView3: Set its text to “ sensors in this system ”.

nb The control names are defined in the id property.

eg android:id="@+id/textView3" means that TextView is textView3

  • To highlight the number of sensors, set TextView2 color to blue, add the following within its settings:
android:textColor="#0000FF"

Note that a blue box appears on left. Could we have that for color properties in the XAML editor in Visual Studio?

So in Design mode, it should now read:
There are 137 sensors in this system.

  • Add a button to the activity.
  • Set its text to “How many?”
  • Check that its name is button.
  • image
  • image

 

Now for the java code

Open java/……/MainActivity

At the top (within) the MainActivity class insert:

public Button butt1;

private SensorManager mSensorManager;

public void Init() {
}

 

  • And at the end of OnCreate() insert:
Init();

We will add an event handler the button press in Init().

  • Insert the following into Init():

 

butt1 = (Button) findViewById(R.id.button);
butt1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    
    //Get number of sensors in system


    //Update the display


    //ToDo: List sensors and their types
    
    });

 

  • Under the first comment insert:
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
    int i = deviceSensors.size();

 

  • Then under the second comment insert,
    String s = Integer.toString(i);
    TextView textView = (TextView) findViewById(R.id.textView2);
    textView.setText(s);
  • The third comment is left for you to implement!

 

Note: If any elements of this code have a highlight, press alt-enter.
You will get options to resolve such as add an import.


Set the app in Debug mode and build it. Resolve any issues.

Note that to build in Release mode you have to configure code signing.

image

image

image

Test the app now.

Note That when connected this way the OTG USB precludes the USB Host and so you get no keyboard/mouse via USB. If you have previously paired a Bluetooth keyboard and mouse that should work. (The mouse did for me!)

  • You should check in Settings that Developer Mode is enabled (towards the bottom of System Settings.

 

  • Connect the target to the dev machine via USB and power it up.

 

  • Just run the app (not Debug). This should deploy the and run it.
  • If you press the button the 137 should change to 0 unless you have some sensors configured. If you want to get a sensor going you have to build the kernel for it. See:

https://www.youtube.com/embed/1c3Epva9hCc?rel=0&width=560&height=315&wmode=transparent&iframe=true&autoplay=1

https://developer.qualcomm.com/download/db410c/interfacing-grove-digital-light-i2c-sensor-application-note.pdf

 

I haven’t got debugging to work yet. When I run in debug mode it deploys but fails to connect. (ToDo)

 

Codeplex

I’ll post this project on Codeplex

 

Next: Musings with GPIO using ADB Shell