现在有个需求是,App与智能硬件进行UDP通讯,进行数据交互。
着手展开编写android代码,百度UDP通讯。其实还是比较简单的,并不是很困难。这是因为有现成的东西可以拿来用,那就是 DatagramSocket 。
我基于这个又重新封装一下,可以当做工具来用,实现UDP广播单播与接收消息。
注释写的还可以,直接上代码
工具类
package com.bt.mi.utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
public class SocketUtils {
private static final String TAG = "SocketUtils";
private static String IP;//记录本机IP
private static Integer BROADCAST_PORT = 62535;//端口号,最大65535
private static String BROADCAST_IP = "255.255.255.255"; //向此IP发送则为广播
private InetAddress inetAddress = null;//地址
private BroadcastThread broadcastThread;//发送消息的线程
private DatagramSocket sendSocket = null;//发送消息的socket
private ReceiveThread receiveThread;//接收消息的线程
private DatagramSocket receiveSocket = null;//接收消息的socket
private volatile boolean isRuning = true;//标志
private List<String> ipList = new ArrayList<>();//记录维护IP列表,但是我没用到
private ReceiveCallBack callBack;//设置接收消息回调
/**
* 返回状态,是否接收消息
* @return
*/
public boolean isReceiveing() {
return isRuning;
}
/***
* 设置状态开始
*/
public void startReceive() {
isRuning = true;
}
/***
* 设置状态停止
*/
public void stopReceive() {
isRuning = false;
}
/**
* 销毁
* 其实单例也不需要注销,初始化一次保持全生命周期即可
* 没想好其他的方式,小伙伴多多建议修改
*/
public void destroy() {
isRuning = false;
if (receiveSocket != null && !receiveSocket.isClosed())
receiveSocket.close();
if (sendSocket != null && !sendSocket.isClosed())
sendSocket.close();
sendSocket = null;
receiveSocket = null;
receiveThread = null;
broadcastThread = null;
}
/**
* 获取单例对象
*/
private static class SingletonClassInstance {
private static final SocketUtils instance = new SocketUtils();
}
private SocketUtils() {
}
public static SocketUtils getInstance() {
return SingletonClassInstance.instance;
}
/**
* 回调接口,需要实现此接口进行接收消息
*/
public interface ReceiveCallBack {
void receiveMsg(String message);
}
/**
* 获取本机IP
*
* @return
*/
public String getLocalIP() {
return IP;
}
/**
* 设置监听端口
*
* @param port
*/
public void setReceivePort(Integer port) {
BROADCAST_PORT = port;
}
/**
* 初始化
* 考虑是不是可以直接放到单例里去
* 但是Context和callback也不是每次获取单例都要设置,所以没有放到一起
*/
public void init(Context context, ReceiveCallBack callBack) {
try {
receiveSocket = new DatagramSocket(BROADCAST_PORT);//监听接收消息,设置监听端口
} catch (SocketException e) {
e.printStackTrace();
}
initIp(context);
receiveThread = new ReceiveThread();
receiveThread.start();
broadcastThread = new BroadcastThread();
broadcastThread.start();
this.callBack = callBack;
try {
inetAddress = InetAddress.getByName(BROADCAST_IP);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送广播
*
* @param message
*/
public void sendBroadcast(String message) {
sendUnicast(message, BROADCAST_IP);
}
/**
* 发送单播
* 实际用到的是handler机制进行线程通讯发送
*
* @param message
* @param ip
*/
public void sendUnicast(String message, String ip) {
try {
inetAddress = InetAddress.getByName(ip);
} catch (Exception e) {
e.printStackTrace();
}
Message msg = Message.obtain();
msg.obj = message;
msg.what = 1;
broadcastThread.mhandler.sendMessage(msg);
}
private void initIp(Context context) {
//Wifi状态判断
// WifiManager.MulticastLock lock;
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
IP = getIpString(wifiInfo.getIpAddress());
// ipInfo.append(IP);
System.out.println("IP IP:" + IP);
}
// lock = wifiManager.createMulticastLock("udpservice");
}
/**
* 接收消息的handler,通过接口回调出去消息内容
*/
@SuppressLint("HandlerLeak")
Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1: {
if (!msg.obj.equals(IP)) {
if (!isExistIp(msg.obj.toString())) {
ipList.add(msg.obj.toString());
}
if (callBack != null)
callBack.receiveMsg(msg.obj.toString());
}
}
break;
default:
break;
}
}
};
/**
* 接收消息的线程类,继承Thread
* 循环接收数据。DatagramPacket是收发的数据包
* receive方法接收消息,receiveSocket初始化在init方法中
* 收到消息使用Handler传递出去
*/
private class ReceiveThread extends Thread {
@Override
public void run() {
while (true) {
if (isRuning) {
byte[] receiveData = new byte[1024];
DatagramPacket dpReceive = null;
ipList.clear();
dpReceive = new DatagramPacket(receiveData, receiveData.length);
try {
receiveSocket.receive(dpReceive);
} catch (IOException e) {
e.printStackTrace();
}
String recIp = dpReceive.getAddress().toString().substring(1);
if (dpReceive != null) {
Message revMessage = Message.obtain();
revMessage.what = 1;
revMessage.obj = recIp;
Log.i(TAG, "handleMessage: receive ip" + recIp);
myHandler.sendMessage(revMessage);
}
}
}
}
}
/**
* 发送消息的线程类
* 使用Looper.loop();循环读取队列,一直监听消息
* 根据message内容构建DatagramPacket 发送消息的数据包,inetAddress初始化在调用发送消息方法时设置,端口也是固定的
* 记录打印发送耗时
*/
public class BroadcastThread extends Thread {
private Handler mhandler = null;
@SuppressLint("HandlerLeak")
@Override
public void run() {
Looper.prepare();
mhandler = new Handler() {
@Override
public void handleMessage(Message msg) {
String message = (String) msg.obj;
byte[] data = message.getBytes();
DatagramPacket dpSend = null;
dpSend = new DatagramPacket(data, data.length, inetAddress, BROADCAST_PORT);
try {
double start = System.currentTimeMillis();
sendSocket = new DatagramSocket();
sendSocket.send(dpSend);
sendSocket.close();
Log.i(TAG, "sendMessage: data " + new String(data));
double end = System.currentTimeMillis();
double times = end - start;
Log.i(TAG, "receive: executed time is : " + times + "ms");
} catch (IOException e) {
e.printStackTrace();
}
}
};
Looper.loop();
}
}
private boolean isExistIp(String revIp) {
if (ipList != null && ipList.size() > 0 && revIp != null) {
for (String ip : ipList) {
if (ip == revIp) {
return true;
}
}
}
return false;
}
/**
* 将获取到的int型ip转成string类型
*/
private String getIpString(int i) {
return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "."
+ ((i >> 16) & 0xFF) + "." + (i >> 24 & 0xFF);
}
}
Activity使用
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.bt.mi.R;
import com.bt.mi.utils.SocketUtils;
/**
* UDP通讯activity
* 其实 这个activity也很简单,总共三个按钮,一个输入框,两个输出的textView
* 实现输入发送消息,接收消息功能
*/
public class UdpSocketServerActivity extends Activity implements SocketUtils.ReceiveCallBack {
private static final String TAG = "SocketAutoConnectServer";
private Button sendUDPBrocast;
private TextView ipInfo;
private Button btn_send;
private EditText et_sendInfo;
private TextView tv_receive;
private Button btnClear;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SocketUtils.getInstance().init(this, this);//初始化连接对象
initView();
ipInfo.append(SocketUtils.getInstance().getLocalIP());//获取显示本机IP
}
private void initView() {
ipInfo = (TextView) findViewById(R.id.ip_info);
sendUDPBrocast = (Button) findViewById(R.id.sendUDPBrocast);
tv_receive = findViewById(R.id.tv_receive);
et_sendInfo = findViewById(R.id.et_sendContent);
btn_send = findViewById(R.id.btn_sendInfo);
btn_send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
SocketUtils.getInstance().sendUnicast(et_sendInfo.getText().toString(), "192.168.137.84");//发送消息,单播
// SocketUtils.getInstance().sendBroadcast(et_sendInfo.getText().toString());//发送广播消息
}
});
btnClear = findViewById(R.id.btnClear);
btnClear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
tv_receive.setText("");
}
});
sendUDPBrocast.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (SocketUtils.getInstance().isReceiveing()) {
SocketUtils.getInstance().stopReceive();//绑定按钮事件,是否开启接收消息
sendUDPBrocast.setText("接收广播");
System.out.println("现在停止接收广播..");
} else {
SocketUtils.getInstance().startReceive();
sendUDPBrocast.setText("停止接收");
System.out.println("现在接收广播..");
}
}
});
}
/**
* 接收到消息的回调方法,可以使用匿名内部类进行接收,也可以实现接口
* @param message
*/
@Override
public void receiveMsg(String message) {
tv_receive.append(message + " 接收到信息 " + "\n");
}
@Override
protected void onDestroy() {
super.onDestroy();
SocketUtils.getInstance().destroy();//注销。其实单例也不需要注销,初始化一次保持全生命周期即可
System.out.println("UDP Server程序退出,关掉socket,停止广播");
finish();
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/sendUDPBrocast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止广播"/>
<TextView
android:id="@+id/ip_info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="服务端自身IP: " />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="发送的内容"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et_sendContent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/btn_sendInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="send"/>
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="clear"/>
<TextView
android:id="@+id/tv_receive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
权限
最后,一定要注意权限!一定要注意权限!一定要注意权限!
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
好的,到此为止结束了。