我正在尝试从我的Android手机接收加速数据到我的电脑。
我已尝试使用此JAVA代码访问和发送我的android:
中的数据 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener{
String mClientMsg = "";
Thread myCommsThread = null;
public static final String TAG = "SocketServer";
private CommsThread commsThread = null;
TextView tv;
PrintWriter out;
private float ax, ay, az;
private long timenow = 0, timeprev = 0, timestamp =0 ;
private SensorManager sm;
private Sensor sensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sm.getSensorList(Sensor.TYPE_LINEAR_ACCELERATION).get(0);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
this.commsThread = new CommsThread();
this.myCommsThread = new Thread(this.commsThread);
this.myCommsThread.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
sm.unregisterListener(this);
if (commsThread != null) {
commsThread.stopComms();
}
}
Handler myHandler = new Handler(){
public void handleMessage(Message msg){
TextView status = (TextView) findViewById(R.id.textView3);
status.setText("Status: Streaming Now!");
}
};
class CommsThread implements Runnable {
private volatile boolean stopFlag = false;
private ServerSocket ss = null;
private static final int SERVERPORT = 6000;
public void run() {
Socket s = null;
try {
ss = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
try {
s = ss.accept();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
myHandler.sendEmptyMessage(0);
while(!stopFlag){
try {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())), true);
out.printf("*#%3.2f#%3.2f#%3.2f#%2d#*\n",ax,ay,az,(int)timestamp );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void stopComms() {
// TODO Auto-generated method stub
this.stopFlag = true;
if(ss != null){
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
ax = event.values[0];
ay = event.values[1];
az = event.values[2];
timenow = event.timestamp;
timestamp = (timenow - timeprev)/1000000;
refreshDisplay();
}
private void refreshDisplay() {
// TODO Auto-generated method stub
String output = String.format("time: %d -- x:%03.2f | Y:%03.2f | Z:%03.2f", timestamp, ax,ay,az);
timeprev = timenow;
tv.setText(output);
}
}
这个python脚本作为我PC上的服务器:
import socket
from time import *
import sys
serv=socket.socket()
HOST=''
PORT = 7000
#HOST="78.91.80.123" //here I use my IP
#ADDR = (HOST,PORT)
BUFSIZE = 4096
#bind our socket to the address
try:
serv.bind((HOST, PORT))
serv.listen(5)
conn,addr = serv.accept()
except KeyboardInterrupt:
print "Keyboard Interrupt"
serv.close()
exit(1)
try:
for i in range(0,3100):
data=conn.recv(4096)
sys.stdout.flush()
chunk=data.split()
sys.stdout.write("0:%s\n" % chunk[-1]) # writes the last element in the list
sleep(0.03)
conn.close()
sleep(10)
except KeyboardInterrupt:
conn.close()
print "bye!"
except IndexError:
conn.close()
print "indexError"
我已经测试了python脚本并且它可以工作,但是当我打开应用程序时,没有任何内容被发送到PC。我是一种新的协议,所以我仍然想弄清楚会发生什么。
答案 0 :(得分:1)
首先,您使用两个不同的端口号。在Python脚本中,您有PORT = 7000
,在Java代码中有private static final int SERVERPORT = 6000;
。这些必须匹配才能创建连接,并确保您没有将此端口用于计算机上的任何其他内容。
如果您的Python脚本正在运行服务器,那么您不应该在Java代码中使用ServerSocket
。 ServerSocket
等待来自客户端的传入连接。要建立与服务器的连接,您需要使用:
Socket s = new Socket("78.91.80.123", SERVERPORT);
从那里,您可以通过查看相应Java Documentation.
中的一些示例来写入套接字