레이블이 SensorManager인 게시물을 표시합니다. 모든 게시물 표시
레이블이 SensorManager인 게시물을 표시합니다. 모든 게시물 표시

2010년 8월 17일 화요일

안드로이드 센서 시뮬레이터 (Sensor Simulator in Android)

안드로이드에서 센서를 사용하는 어플리케이션을 개발할 때 에뮬레이터는 센서를 지원하지 않기 때문에 동작을 테스트 할 방법이 없다. 그래서 인터넷을 뒤져 본 결과 에뮬레이터에서 센서 입력을 시뮬레이션 해 줄수있는 툴을 발견했다. OpenIntents가 개발한 SensorSimulator로 별도 프로그램에서 에뮬레이터에서 실행되고 있는 어플리케이션에 센서 값을 보내줄 수 있다.


먼저 SensorSimulator 를 다운받는다. 다운받은 압축을 풀어보면 다음과 같은 파일이 들어있다.


이 중 bin 디렉토리를 보면 다음과 같은 파일이 들어있다.


이 파일중 SensorSimulatorSettings.apk 파일은 에뮬레이터에 인스톨 해 줘야 하고 sensorsimulator.jar는 pc에서 실행할 바이너리 파일이다.


먼저 커맨드 라인에서 adb를 사용해서 SensorSimulatorSettings.apk를 에뮬레이터에 설치해준다.


설치가 끝났으면 PC에서 sensorsimulator.jar를 실행시킨다. 프로그램이 실행되면 위와 같은 화면이 나온다.


에뮬레이터를 실행해보면 SensorSimulatorSettings가 설치된 걸 볼 수 있다. 에뮬레이터에 설치된 앱을 실행한다.


sensorsimulator 화면을 보고 적절한 IP주소와 포트번호를 에뮬레이터의 앱에 넣어준다.


에뮬레이터에서 Testing 탭을 선택한 다음 Connect 버튼을 누른다.


IP 주소와 포트번호가 제대로 설정되었다면 sensorsimulator에 연결되었다는 메시지가 나오고 에뮬레이터에도 위와 같은 화면이 나온다.


초기 연결되었을 때는 모든 센서가 disable상태이기 때문에 에뮬레이터에서 테스트하려는 센서를 선택해서 활성화 시켜주면 된다. 그리고 sensorsimulator의 휴대폰 그림을 클릭하고 움직여보면 그에 따른 센서값의 변화가 에뮬레이터로 전달되어 센서 값이 업데이트 되는걸 볼 수 있다.

-----------------------------------------------------------------------

자신의 앱을 센서 시뮬레이터를 사용해서 테스트 하는 법

1. SensorSimulatorSettings를 실행해서 위와 같이 필요한 설정을 해 준다.

2. 프로젝트 루트에 lib 디렉토리를 만든다.


3. sensorsimulator-xxx/lib/sensorsimulator-lib.jar 파일을 lib 디렉토리에 넣어 준다.


4. 프로젝트의 Properties 메뉴를 선택한다.


5. Java Build Path -> Libraries -> Add JARs...를 선택한다.


6. 방금전에 넣어 준 sensorsimulator-lib.jar를 선택해 추가해준다.


7. sensorsimulator-lib.jar가 Java Build Path의 Libraries에 추가된 걸 확인할 수 있다.


8. Java 소스코드에서 SensorManager를 SensorManagerSimulator로 변경해줘야 한다. 아래와 같이 SensorManager 인스턴스를 가져오는 방법을 변경하고 시뮬레이터에 연결하도록 변경해 주면 된다.


9. 센서 시뮬레이터가 앱에 시뮬레이션 되는 센서 데이터는 소켓을 통해 통신하기 때문에 INTERNET permission이 필요하다. AndroidManifest.xml에 아래와 같이 추가해 주면 된다.


10. PC에 sensorsimulator를 실행한 다음 에뮬레이터에서 앱을 실행하면 앱이 시뮬레이터에 자동으로 연결된다.


11. 시뮬레이터의 휴대폰 그림을 움직이면 에뮬레이터 화면에서 센서값이 변화되는걸 볼 수 있다.


* 센서 인터페이스가 API level 3(Android SDK 1.5)부터 SensorListener에서 SensorEventListener로 바뀌면서 센서 관련 API가 변경되었는데 아쉽게도 SensorSimulator는 SensorListener를 사용하는 API만 지원한다. 그래서 이전 포스트의 코드를 바로 사용할 수는 없고 이전 API에 맞게 조금 변형해 줘야 한다. res/layout/main.xml은 동일한 코드를 사용하면 된다.

/src/.../SensorSimTest.java

...
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;
...
public class SensorSimTest extends Activity {
    SensorManagerSimulator sm;
    SensorListener sl;
    TextView ax, ay, az;
    TextView ox, oy, oz;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        sm = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);
        sm.connectSimulator();

        sl = new SL();
       
        ax = (TextView)findViewById(R.id.acc_x);
        ay = (TextView)findViewById(R.id.acc_y);
        az = (TextView)findViewById(R.id.acc_z);
        ox = (TextView)findViewById(R.id.ori_x);
        oy = (TextView)findViewById(R.id.ori_y);
        oz = (TextView)findViewById(R.id.ori_z);
    }
   
    @Override
    public void onResume() {
        super.onResume();
       
        sm.registerListener(sl, SensorManager.SENSOR_ACCELEROMETER | SensorManager.SENSOR_ORIENTATION,
                SensorManager.SENSOR_DELAY_NORMAL);
    }
   
    @Override
    public void onPause() {
        super.onPause();
       
        sm.unregisterListener(sl);
    }
   
    private class SL implements SensorListener {
        public void onSensorChanged(int sensor, float[] values) {
            if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
                ax.setText(Float.toString(values[0]));
                ay.setText(Float.toString(values[1]));
                az.setText(Float.toString(values[2]));
            } else if (sensor == SensorManager.SENSOR_ORIENTATION) {
                ox.setText(Float.toString(values[0]));
                oy.setText(Float.toString(values[1]));
                oz.setText(Float.toString(values[2]));
            }
        }
       
        public void onAccuracyChanged(int sensor, int accuracy) {
        }
    }

}





