app连接wifi模块,通过wifi模块串口读取数据失败

xjx1993 发布于 2016/08/09 16:24
阅读 526
收藏 1
写了一个数据接收测试的程序,如果接收到数据则实现一个toast,但运行没有实现。
WIFI板块参考资料:



arduino模块向WIFI模块不断输入数据:

app源码:


1.layout:activity_main.xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="数据接收测试" />


</RelativeLayout>


2.MainActivity.java:


package ceshi;




import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.URL;


import com.example.ceshi.R;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.widget.Toast;


public class MainActivity extends Activity {


 
   URL videoUrl;
   private boolean isConnecting = false;
   private Thread mThreadClient = null;
private Socket mSocketClient = null;
static BufferedReader mBufferedReaderClient = null;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mThreadClient = new Thread(mRunnable);
mThreadClient.start();
        
    }






private Runnable mRunnable = new Runnable() //开线程连接服务器,获取输入、输出流
{
public void run()
{
String msgText ="192.168.1.1:2001";


int start = msgText.indexOf(":"); // 找到端口号的标示符,控制端口号为2001


String sIP = msgText.substring(0, start); //得到链接的ip地址
String sPort = msgText.substring(start+1); //找到端口号2001
int port = Integer.parseInt(sPort); //String型转换为Int型

Log.d("gjz", "IP:"+ sIP + ":" + port);

try 
{
//连接服务器
mSocketClient = new Socket(sIP, port); //portnum
//取得输入、输出流
mBufferedReaderClient = new BufferedReader(new InputStreamReader(mSocketClient.getInputStream()));


Message msg = new Message();
            msg.what = 1;
mHandler.sendMessage(msg);
//break;
}
catch (Exception e) 
{
Message msg = new Message();
            msg.what = 1;
mHandler.sendMessage(msg);
return;
}


char[] buffer = new char[256];

while (isConnecting)
{
try
{
//if ( (recvMessageClient = mBufferedReaderClient.readLine()) != null )
if((mBufferedReaderClient.read(buffer))>0)
{
Toast.makeText(getApplicationContext(), "数据接收成功", Toast.LENGTH_SHORT).show();
Message msg = new Message();
               msg.what = 1;
mHandler.sendMessage(msg);
}
}
catch (Exception e)
{
Message msg = new Message();
                msg.what = 1;
mHandler.sendMessage(msg);

}
}



}
};


Handler mHandler = new Handler()
{
public void handleMessage(Message msg)
 {
 super.handleMessage(msg);


 }
 };

 public void onDestroy()
{
super.onDestroy();
if (isConnecting) 
{
isConnecting = false;
try
{
if(mSocketClient!=null)
{
mSocketClient.close();
mSocketClient = null;


}

catch (IOException e) 
{
e.printStackTrace();
}
mThreadClient.interrupt();
}


}
}
不知道这么测试有没有问题?正常来说,arduino是能向WIFI模块输入数据(这里我用arduino设置每5S向串口发送一个数字:1),所以应该是程序的问题,但不知道问题出在哪?

加载中
返回顶部
顶部