안녕하세요
제가 이번에 처음 안드로이드 공부를 시작하게 되어서 아무것도 모릅니다.. 말 그대로 짬찌...
제가 핸드폰에 내장된 센서와 아트메가를 블루투스 통신을 해서 실시간으로 값을 전송하려합니다.
여기저기 알아본 결과
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;
}
}
};
}
이렇게 짰는데요.
블루투스 부분의 코딩을 어떻게 짜야할지도 모르겠고요
그리고 이 센서 코딩부분과 어떻게 합쳐야 할지도 모르겠습니다..
부탁드립니다.. 제발 도와주세요ㅠ