|
请教各位大侠,我最近在做Android socket通信方面,是想双向传输数据,代码如下:
public class MessageActivity extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "MessageActivity";
private Socket socket ;
static PrintWriter mPrintWriter ;
static BufferedReader br ;
private Thread mThread=null;
private String msg="";
public TextView textview2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button2 = (Button) findViewById(R.id.button2);//设置按钮,按下后给服务器发送数据
textview2=(TextView)findViewById(R.id.textview2);//显示从服务器传来的数据
try
{
socket=new Socket("10.10.100.254",8899);
br =new BufferedReader(new InputStreamReader(socket.getInputStream())); //获得输出流
mPrintWriter=new PrintWriter(socket.getOutputStream(),true); //获得输入流
}
catch(Exception e)
{
Log.e(TAG,e.toString());
}
button2.setOnTouchListener(new Button.OnTouchListener() //给button2 设置监听
{
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
mPrintWriter.print("f");
mPrintWriter.print("\r");
mPrintWriter.print("\n");//前进
mPrintWriter.flush();
break;
case MotionEvent.ACTION_UP:
mPrintWriter.print("0");
mPrintWriter.print("\r");
mPrintWriter.print("\n");
mPrintWriter.flush();
}
return false;
}
});
mThread= new Thread(mRunnable); //创建一个新的线程
mThread.start(); //开启新的线程
}
private Runnable mRunnable = new Runnable() //通过Runnable接口创建线程,来处理从服务器传来的数据
{
public void run()
{
while (true)
{
try
{
if((msg=br.readLine())!=null)
{
mHandler.sendMessage(mHandler.obtainMessage());
}
}
catch(Exception e)
{
Log.e(TAG,e.toString());
}
}
}
};
Handler mHandler=new Handler() //处理传来的数据
{
public void handleMessage( Message Msg)
{
super.handleMessage(Msg);
try
{
textview2.setText(msg);
}
catch(Exception e)
{
Log.e(TAG,e.toString());
}
}
};
}
然后我通过电脑串口调试助手进行试验,将手机连上wifi芯片的wifi,将wifi芯片与电脑串口相连,按下按钮,串口能够收到数据,但是从串口调试助手发送的数据就是显示不到textview2里面。请问这到底是怎么回事啊??是这个程序有什么问题,还是不能用串口调试助手传输数据??
万分感谢
|
|