显示Android传感器列表

我试图显示一个可用的传感器列表,但它就像没有!
我想这是因为模拟器,但我试过在手机上,结果是一样的。

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.sensoritot); 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); } } 

Solutions Collecting From Web of "显示Android传感器列表"

不断的SensorManager.SENSOR_ALL被弃用,似乎不再工作。

Sensor.TYPE_ALL查询传感器列表,它应该工作(我的模拟器返回一个“金鱼3轴加速度计”)。

你可以试试这个代码:

 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)); } } 

只需在你的XML布局中放置一个列表视图。

您可以尝试列出传感器如下: –

stringcAPP_FOLDER =文件夹的path

stringcSENSOR_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(); 

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

// 输出文件

{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}

{传感器名称=“BOSCH磁场传感器”,供应商=“Bosch Sensortec GmbH”,版本= 2,types= 2,最大范围= 1600.0,分辨率= 0.3,功率= 0.5,最小延迟= 40000}

{传感器名称=“BOSCH方向传感器”,供应商=“Bosch Sensortec GmbH”,版本= 2,types= 3,最大范围= 360.0,分辨率= 1.0,功率= 0.63,最小延迟= 5000}

{传感器名称=“BOSCH陀螺仪传感器”,供应商=“Bosch Sensortec GmbH”,版本= 2,types= 4,maxRange = 2500.0,分辨率= 0.0038146973,功率= 5.0,最小延迟= 5000}

{传感器名称=“BOSCH Gravity Sensor”,供应商=“Bosch Sensortec GmbH”,版本= 2,types= 9,maxRange = 19.61,分辨率= 5.984497E-4,功率= 5.63,minDelay = 5000}

{传感器名称=“BOSCH Linear Acceleration Sensor”,供应商=“Bosch Sensortec GmbH”,版本= 2,types= 10,maxRange = 19.61,分辨率= 5.984497E-4,功率= 5.63,minDelay = 5000}

{Sensor name =“BOSCH Rotation Vector Sensor”,vendor =“Bosch Sensortec GmbH”,版本= 2,types= 11,maxRange = 1.0,

{传感器名称=“BOSCH磁场未校准传感器”,供应商=“Bosch Sensortec GmbH”,版本= 2,types= 14,最大范围= 1600.0,分辨率= 0.3,功率= 0.5,最小延迟= 40000}

{Sensor name =“BOSCH Game Rotation Vector Sensor”,vendor =“Bosch Sensortec GmbH”,版本= 2,types= 15,maxRange = 1.0,分辨率= 5.9604645E-8,功率= 5.63,minDelay = 5000}

{传感器名称=“BOSCH陀螺仪未校准传感器”,供应商=“Bosch Sensortec GmbH”,版本= 2,types= 16,最大范围= 2500.0,分辨率= 0.0038146973,功率= 5.0,最小延迟= 5000}

{传感器名称=“BOSCH地磁旋转vector传感器”,供应商=“Bosch Sensortec GmbH”,版本= 2,types= 20,maxRange = 1.0,分辨率= 5.9604645E-8,功率= 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}

{传感器名称=“SIGNIFICANT_MOTION”,厂商=“MTK”,版本= 1,types= 17,最大范围= 85.0,分辨率= 0.1,功率= 0.5,最小延迟= -1)

SensorManager类的getSensorList(inttypes)给出传感器列表==> List。 (要获取某种types的可用传感器的列表)

使用Sensor.TYPE_ALL获取所有传感器。 多次调用以获取不同types的传感器。