android 获取所有传感器数据

main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="加速度"
    android:id="@+id/edt1"
    />
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="磁场"
    android:id="@+id/edt2"
    />
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="定位"
    android:id="@+id/edt3"
    />   
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="陀螺仪"
    android:id="@+id/edt4"
    />
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="光线"
    android:id="@+id/edt5"
    />
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="压力"
    android:id="@+id/edt6"
    />    
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="温度"
    android:id="@+id/edt7"
    />  
        <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="距离"
    android:id="@+id/edt8"
    />  
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="重力"
    android:id="@+id/edt9"
    />  
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="线性加速度"
    android:id="@+id/edt10"
    />  
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="旋转矢量"
    android:id="@+id/edt11"
    />    
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="defalut"
    android:id="@+id/edt12"
    />        
</LinearLayout>

main.java

复制代码
/*
 * 
 * IBMEyes.java
 * sample code for IBM Developerworks Article
 * Author: W. Frank Ableson
 * fableson@msiservices.com
 * 
 
*/

package com.msi.ibm.eyes;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.SensorListener;

public  class IBMEyes  extends Activity  implements SensorListener {
    
     final String tag = "IBMEyes";
    SensorManager sm =  null;
    
    TextView View1 =  null;
    TextView View2 =  null;
    TextView View3 =  null;
    TextView View4 =  null;
    TextView View5 =  null;
    TextView View6 =  null;
    TextView View7 =  null;
    TextView View8 =  null;
    TextView View9 =  null;
    TextView View10 =  null;
    TextView View11 =  null;
    TextView View12 =  null;
    
     /**  Called when the activity is first created.  */
    @Override
     public  void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        setContentView(R.layout.main);
        View1 = (TextView) findViewById(R.id.edt1);
        View2 = (TextView) findViewById(R.id.edt2);
        View3 = (TextView) findViewById(R.id.edt3);
        View4 = (TextView) findViewById(R.id.edt4);
        View5 = (TextView) findViewById(R.id.edt5);
        View6 = (TextView) findViewById(R.id.edt6);
        View7 = (TextView) findViewById(R.id.edt7);
        View8 = (TextView) findViewById(R.id.edt8);
        View9 = (TextView) findViewById(R.id.edt9);
        View10 = (TextView) findViewById(R.id.edt10);
        View11 = (TextView) findViewById(R.id.edt11);
        View12 = (TextView) findViewById(R.id.edt12);        
    }
    
     public  void onSensorChanged( int sensor,  float[] values) {
         synchronized ( this) {
            String str =  "X:" + values[0] + ",Y:" + values[1] + ",Z:" + values[2];
             switch (sensor){
             case Sensor.TYPE_ACCELEROMETER:
                View1.setText("加速度:" + str);
                 break;    
             case Sensor.TYPE_MAGNETIC_FIELD:
                View2.setText("磁场:" + str);
                 break
             case Sensor.TYPE_ORIENTATION:
                View3.setText("定位:" + str);
                 break
             case Sensor.TYPE_GYROSCOPE:
                View4.setText("陀螺仪:" + str);
                 break
             case Sensor.TYPE_LIGHT:
                View5.setText("光线:" + str);
                 break
             case Sensor.TYPE_PRESSURE:
                View6.setText("压力:" + str);
                 break
             case Sensor.TYPE_TEMPERATURE:
                View7.setText("温度:" + str);
                 break
             case Sensor.TYPE_PROXIMITY:
                View8.setText("距离:" + str);
                 break
             case Sensor.TYPE_GRAVITY:
                View9.setText("重力:" + str);
                 break
             case Sensor.TYPE_LINEAR_ACCELERATION:
                View10.setText("线性加速度:" + str);
                 break
             case Sensor.TYPE_ROTATION_VECTOR:
                View11.setText("旋转矢量:" + str);
                 break
             default:
                View12.setText("NORMAL:" + str);
                 break
            }   
        }
    }
    
     public  void onAccuracyChanged( int sensor,  int accuracy) {
        Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
    }

    @Override
     protected  void onResume() {
         super.onResume();
        sm.registerListener( this
                Sensor.TYPE_ACCELEROMETER |
                Sensor.TYPE_MAGNETIC_FIELD |
                Sensor.TYPE_ORIENTATION |
                Sensor.TYPE_GYROSCOPE |
                Sensor.TYPE_LIGHT |
                Sensor.TYPE_PRESSURE |
                Sensor.TYPE_TEMPERATURE |
                Sensor.TYPE_PROXIMITY |
                Sensor.TYPE_GRAVITY |
                Sensor.TYPE_LINEAR_ACCELERATION |
                Sensor.TYPE_ROTATION_VECTOR,
                SensorManager.SENSOR_DELAY_NORMAL);
    }
    
    @Override
     protected  void onStop() {
        sm.unregisterListener( this);
         super.onStop();
    }     
复制代码


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取传感器数据并展示的步骤如下: 1. 在 AndroidManifest.xml 文件中添加相应的权限: ``` <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BODY_SENSORS" /> ``` 2. 在 MainActivity.java 文件中创建 SensorManager 对象,并注册需要监听的传感器: ``` SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensorManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL); ``` 3. 实现 SensorEventListener 接口,重写 onSensorChanged() 方法以获取传感器数据: ``` @Override public void onSensorChanged(SensorEvent event) { float[] values = event.values; switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: // 处理加速度传感器数据 break; case Sensor.TYPE_GYROSCOPE: // 处理陀螺仪传感器数据 break; case Sensor.TYPE_MAGNETIC_FIELD: // 处理磁场传感器数据 break; // 其他传感器类型同理 } } ``` 4. 在 UI 界面上展示传感器数据。 ``` // 示例代码展示加速度传感器数据 TextView xTextView = findViewById(R.id.xTextView); TextView yTextView = findViewById(R.id.yTextView); TextView zTextView = findViewById(R.id.zTextView); @Override public void onSensorChanged(SensorEvent event) { float[] values = event.values; switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: float x = values[0]; float y = values[1]; float z = values[2]; xTextView.setText(String.format("%.2f", x)); yTextView.setText(String.format("%.2f", y)); zTextView.setText(String.format("%.2f", z)); break; // 其他传感器类型同理 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值