Getting Sensor Data [60-90 Mins]

Getting Sensor Data [60-90 Mins]

COM1032 Lab #5-1: Getting sensor data [60-90 Mins] What you'll learn Query the sensor manager for available sensors, and retrieve information about specific sensors. Register listeners for sensor data. React to incoming sensor data. What you'll do Create an app that lists the available device sensors. Run the app on a device and on the emulator to view sensors. Create a second app that gets data from the light and proximity sensors, and displays that data. Interact with the device and note the changes in sensor data. Run the app in the emulator and learn about the emulator's virtual sensors. 1. Introduction Many Android-powered devices include built-in sensors that measure motion, orientation, and environmental conditions such as ambient light or temperature. These sensors can provide data to your app with high precision and accuracy. Sensors can be used to monitor three-dimensional device movement or positioning, or to monitor changes in the environment near a device, such as changes to temperature or humidity. For example, a game might track readings from a device's accelerometer sensor to infer complex user gestures and motions, such as tilt, shake, or rotation. In this codelab you learn about the Android sensor framework, which is used to find the available sensors on a device and retrieve data from those sensors. The device camera, fingerprint sensor, microphone, and GPS (location) sensor all have their own APIs and are not considered part of the Android sensor framework. 2. App overview You will build two apps in this codelab. The first app lists the available sensors on the device or emulator. The list of sensors is scrollable, if it is too big to fit the screen. This material is based on Android Code labs: https://codelabs.developers.google.com/android-training/ The second app, modified from the first, gets data from the ambient light and proximity sensors, and displays that data. Light and proximity sensors are some of the most common Android device sensors. 3. Task 1. List the available sensors In this task, you build a simple app that queries the sensor manager for the list of sensors available on the device. 1.1 Build the app 1. Create a new Android project. Call it SensorSurvey and use the Empty activity template. 2. Open res/layout/activity_main.xml. 3. Add a margin of 16 dp to the constraint layout. android:layout_margin="16dp" 4. Delete the existing TextView. 5. Add a ScrollView element inside the constraint layout. Give it these attributes: Attribute Value android:layout_width "match_parent" android:layout_height "match_parent" app:layout_constraintBottom_toBottomOf "parent" app:layout_constraintTop_toTopOf "parent" This material is based on Android Code labs: https://codelabs.developers.google.com/android-training/ app:layout_constraintLeft_toLeftOf "parent" app:layout_constraintRight_toRightOf "parent" The ScrollView is here to allow the list of sensors to scroll if it is longer than the screen. 6. Add a TextView element inside the ScrollView and give it these attributes: Attribute Value android:id "@+id/sensor_list" android:layout_width "wrap_content" android:layout_height "wrap_content" android:text "(placeholder)" This TextView holds the list of sensors. The placeholder text is replaced at runtime by the actual sensor list. The layout for your app should look like this screenshot: 7. Open MainActivity and add a variable at the top of the class to hold an instance of SensorManager: private SensorManager mSensorManager; The sensor manager is a system service that lets you access the device sensors. This material is based on Android Code labs: https://codelabs.developers.google.com/android-training/ 8. In the onCreate() method, below the setContentView() method, get an instance of the sensor manager from system services, and assign it to the mSensorManager variable: mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 9. Get the list of all sensors from the sensor manager. Store the list in a List object whose values are of type Sensor: List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL); The Sensor class represents an individual sensor and defines constants for the available sensor types. The Sensor.TYPE_ALL constant indicates all the available sensors. 10. Iterate over the list of sensors. For each sensor, get that sensor's official name with the getName() method, and append that name to the sensorText string. Each line of the sensor list is separated by the value of the line.separator property, typically a newline character: StringBuilder sensorText = new StringBuilder(); for (Sensor currentSensor : sensorList ) { sensorText.append(currentSensor.getName()).append( System.getProperty("line.separator")); } 11. Get a reference to the TextView for the sensor list, and update the text of that view with the string containing the list of sensors: TextView sensorTextView = (TextView) findViewById(R.id.sensor_list); sensorTextView.setText(sensorText); 1.2 Run the app on a device and in the emulator Different Android devices have different sensors available, which means the SensorSurvey app shows different results for each device. In addition, the Android emulator includes a small set of simulated sensors. 1. Run the app on a physical device. The output of the app looks something like this screenshot: This material is based on Android Code labs: https://codelabs.developers.google.com/android-training/ In this list, lines that begin with a letter/number code represent physical hardware in the device. The letters and numbers indicate sensor manufacturers and model numbers. In most devices the accelerometer, gyroscope, and magnetometer are physical sensors. Lines without letter/number codes are virtual or composite sensors, that is, sensors that are simulated in software. These sensors use the data from one or more physical sensors. So, for example, the gravity sensor may use data from the accelerometer, gyroscope, and magnetometer to provide the direction and magnitude of gravity in the device's coordinate system. 2. Run the app in an emulator. The output of the app looks something like this screenshot: This material is based on Android Code labs: https://codelabs.developers.google.com/android-training/ Because the Android emulator is a simulated device, all the available sensors are virtual sensors. "Goldfish" is the name of the emulator's Linux kernel. 3. Click the More button (three horizontal dots) on the emulator's control panel. The Extended Controls window appears. 4. Click Virtual Sensors. This material is based on Android Code labs: https://codelabs.developers.google.com/android-training/ This window shows the settings and current values for the emulator's virtual sensors. Drag the image of the device to simulate motion and acceleration with the accelerometer. Dragging the device image may also rotate the main emulator window. 5. Click the Additional Sensors tab. This material is based on Android Code labs: https://codelabs.developers.google.com/android-training/ This tab shows the other available virtual sensors for the emulator, including the light, temperature, and proximity sensors. You use more of these sensors in the next task. 4. Task 2. Get sensor data The Android sensor framework provides the ability for your app to register for and react to changes in sensor data. In this task you modify your existing app to listen to and report values from the proximity and light sensors. The light sensor measures ambient light in lux, a standard unit of illumination. The light sensor typically is used to automatically adjust screen brightness. The proximity sensor measures when the device is close to another object. The proximity sensor is often used to turn off touch events on a phone's screen when you answer a phone call, so that touching your phone to your face does not accidentally launch apps or otherwise interfere with the device's operation. 2.1 Modify the layout 1. Open res/layout/activity_main.xml. 2. Delete the ScrollView and TextView elements from the previous app. 3. Add a TextView and give it the attributes in the following table. Extract the string into a resource called "label_light". This text view will print the current value from the light sensor. Attribute Value android:id "@+id/label_light" android:layout_width "wrap_content" android:layout_height "wrap_content" android:text "Light Sensor: %1$.2f" app:layout_constraintLeft_toLeftOf "parent" app:layout_constraintTop_toBottomOf "parent" The "%1$.2f" part of the text string is a placeholder code. This code will be replaced in the Java code for your app with the placeholder filled in with an actual numeric value. In this case the placeholder code has three parts: %1: The first placeholder. You could include multiple placeholders in the same string with %2, %3, and so on. $.2: The number format. In this case, .2 indicates that the value should be formatted with only two digits after the decimal point. f: Indicates that the value to display is a floating-point number. Use s for string values and d for decimal values. The part of the string that is not made up of placeholders ("Light Sensor: ") is passed through to the new string. You can find out more about placeholders and formatting codes in the Formatter documentation. This material is based on Android Code labs: https://codelabs.developers.google.com/android-training/ 4. Copy and paste the TextView element. Change the attributes in the following table. Extract the string into a resource called "label_proximity". This text view will print values from the proximity sensor. Attribute Value android:id "@+id/label_proximity" android:text "Proximity Sensor: %1$.2f" app:layout_constraintTop_toBottomOf "@+id/label_light" The layout for your app should look like this screenshot: 5. Open res/values/strings.xml and add this line: <string name="error_no_sensor">No sensor</string> You'll use this message in the next task when you test if a sensor is available. 2.2 Get the sensors In this task, you modify the activity's onCreate() method to gain access to the light and proximity sensors.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    14 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us