-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMulticastDemo.java
115 lines (103 loc) · 3.77 KB
/
MulticastDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.example.getlocalinfo;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.ArrayList;
import java.util.List;
import xlwireless.deviceutility.XLWirelessUtility;
import xlwireless.tools.XLWirelessLog;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.AsyncTask;
/// 测试Android多设备之间的组播程序
class MulticastDemo {
// / 测试Android多设备之间的组播程序之初始化与反初始化。
private MulticastLock multicastLock = null;
public void init(Context c) {
// Android的Wifi,默认情况下是不接受组播的,调用MulticastLock对象的acquire方法,获取到组播锁。
WifiManager wifiManager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
multicastLock = wifiManager.createMulticastLock("multicast.test");
multicastLock.acquire();
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
XLWirelessLog.d(TAG, "getBSSID:" + wifiInfo.getBSSID());
}
}
public void uninit() {
if (null != multicastLock) {
multicastLock.release();
multicastLock = null;
}
}
private static final String TAG = "MulticastDemo";
private static final int MULTICAST_PORT = 5111;
private static final String GROUP_IP = "239.0.0.10";//224.5.0.7
private static class WrapAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
try {
XLWirelessLog.d(TAG, "findAroundStationIpAddress");
findAroundStationIpAddress();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
public static void asyncTaskFindAroundStation() {
new WrapAsyncTask().execute();
}
public static List<String> findAroundStationIpAddress() throws IOException {
String ip = null;
MulticastSocket multicastSocket = new MulticastSocket(MULTICAST_PORT);
multicastSocket.setLoopbackMode(true);
//multicastSocket.setSoTimeout(5000);
//multicastSocket.setSoTimeout(10 * 1000);//
InetAddress group = InetAddress.getByName(GROUP_IP);
multicastSocket.joinGroup(group);
final byte[] sendData = XLWirelessUtility.getLocalIpAddress().getBytes();
XLWirelessLog.d(TAG, "sendData: " + new String(sendData));
DatagramPacket packet = new DatagramPacket(sendData, sendData.length,
group, MULTICAST_PORT);
ArrayList<String> ipAddressList = new ArrayList<String>();
for (;;) {
multicastSocket.send(packet);
XLWirelessLog.d(TAG, ">>>send packet ok");
byte[] receiveData = new byte[256];
packet = new DatagramPacket(receiveData, receiveData.length);
XLWirelessLog.d(TAG, "0 receive packet getLength: " + packet.getLength());
multicastSocket.receive(packet);
XLWirelessLog.d(TAG, "1 receive packet getLength: " + packet.getLength());
String packetIpAddress = packet.getAddress().toString();
XLWirelessLog.d(TAG, "packet ip address: " + packetIpAddress);
packetIpAddress = packetIpAddress.substring(1, packetIpAddress.length());
StringBuilder packetContent = new StringBuilder();
for (int i = 0; i < receiveData.length; i++) {
if (receiveData[i] == 0) {
break;
}
packetContent.append((char) receiveData[i]);
}
XLWirelessLog.d(TAG, "packet content is:" + packetContent);
ip = packetContent.toString();
if (ip.equals(packetIpAddress)) {
XLWirelessLog.d(TAG, "find station's ip address: " + ip);
ipAddressList.add(ip);
} else {
XLWirelessLog.d(TAG, "not find station's ip address, continue …");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
XLWirelessLog.e(TAG, "InterruptedException e" + e);
break;
}
}
}
return ipAddressList;
}
}