Time for action – knowing individual sensors' capabilities

Android phones are manufactured by different OEMs, which use different vendors to get their sensors. It is very much possible that two different Android phones have different gyroscope sensors, which will have different ranges and other properties. Before developing a universal logic based on sensors, it's important to keep in mind sensor's individual properties and capabilities, which may vary from device to device. In this section, we will explore the common methods for finding out the properties and capabilities of a sensor:

  1. We will show the sensor properties in the individual TextView on the screen. In the following code snippet, the TextViewSensor, and SensorManager variables are declared:
          public class SensorCapabilityActivity extends Activity { 
     
            private SensorManager mSensorManager; 
            private int mSensorType; 
            private Sensor mSensor; 
            private TextView mSensorNameTextView; 
            private TextView mSensorMaximumRangeTextView; 
            private TextView mSensorMinDelayTextView; 
            private TextView mSensorPowerTextView; 
            private TextView mSensorResolutionTextView; 
            private TextView mSensorVendorTextView; 
            private TextView mSensorVersionTextView; 
    
  2. In the OnCreated() method, we instantiate the TextViewSensor, and SensorManager objects. We use the following sensor methods (getName()getMaximumRange()getMinDelay()getPower()getResolution()getVendor(), and getVersion()) of the Sensor object to get sensor properties and show the values in their respective TextView. We receive the sensor type through the getIntExtra() API of intent from the previous SensorListActivity:
          @Override 
          protected void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.capability_layout); 
            Intent intent = getIntent(); 
    
            mSensorType = intent.getIntExtra(getResources()
            .getResourceName(R.string.sensor_type), 0); 
            mSensorManager = (SensorManager)this.getSystemService
            (Context.SENSOR_SERVICE); 
          
            mSensor = 
            mSensorManager.getDefaultSensor(mSensorType); 
         
            mSensorNameTextView = (TextView)findViewById
            (R.id.sensor_name); 
            mSensorMaximumRangeTextView = (TextView)findViewById
            (R.id.sensor_range); 
            mSensorMinDelayTextView = (TextView)findViewById
            (R.id.sensor_mindelay); 
            mSensorPowerTextView = (TextView)findViewById
            (R.id.sensor_power); 
            mSensorResolutionTextView = (TextView)findViewById
            (R.id.sensor_resolution); 
            mSensorVendorTextView = (TextView)findViewById
            (R.id.sensor_vendor); 
            mSensorVersionTextView = (TextView)findViewById
            (R.id.sensor_version); 
         
            mSensorNameTextView.setText(mSensor.getName()); 
            mSensorMaximumRangeTextView.setText(String.valueOf
            (mSensor.getMaximumRange())); 
            mSensorMinDelayTextView.setText(String.valueOf
            (mSensor.getMinDelay())); 
            mSensorPowerTextView.setText(String.valueOf
            (mSensor.getPower())); 
            mSensorResolutionTextView.setText(String.valueOf
            (mSensor.getResolution())); 
            mSensorVendorTextView.setText(String.valueOf
            (mSensor.getVendor())); 
            mSensorVersionTextView.setText(String.valueOf
            (mSensor.getVersion())); 
          } 
    
  3. After knowing all the sensor properties, we would like to play with the sensor values. The following onClickSensorValues() method takes us to the new activity, which will get the values of the sensor, whose properties are shown in this activity. This onClickSensorValues() method is a custom method,which is attached to the button on the screen using the XML onClick tag. We pass the sensor type to the next activity so that it can identify the right sensor type:
      public void onClickSensorValues(View v) 
      { 
        Intent intent = new Intent(getApplicationContext(), 
        SensorValuesActivity.class); 
   
        intent.putExtra(getResources().getResourceName
        (R.string.sen sor_type), mSensorType); 
     
        startActivity(intent); 
      } 

What just happened?

We used the sensor methods (getName(),  getMaximumRange(),  getMinDelay(),  getPower(),  getResolution(),  getVendor(),  getVersion()) of the Sensor object and displayed the values on the screen. The following screenshot shows the layout file, showing the capabilities of an accelerometer sensor. These values play an important role when you are developing a universal logic, which should work with all types of sensors on different devices:

What just happened?

..................Content has been hidden....................

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