我正在進行遠程控制。有一輛汽車,它有伺服+ esc,並且它們連接到與wifi屏蔽連接的arduino。 另一方面,我有Android平板電腦,它發送數據到arduino來控制一輛車。 沒有爲Android應用源代碼:Android監聽器中斷監聽器
package rccardriver.app;
import android.widget.Button;
import android.widget.SeekBar;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.IOException;
import android.os.AsyncTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import java.io.DataOutputStream;
public class MainActivity extends Activity {
private Socket socket=null;
private SeekBar steeringControl = null;
private SeekBar speedControl = null;
private Button btn=null;
DataOutputStream DOS = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(buttonConnectOnClickListener);
steeringControl =(SeekBar)findViewById(R.id.steering_bar);
speedControl =(SeekBar)findViewById(R.id.speed_bar);
steeringControl.setMax(75);
speedControl.setMax(180);
speedControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
try {
DOS.write(0x01);
DOS.write((byte)(speedControl.getProgress()+3));
} catch (IOException e) {
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
speedControl.setProgress(84);
}
});
steeringControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
try {
DOS.write(0x00);
DOS.write((byte)(steeringControl.getProgress()+3));
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
steeringControl.setProgress(35);
}
});
MyClientTask myClientTask = new MyClientTask("192.168.12.101", 8899);
myClientTask.execute();
}
@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);
}
View.OnClickListener buttonConnectOnClickListener =
new View.OnClickListener(){
@Override
public void onClick(View arg0) {
try {
DOS.write(0x02);
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
}};
public class MyClientTask extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
MyClientTask(String addr, int port){
dstAddress = addr;
dstPort = port;
}
@Override
protected Void doInBackground(Void... arg0) {
try {
socket = new Socket(dstAddress, dstPort);
DOS = new DataOutputStream(socket.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// response = "IOException: " + e.toString();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// textResponse.setText(response);
super.onPostExecute(result);
}
}
}
應用具有兩個seekbars和1個按鈕。 Seekbars控制轉向和速度。按鈕控制中斷。每個對象都有自己的偵聽器時。當我觸及這些對象時,監聽器被調用,它發送兩個字節:第一個字節代表命令的nr,第二個代表值。 問題是,當我連續觸摸對象時,最近調用的偵聽器被下一個被調用的偵聽器打斷。這樣arduino會收到隨機字節。 有人有一個想法如何解決這個問題? 如何讓聽衆完全完成?
嘗試在同步方法中包裝DOS.write。 – kujeensiti
可否請您指出我必須粘貼「同步」的位置。我不明白 – Qeeet