forked from ShwoTimeNow/Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBroadcaster.java
More file actions
107 lines (97 loc) · 3.31 KB
/
Copy pathBroadcaster.java
File metadata and controls
107 lines (97 loc) · 3.31 KB
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
package com.example.test;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiManager;
public class Broadcaster {
private Context mContext;
private int mDestPort = 0;
private DatagramSocket mSocket;
public Broadcaster(Context context) {
mContext = context;
}
public boolean open(int localport,int destport) {
mDestPort = destport;
try {
mSocket = new DatagramSocket(localport);
mSocket.setBroadcast(true);
mSocket.setReuseAddress(true);
return true;
}
catch (SocketException e) {
e.printStackTrace();
}
return false;
}
public boolean close() {
if(mSocket != null && !mSocket.isClosed()) {
mSocket.close();
}
return true;
}
public boolean sendPacket(byte[] buffer) {
try {
InetAddress addr = getBroadcastAddress(mContext);
DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
packet.setAddress(addr);
packet.setPort(mDestPort);
mSocket.send(packet);
return true;
}
catch (UnknownHostException e1) {
e1.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return false;
}
public boolean recvPacket(byte[] buffer) {
DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
try {
mSocket.receive(packet);
return true;
}
catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static InetAddress getBroadcastAddress(Context context) throws UnknownHostException {
if(isWifiApEnabled(context)) {
return InetAddress.getByName("192.168.43.255");
}
WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifi.getDhcpInfo();
if(dhcp==null) {
return InetAddress.getByName("255.255.255.255");
}
int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++) {
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
}
return InetAddress.getByAddress(quads);
}
protected static Boolean isWifiApEnabled(Context context) {
try {
WifiManager manager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
Method method = manager.getClass().getMethod("isWifiApEnabled");
return (Boolean)method.invoke(manager);
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
return false;
}
}