开发者

Display an Android Sensors List

I'm trying to display a list of available sensors but it's like there are not!

I was thinking that it was because of the emulator, but i tried it on the phone and the result is the same.

private SensorManager mSensorManager;
TextView mSensorsTot,mSensorAvailables;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Get the texts fields of the layout and setup to invisible
    mSensorsTot   = (TextView) findViewById(R.id.s开发者_如何学Pythonensoritot);
    mSensorAvailables  = (TextView) findViewById(R.id.sensoridisponibili);

    // Get the SensorManager 
    mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);

    // List of Sensors Available
    List<Sensor> msensorList = mSensorManager.getSensorList(SensorManager.SENSOR_ALL);

    // Print how may Sensors are there
    mSensorsTot.setText(msensorList.size()+" "+this.getString(R.string.sensors)+"!");

    // Print each Sensor available using sSensList as the String to be printed
    String sSensList = new String("");
    Sensor tmp;
    int x,i;
    for (i=0;i<msensorList.size();i++){
     tmp = msensorList.get(i);
     sSensList = " "+sSensList+tmp.getName(); // Add the sensor name to the string of sensors available
    }
    // if there are sensors available show the list
    if (i>0){
     sSensList = getString(R.string.sensors)+":"+sSensList;
     mSensorAvailables.setText(sSensList);
    }
}


The constant SensorManager.SENSOR_ALL is deprecated and doesn't seem to work anymore.

Query the sensor list with Sensor.TYPE_ALL instead and it should work (my emulator returns a "Goldfish 3-axis Accelerometer").


You can try this code:-

package com.example.sensor;

import java.util.List;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {

SensorManager smm;
List<Sensor> sensor;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    smm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    lv = (ListView) findViewById (R.id.listView1);
    sensor = smm.getSensorList(Sensor.TYPE_ALL);
    lv.setAdapter(new ArrayAdapter<Sensor>(this, android.R.layout.simple_list_item_1,  sensor));
}
}

Just put a list view in your xml layout.


You can try listing the sensors as below:-

String cAPP_FOLDER = Path of your folder

String cSENSOR_FILE = Name of your file

// Create a file to export the sensor info.
File fp = new File(cAPP_FOLDER + cSENSOR_FILE);
fp.createNewFile();
PrintWriter pw = new PrintWriter(new FileWriter(fp, true));

SensorManager oSM = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensorsList = oSM.getSensorList(Sensor.TYPE_ALL);
for (Sensor s : sensorsList) {
    pw.write(s.toString() + "\n");
}
pw.close();

//////////////////////////////////////////////////////////////////////////////

// OUTPUT FILE

{Sensor name="BOSCH Accelerometer Sensor ", vendor="Bosch Sensortec GmbH", version=2, type=1, maxRange=156.88, resolution=5.984497E-4, power=0.13, minDelay=10000}

{Sensor name="BOSCH Magnetic Field Sensor", vendor="Bosch Sensortec GmbH", version=2, type=2, maxRange=1600.0, resolution=0.3, power=0.5, minDelay=40000}

{Sensor name="BOSCH Orientation Sensor", vendor="Bosch Sensortec GmbH", version=2, type=3, maxRange=360.0, resolution=1.0, power=0.63, minDelay=5000}

{Sensor name="BOSCH Gyroscope Sensor", vendor="Bosch Sensortec GmbH", version=2, type=4, maxRange=2500.0, resolution=0.0038146973, power=5.0, minDelay=5000}

{Sensor name="BOSCH Gravity Sensor", vendor="Bosch Sensortec GmbH", version=2, type=9, maxRange=19.61, resolution=5.984497E-4, power=5.63, minDelay=5000}

{Sensor name="BOSCH Linear Acceleration Sensor", vendor="Bosch Sensortec GmbH", version=2, type=10, maxRange=19.61, resolution=5.984497E-4, power=5.63, minDelay=5000}

{Sensor name="BOSCH Rotation Vector Sensor", vendor="Bosch Sensortec GmbH", version=2, type=11, maxRange=1.0, resolution=5.9604645E-8, power=5.63, minDelay=5000}

{Sensor name="BOSCH Magnetic Field Uncalibrated Sensor", vendor="Bosch Sensortec GmbH", version=2, type=14, maxRange=1600.0, resolution=0.3, power=0.5, minDelay=40000}

{Sensor name="BOSCH Game Rotation Vector Sensor", vendor="Bosch Sensortec GmbH", version=2, type=15, maxRange=1.0, resolution=5.9604645E-8, power=5.63, minDelay=5000}

{Sensor name="BOSCH Gyroscope Uncalibrated Sensor", vendor="Bosch Sensortec GmbH", version=2, type=16, maxRange=2500.0, resolution=0.0038146973, power=5.0, minDelay=5000}

{Sensor name="BOSCH Geomagnetic Rotation Vector Sensor", vendor="Bosch Sensortec GmbH", version=2, type=20, maxRange=1.0, resolution=5.9604645E-8, power=5.63, minDelay=5000}

{Sensor name="LIGHT", vendor="MTK", version=1, type=5, maxRange=10240.0, resolution=1.0, power=0.13, minDelay=0}

{Sensor name="PROXIMITY", vendor="MTK", version=1, type=8, maxRange=1.0, resolution=1.0, power=0.13, minDelay=0}

{Sensor name="SIGNIFICANT_MOTION", vendor="MTK", version=1, type=17, maxRange=85.0, resolution=0.1, power=0.5, minDelay=-1}


getSensorList (int type) of SensorManager class gives a list of sensors ==> List. (To get the list of available sensors of a certain type.)

Use Sensor.TYPE_ALL to get all the sensors. Make multiple calls to get sensors of different types.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