安卓Socket连接实现连接实现发送接收数据,openwrt wifi转串口连接单片机实现控制
安卓Socket连接实现连接实现发送接收数据,openwrt wifi转串口连接单片机实现控制
socket 连接采用流的方式进行发送接收数据,采用thread线程的方式。
什么是线程?
详细代码介绍:
博文来源:http://blog.csdn.net/zhaocundang 如果你对代码有些疑问,请在下面留言来探讨,或者加我的QQ463431476一起学习进步!
//线程mRunnable启动
private Runnable mRunnable = new Runnable()
{
public void run()
{
try
{
//连接服务器
mSocketClient = new Socket(wifiurl,Integer.parseInt(wificom));
//取得输入、输出流
mBufferedReaderClient = new BufferedReader(new InputStreamReader(mSocketClient.getInputStream()));
mPrintWriterClient = new PrintWriter(mSocketClient.getOutputStream(), true);
recvMessageClient = "wifi成功连接";//消息换行
// Toast.makeText(getActivity(),"wifi连接成功",Toast.LENGTH_SHORT).show();
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
}
catch (Exception e)
{
recvMessageClient = "连接错误!";//消息换行
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
return;
}
setRecvMessage();
}
};
public void setRecvMessage(){
char[] buffer = new char[256];
int count = 0;
while (true)
{
try
{
if((count = mBufferedReaderClient.read(buffer))>0)
{
recvMessageClient = getInfoBuff(buffer, count);
//将接收的数据放入数组
Thread.sleep(500);
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
}
}
catch (Exception e)
{
recvMessageClient = "接收异常:" + e.getMessage() + "\n";//消息换行
Message msg = new Message();
msg.what = 0;
mHandler.sendMessage(msg);
}
}
}
//handler用于线程中更新界面数据
Handler mHandler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
if(msg.what == 0)
{
Toast.makeText(getActivity(),recvMessageClient,Toast.LENGTH_SHORT).show();//刷新消息机制
}
else if(msg.what == 1)
{
try
{
//// String result = recvMessageClient;
// Toast.makeText(getActivity(),result,Toast.LENGTH_SHORT).show(); //接收消息的
String [] results = recvMessageClient.split("\\.");;
// Toast.makeText(getActivity(),results[3],Toast.LENGTH_SHORT).show(); //接收消息的
/*
Intent intent=new Intent();
intent.putExtra("temp",results[0]);
intent.putExtra("humi",results[2]);
intent.setAction("zcd.netanything.temp");
getActivity().sendBroadcast(intent);
*/
}catch(Exception e)
{
// Toast.makeText(getActivity(),"无法获取数据,检查网络是否连接!",Toast.LENGTH_SHORT).show();//刷新消息机制
}
}
}
};
//接收处理
private String getInfoBuff(char[] buff, int count)
{
char[] temp = new char[count];
for(int i=0; i<count; i++)
{
temp[i] = buff[i];
}
return new String(temp);
}
/*public void onDestroy() {
super.onDestroy();
if (isConnect)
{
isConnect = false;
try {
if(mSocketClient!=null)
{
mSocketClient.close();
mSocketClient = null;
//mThreadvideo.destroy();
mPrintWriterClient.close();
mPrintWriterClient = null;
recvMessageClient = "网络端口成功";//消息换行
Message msg = new Message();
msg.what = 0;
mHandler.sendMessage(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
mThreadClient.interrupt();
}
}
*/
按键触发,开启线程:
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
mPrintWriterClient.print("f");
//mPrintWriterClient.print("t");
mPrintWriterClient.flush();//清空
Toast.makeText(getActivity(),"前进",Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
mPrintWriterClient.print("b");
// mPrintWriterClient.print("t");
mPrintWriterClient.flush();
Toast.makeText(getActivity(),"后退",Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
mPrintWriterClient.print("l");
// mPrintWriterClient.print("t");
mPrintWriterClient.flush();
Toast.makeText(getActivity(),"向左",Toast.LENGTH_SHORT).show();
break;
case R.id.button4:
mPrintWriterClient.print("r");
// mPrintWriterClient.print("t");
mPrintWriterClient.flush();
Toast.makeText(getActivity(),"向右",Toast.LENGTH_SHORT).show();
break;
case R.id.button5:
if(!isConnect)
{
//开启mThreadClient线程
mThreadClient = new Thread(mRunnable);
mThreadClient.start();
Toast.makeText(getActivity(),"尝试连接网络",500).show();
}
else {
onDestroy();
isConnect=false;
}
break;
//case R.id.button6:
//关闭wifi
/* if (isConnect)
{
isConnect = false;
try {
if(mSocketClient!=null)
{
mSocketClient.close();
mSocketClient = null;
//mThreadvideo.destroy();
mPrintWriterClient.close();
mPrintWriterClient = null;
recvMessageClient = "网络端口成功";//消息换行
Message msg = new Message();
msg.what = 0;
mHandler.sendMessage(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
mThreadClient.interrupt();
}
*/
// break;
}
}
项目演示: