iT邦幫忙

DAY 5
0

x86 android 設備與外部硬體溝通研究系列 第 5

x86 android 設備與外部硬體溝通研究 - 更多的感應器 (5/30)

  • 分享至 

  • xImage
  •  

昨天介紹加速度計的使用,如果當我們需要超過一種感應器時,該怎們半呢?

我們來看兩段從昨天範例修改而來的兩段 code :

package com.example.test;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener; 
import android.widget.TextView; 
import android.widget.Button; 
import android.os.CountDownTimer; 
import android.view.View; 
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.Sensor;



public class MainActivity extends ActionBarActivity{

	private boolean timerHasStarted = false ;
	private Button startB;
	private ThisIsATimer CountDownTimer;
	private TextView TimeView;
	private TextView ElapsedView;
	private TextView AccelX;
	private TextView AccelY;
	private TextView AccelZ;
	private TextView OrienX;
	private TextView OrienY;
	private TextView OrienZ;
	private long timeElapsed;
	private final long startTime = 50*1000;
	private final long interval = 1*1000; // 1 sec. per time interval
	private SensorActivity MySensor;

	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startB = (Button) this.findViewById(R.id.button1);
        startB.setOnClickListener(new Button_Listener(this));
        TimeView = (TextView) this.findViewById(R.id.timer);
        AccelX = (TextView) this.findViewById(R.id.AccelX);
        AccelY = (TextView) this.findViewById(R.id.AccelY);
        AccelZ = (TextView) this.findViewById(R.id.AccelZ);
        OrienX = (TextView) this.findViewById(R.id.OrienX);
        OrienY = (TextView) this.findViewById(R.id.OrienY);
        OrienZ = (TextView) this.findViewById(R.id.OrienZ);
        ElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
        CountDownTimer = new ThisIsATimer(startTime, interval);

        MySensor = new SensorActivity(); //初始化 sensor manager 

        MySensor.onResume(); //跟昨天不同的是,今天我們讓他執行時即啟動感應器監聽

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    
    public class ThisIsATimer extends CountDownTimer{

		public ThisIsATimer(long startTime, long interval) {
			super(startTime, interval);
		}
		@Override
		public void onFinish(){
			TimeView.setText("Time's Up");
			ElapsedView.setText("Time Elapsed:" + String.valueOf(startTime));
		}
		@Override
		public void onTick(long millisUntillFinished){
			TimeView.setText("Time remain : "+ millisUntillFinished);
			timeElapsed = startTime - millisUntillFinished;
			ElapsedView.setText("Time Elapsed : " + String.valueOf(timeElapsed)); 
		}
    	
    }
    
    class Button_Listener implements OnClickListener {

    	  public Button_Listener(MainActivity activity) {

    	  }
    	  @Override
    	    public void onClick(View view) {
    	        if(!timerHasStarted){
    	        	CountDownTimer.start();
    	        	MySensor.onResume();

    	        	timerHasStarted = true;
    	        	startB.setText("RESET ME!");
    	        }else{
    	        	CountDownTimer.cancel();
    	        	MySensor.onPause();

    	        	timerHasStarted = false;
    	        	startB.setText("Start Counting... 50 Sec.");	
    	        }
    	    }
    	}
    public class SensorActivity implements SensorEventListener {
        private SensorManager mSensorManager;


		public SensorActivity() {
            mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
            //mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //我們捨棄昨天的作法,不使用getDefaultSensor 回傳 sensor object 的方式,這種方式我目前還找不到如何一次預設註冊 2 種以上的 sensor , 只好分次註冊 sensor 
            //mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        }


		@SuppressWarnings("deprecation")
		protected void onResume() {
            // 不使用 Sensor.TYPE_ALL 這種一次註冊監聽「所有」感應器的方式,只選我們需要的,可以節省電力使用 
        	mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) , SensorManager.SENSOR_DELAY_NORMAL); //註冊監聽加速度計,更新頻率為 NORMAL
        	mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION) , SensorManager.SENSOR_DELAY_NORMAL); //註冊監聽第二項感應器,這邊我們選用方位計,更新頻率同上
        }

        protected void onPause() {
        	mSensorManager.unregisterListener(this);
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }


		@SuppressWarnings("deprecation")
		public void onSensorChanged(SensorEvent event) {
			switch(event.sensor.getType()){
			case Sensor.TYPE_ACCELEROMETER : //判斷是加速度計
        		float X 	= event.values[0];
        		float Y 	= event.values[1];
        		float Z 	= event.values[2];
        		AccelX.setText("X : " + String.valueOf(X));
        		AccelY.setText("Y : " + String.valueOf(Y));
        		AccelZ.setText("Z : " + String.valueOf(Z));
				break;
			case Sensor.TYPE_ORIENTATION :    //判斷是方位計
        		float O_Z 	= event.values[0];
        		float O_X 	= event.values[1];
        		float O_Y 	= event.values[2];
        		OrienX.setText("X : " + String.valueOf(O_X));
        		OrienY.setText("Y : " + String.valueOf(O_Y));
        		OrienZ.setText("Z : " + String.valueOf(O_Z));
				break;
			}
        }
    }
}

UI 部分就是多了幾個 TextView , 讓感應器的資訊可以順利顯示於螢幕中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test.MainActivity" >

    <TextView
        android:id="@+id/timer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/Time" /> 
        <!-- Call Resource String, 
        	需要到./res/values/strings.xml 
        	建立 Name => Value 對照
        	-->

     <TextView
         android:id="@+id/timeElapsed"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/timer"
         android:layout_below="@+id/timer"
         android:text="@string/Time_elasped" />

	<TextView
         android:id="@+id/Accel"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/button1"
         android:layout_below="@+id/button1"
         android:text="加速度計:" />
          <TextView
         android:id="@+id/AccelX"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/button1"
         android:layout_below="@+id/Accel"
         android:text="X" />
          <TextView
         android:id="@+id/AccelY"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/AccelX"
         android:layout_below="@+id/AccelX"
         android:text="Y" />
          <TextView
         android:id="@+id/AccelZ"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/AccelY"
         android:layout_below="@+id/AccelY"
         android:text="Z" />
         
          <TextView
         android:id="@+id/Orien"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/button1"
         android:layout_below="@+id/AccelZ"
         android:text="方位計:" />
         <TextView
         android:id="@+id/OrienX"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/button1"
         android:layout_below="@+id/Orien"
         android:text="X" />
          <TextView
         android:id="@+id/OrienY"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/AccelX"
         android:layout_below="@+id/OrienX"
         android:text="Y" />
          <TextView
         android:id="@+id/OrienZ"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/AccelY"
         android:layout_below="@+id/OrienY"
         android:text="Z" />
          
     <Button
         android:id="@+id/button1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/timeElapsed"
         android:layout_below="@+id/timeElapsed"
         android:text="Start Counting... 50 Sec." />
     <!-- Hardcode string 方式 -->
          

</RelativeLayout>

我們明天見


上一篇
x86 android 設備與外部硬體溝通研究 - 加速度 (4/30)
下一篇
x86 android 設備與外部硬體溝通研究 - Android library project (6/30)
系列文
x86 android 設備與外部硬體溝通研究30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言