2010년 8월 16일 월요일

안드로이드 센서 사용하기(Using Sensors in Android)



안드로이드 SDK는 하드웨어에 있는 다양한 센서를 억세스 할 수 있게 해 준다. 현재 기본적으로 지원하는 센서는 다음과 같다.



안드로이드 어플리케이션에서 센서값을 억세스 하려면 SensorManager와 SensorEventListener를 사용한다.

SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);
sm.registerListener(gSensorEventListener, gSensorType, gDelay);

getSystemService(SENSOR_SERVICE)를 사용해 SensorManager 인스턴스를 가져온 다음 SensorEventListener 오브젝트를 등록해주면 센서값을 억세스 할 수 있다.

private void AccSensorEventListener implements SensorEventListener {
  public void onSensorChanged() {
    ...
  }
  public void onAccuracyChanged() {
    ...
  }
}

SensorEventListener 오브젝트에는 onSensorChanged()와 onAccuracyChanged() 메소드를 구현해 줘야 한다. 센서 값이 바뀔때마다 리스너 오브젝트의 onSensorChanged() 메소드가 호출된다.





/src/.../SensorTest.java

...
public class SensorTest extends Activity {
    SensorManager sm;
    SensorEventListener accL;
    SensorEventListener oriL;   
    Sensor oriSensor;
    Sensor accSensor;
    TextView ax, ay, az;
    TextView ox, oy, oz;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
      sm = (SensorManager)getSystemService(SENSOR_SERVICE);    // SensorManager 인스턴스를 가져옴           
        oriSensor = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);    // 방향 센서
        accSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);    // 가속도 센서
        oriL = new oriListener();        // 방향 센서 리스너 인스턴스
        accL = new accListener();       // 가속도 센서 리스너 인스턴스
        ax = (TextView)findViewById(R.id.acc_x);
        ay = (TextView)findViewById(R.id.acc_y);
        az = (TextView)findViewById(R.id.acc_z);
        ox = (TextView)findViewById(R.id.ori_x);
        oy = (TextView)findViewById(R.id.ori_y);
        oz = (TextView)findViewById(R.id.ori_z);
    }
   
    @Override
    public void onResume() {
        super.onResume();
               
        sm.registerListener(accL, accSensor, SensorManager.SENSOR_DELAY_NORMAL);    // 가속도 센서 리스너 오브젝트를 등록
        sm.registerListener(oriL, oriSensor, SensorManager.SENSOR_DELAY_NORMAL);    // 방향 센서 리스너 오브젝트를 등록
    }
   
    @Override
    public void onPause() {
        super.onPause();
       
       sm.unregisterListener(oriL);    // unregister acceleration listener
       sm.unregisterListener(accL);    // unregister orientation listener
    }
   
   
    private class accListener implements SensorEventListener {
        public void onSensorChanged(SensorEvent event) {  // 가속도 센서 값이 바뀔때마다 호출됨
            ax.setText(Float.toString(event.values[0]));
            ay.setText(Float.toString(event.values[1]));
            az.setText(Float.toString(event.values[2]));
            Log.i("SENSOR", "Acceleration changed.");
            Log.i("SENSOR", "  Acceleration X: " + event.values[0]
                          + ", Acceleration Y: " + event.values[1]
                           + ", Acceleration Z: " + event.values[2]);
        }
       
        public void onAccuracyChanged(Sensor sensor, int accuracy) {   
        }       
    }
   
    private class oriListener implements SensorEventListener {
        public void onSensorChanged(SensorEvent event) {  // 방향 센서 값이 바뀔때마다 호출됨
            ox.setText(Float.toString(event.values[0]));
            oy.setText(Float.toString(event.values[1]));
            oz.setText(Float.toString(event.values[2]));
            Log.i("SENSOR", "Orientation changed.");
            Log.i("SENSOR", "  Orientation X: " + event.values[0]
                          + ", Orientation Y: " + event.values[1]
                          + ", Orientation Z: " + event.values[2]);
        }
       
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
           
        }
    }
}




/res/layout/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:text="Sensor Values" android:gravity="center" android:textSize="18sp"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TextView android:text="Acceleration(X-axis): "
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView android:id="@+id/acc_x"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />   
    </LinearLayout>
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TextView android:text="Acceleration(Y-axis): "
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView android:id="@+id/acc_y"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />   
    </LinearLayout>
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TextView android:text="Acceleration(Z-axis): "
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView android:id="@+id/acc_z"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />   
    </LinearLayout>
    <View android:layout_width="fill_parent" android:layout_height="4dp" android:background="#ff0000" />
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TextView android:text="Orientation(X-axis): "
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView android:id="@+id/ori_x"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />   
    </LinearLayout>
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TextView android:text="Orientation(Y-axis): "
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView android:id="@+id/ori_y"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />   
    </LinearLayout>
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TextView android:text="Orientation(Z-axis): "
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView android:id="@+id/ori_z"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />   
    </LinearLayout>
   
</LinearLayout>