我已经开发了一个android
通过 wifi 遥控汽车的应用程序。为此我使用了esp8266 nodemcu
wifi 模块。在我的应用程序中。有前进、后退、右、左四个按钮。我可以通过 wifi 连接这个模块并为 ex 做一些基本的操作。前进,后退,右转,左转。以下是执行此操作的代码:
@Override
public boolean onTouch(View v, MotionEvent event) {
String action;
if (v.getId() == moveForward.getId())
action = "/forward/";
else if (v.getId() == moveReverse.getId())
action = "/reverse/";
else if (v.getId() == moveRigth.getId())
action = "/right/";
else
action = "/left/";
String serverAddress = ipAddress.getText().toString() + ":" + "80" + action;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
HttpRequestTask requestTask = new HttpRequestTask(serverAddress);
requestTask.execute("1");
} else if (event.getAction() == MotionEvent.ACTION_UP) {
HttpRequestTask requestTask = new HttpRequestTask(serverAddress);
requestTask.execute("0");
}
return false;
}
private class HttpRequestTask extends AsyncTask<String, Void, String> {
private String serverAdress;
private String serverResponse = "";
public HttpRequestTask(String serverAdress) {
this.serverAdress = serverAdress;
}
@Override
protected String doInBackground(String... params) {
String val = params[0];
String url = "http://" + serverAdress + val;
Log.e("url", url);
try {
HttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet();
getRequest.setURI(new URI(url));
HttpResponse response = client.execute(getRequest);
InputStream inputStream = null;
inputStream = response.getEntity().getContent();
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(inputStream));
serverResponse = bufferedReader.readLine();
inputStream.close();
} catch (URISyntaxException e) {
e.printStackTrace();
serverResponse = e.getMessage();
} catch (ClientProtocolException e) {
e.printStackTrace();
serverResponse = e.getMessage();
} catch (IOException e) {
e.printStackTrace();
serverResponse = e.getMessage();
}
return serverResponse;
}
当用户向前按下时,也可以按下右键或左键,小车同时前进右转或倒车左转。但是我无法在代码中同时处理两个按钮按下事件。你能告诉我一个方法吗?