我想用安卓手机驾驶遥控车。我用蓝牙耳机作为接收器。我的手机发送 DTMF 音,当耳机收到它时,微控制器会对其进行分析以打开/关闭汽车电机。
我使用加速度计来检测手机的运动......(前/左/右/后)并且在每次移动时它都会播放特定的 DTMF 音。
一切正常,但有一个问题:
随着传感器值不断变化,音调播放很多!
问题:我怎样才能让音调只播放一次??(如代码中所示)
这是代码:
X = (TextView)findViewById(R.id.x);
Y = (TextView)findViewById(R.id.y);
Z = (TextView)findViewById(R.id.z);
toneGenerator= new ToneGenerator(AudioManager.STREAM_DTMF,ToneGenerator.MAX_VOLUME);
sel = new SensorEventListener(){
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
float x = values[0];
float xx = Math.round(x) ;
float y = values[1];
float yy = Math.round(y) ;
float z = values[2];
float zz = Math.round(z) ;
X.setText("x:" + xx);
Y.setText("y:" + yy);
Z.setText("z:" + zz);
if (xx>-1 & xx<1 & zz>3 & zz<8 ) {
state.setText ("normal");
toneGenerator.startTone(ToneGenerator.TONE_DTMF_5);
new CountDownTimer(50, 500) {
public void onTick(long millisUntilFinished) { }
public void onFinish() {
toneGenerator.stopTone();
}}.start();
}
if (xx>-1 & xx<1 & zz>8 ) {
state.setText ("front");
toneGenerator.startTone(ToneGenerator.TONE_DTMF_2);
new CountDownTimer(50, 500) {
public void onTick(long millisUntilFinished) { }
public void onFinish() {
toneGenerator.stopTone();
}}.start();
有什么建议么 ???