마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

bluetooth를 이용한 센서 값 실시간 보내기.. 도와주세요ㅠㅠ

0 추천

안녕하세요

제가 이번에 처음 안드로이드 공부를 시작하게 되어서 아무것도 모릅니다.. 말 그대로 짬찌...

제가 핸드폰에 내장된 센서와 아트메가를 블루투스 통신을 해서 실시간으로 값을 전송하려합니다.

여기저기 알아본 결과

Sample example에 bluetoothchat의 bluetoothchatservice부분에 이부분을 응용하라고 합니다.

public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[1024];
            int bytes;

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);

                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    // Start the service over to restart listening mode
                    BluetoothChatService.this.start();
                    break;
                }
            }
        }

 

제가 센서 값을 출력하는 부분은


public class SensorDump extends Activity {

	 SensorManager mSm;
	  TextView mTxtLight, mTxtProxi, mTxtPress, mTxtAccel, mTxtMagnetic, mTxtOrient;
	  int mLightCount, mProxiCount, mPressCount;
	  int mAccelCount, mMagneticCount, mOrientCount;
	  public void onCreate(Bundle savedInstanceState) {
	   super.onCreate(savedInstanceState);
	   setContentView(R.layout.sensordump);

	   mSm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

	   mTxtLight =(TextView)findViewById(R.id.light);
	   mTxtProxi =(TextView)findViewById(R.id.proxi);
	   mTxtPress =(TextView)findViewById(R.id.press);
	   mTxtOrient =(TextView)findViewById(R.id.orient);
	   mTxtAccel =(TextView)findViewById(R.id.accel);
	   mTxtMagnetic =(TextView)findViewById(R.id.magnetic);
	  }

	     protected void onResume() {
	        super.onResume();
            int delay = SensorManager.SENSOR_DELAY_UI;

	   mSm.registerListener(mSensorListener, mSm.getDefaultSensor(Sensor.TYPE_LIGHT), delay);
	   mSm.registerListener(mSensorListener, mSm.getDefaultSensor(Sensor.TYPE_PROXIMITY), delay);
	   mSm.registerListener(mSensorListener, mSm.getDefaultSensor(Sensor.TYPE_PRESSURE), delay);
	   mSm.registerListener(mSensorListener, mSm.getDefaultSensor(Sensor.TYPE_ORIENTATION), delay);
	   mSm.registerListener(mSensorListener, mSm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), delay);
	   mSm.registerListener(mSensorListener, mSm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), delay);
	   mSm.registerListener(mSensorListener, mSm.getDefaultSensor(Sensor.TYPE_TEMPERATURE), delay);
	   mSm.registerListener(mSensorListener, mSm.getDefaultSensor(Sensor.TYPE_GYROSCOPE), delay);
	    }

	     protected void onPause() {
	        super.onPause();
	        mSm.unregisterListener(mSensorListener);
	    }

	     SensorEventListener mSensorListener = new SensorEventListener() {
	   public void onAccuracyChanged(Sensor sensor, int accuracy) {
	    // 특별히 처리할 필요없음
	   }

	   public void onSensorChanged(SensorEvent event) {
	    // 신뢰성없는 값은 무시
	    if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
	     //return;
	    }

	    float[] v = event.values;
	    switch (event.sensor.getType()) {
	    case Sensor.TYPE_LIGHT: mTxtLight.setText("조도 = " + ++mLightCount + "회 : " + v[0]);
	     break;
	    case Sensor.TYPE_PROXIMITY: mTxtProxi.setText("근접 = " + ++mProxiCount + "회 : " + v[0]);
	     break;
	    case Sensor.TYPE_PRESSURE: mTxtPress.setText("압력 = " + ++mPressCount + "회 : " + v[0]);
	     break;
	    case Sensor.TYPE_ORIENTATION: mTxtOrient.setText("방향 = " + ++mOrientCount + "회 : \n  azimuth:" + v[0] + "\n  pitch:" + v[1] + "\n  roll:" + v[2]);
	     break;
	    case Sensor.TYPE_ACCELEROMETER: mTxtAccel.setText("가속 = " + ++mAccelCount + "회 : \n  X:" + v[0] + "\n  Y:" + v[1] + "\n  Z:" + v[2]);
	     break;
	    case Sensor.TYPE_MAGNETIC_FIELD: mTxtMagnetic.setText("자기 = " + ++mMagneticCount + "회 : \n  X:" + v[0] + "\n  Y:" + v[1] + "\n  Z:" + v[2]);
	     break;
	    }
	   }
	  };
	  
	}

이렇게 짰는데요.

블루투스 부분의 코딩을 어떻게 짜야할지도 모르겠고요

그리고 이 센서 코딩부분과 어떻게 합쳐야 할지도 모르겠습니다..

부탁드립니다.. 제발 도와주세요ㅠ

짬찌안드로이드 (120 포인트) 님이 2013년 8월 6일 질문
안녕하세요 혹시 해결하셨나요? 제가 지금 비슷한문제로 막혀있어서 도움좀 받고싶습니다

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...