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.
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
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.
Comment. For Windows programmers, each activity is a page (or form) and the java code is the code behind.
Comment: For XAML developers, see the similarities.
In Text mode, modify them as follows:
nb The control names are defined in the id property.
eg android:id="@+id/textView3" means that TextView is textView3
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.
Open java/……/MainActivity
At the top (within) the MainActivity class insert:
public Button butt1; private SensorManager mSensorManager; public void Init() { }
Init();
We will add an event handler the button press in 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 });
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL); int i = deviceSensors.size();
String s = Integer.toString(i); TextView textView = (TextView) findViewById(R.id.textView2); textView.setText(s);
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.
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!)
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)
I’ll post this project on Codeplex
Next: Musings with GPIO using ADB Shell