app连接wifi模块,通过wifi模块串口读取数据失败
来源:互联网 发布:淘宝买的发票是真的吗 编辑:程序博客网 时间:2024/04/30 15:09
写了一个数据接收测试的程序,如果接收到数据则实现一个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),所以应该是程序的问题,但不知道问题出在哪?
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),所以应该是程序的问题,但不知道问题出在哪?
0 0
- app连接wifi模块,通过wifi模块串口读取数据失败
- 51单片机连接ESP8266串口WiFi模块
- ESP8266串口WiFi模块
- WIFI模块连接手机
- WIFI 模块 串口 调试 助手
- 有人串口转wifi模块 httpd client通信示例-用户使用网页通过服务器收发串口数据源码 小黄人软件
- WIFI模块
- wifi模块
- WIFI模块
- WIFI模块
- WiFi模块
- 串口wifi- 无线局域网wlan模块
- 工业级嵌入式串口转wifi模块
- ESP8266串口wifi模块 调试方法
- Arduino Uno 连接JY-61陀螺仪模块 串口读取数据
- WIFI学习之一:WIFI模块
- 关于WiFi模块(连接流程介绍)
- APP发数据到ESP8266这个WiFi模块数据发生错乱
- 在线等......xmpp
- Spring学习笔记-C5-SpringMVC起步
- asp 页面控件修改
- android 动画的问题
- Exoplayer 播放M3U8格式视频失败
- app连接wifi模块,通过wifi模块串口读取数据失败
- 未安装APK ,动态部署,加载资源布局XML的问题
- Android显示文章问题
- 137. Single Number II
- 想往一个apk里面添加功能,想知道能不能实现
- 安卓自定义layout实现下拉的问题
- IPhone通过USB通信
- 安卓 微信支付code=-1 问题
- 初学ios遇到麻烦了,烦请大神讲解