在我的Android客户端应用程序中,我试图通过套接字将虚拟操纵杆坐标从客户端(Android)发送到服务器(Arduino).Tray在主活动中编写包含套接字's'的线程't' ,以将x,y坐标发送到arduino服务器。 当我移动操纵杆时,JoystickView类会给我“ x”和“ y”。 这将在用户单击“连接”按钮后完成, 'x'是从0到255,也是'y',但是为了能够知道arduino端女巫的值是'x'并且女巫是'y',我在y中添加了255,所以y将是255到510。 / p> 虚拟操纵杆为我提供存储在公共变量“ xxx”和“ yyy”中的“ x”,“ y”坐标。 将连接按钮与线程“ t,已创建的I套接字”链接在一起,以将“ x”,“ y”发送到服务器。
**我的问题:我的代码可以与Arduino一起使用吗,我可以在Arduino Side中获得x,y吗?我也不能将线程“ t”放在“ public void onClick(View v)”函数中。这是第二个问题:( **
package com.ravensoft.daniel.joysticktest;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.DataOutputStream;
public class MainActivity extends AppCompatActivity implements
JoystickView.JoystickListener{
TextView x;
TextView y;
public float xxx=0;//bublic var will Contain x value from virtual
joystick
public float yyy=0;//bublic var will Contain x value from virtual
joystick
EditText txt_IP;//arduino ip
EditText txt_PORT;//arduino port
private volatile Socket socket = null;
private boolean connectionOk = false;
private Button buttonDisconnect; //button for disconnect connection
private Button buttonConnect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
JoystickView joystick = new JoystickView(this);
setContentView(R.layout.activity_main);
buttonConnect = findViewById(R.id.buttonConnect);
buttonDisconnect = findViewById(R.id.buttonDisconnect);
JoystickView joystic =findViewById(R.id.joystick);
txt_IP=(EditText)findViewById(R.id.editText_IP);
txt_PORT=(EditText)findViewById(R.id.editText_PORT);
buttonConnect.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
}
});
buttonDisconnect.setEnabled(false);
buttonConnect.setEnabled(true);
}
Thread t=new Thread(new Runnable() {
@Override
public void run() {
try {
Socket s = new Socket(txt_IP.getText().toString(),Integer.parseInt(txt_PORT.getText().toString() ));
s.connect(new InetSocketAddress(txt_IP.getText().toString(), Integer.parseInt(txt_PORT.getText().toString() )), 50);
if(s == null)System.out.println("SOCKET NULL");
DataOutputStream DataOut = new DataOutputStream(s.getOutputStream());
String message= Float.toString(xxx+256);
byte[] msgBuffer=message.getBytes();
String message2= Float.toString(yyy);
byte[] msgBuffer2=message2.getBytes();
try {
DataOut.write(msgBuffer);
DataOut.write(msgBuffer2);
}catch (IOException e){
}
DataOut.flush();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
@Override
public void onJoystickMoved( float xPercent, float yPercent) {
x = (TextView) findViewById(R.id.x_value);
x.setText(Float.toString(xPercent));
y = (TextView) findViewById(R.id.y);
y.setText(Float.toString(-yPercent));
xxx=xPercent;
yyy=yPercent;
Log.d("Right Joystick", "X percent: " + xPercent + " Y percent: " + yPercent);
}}