-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGDMService.java
90 lines (79 loc) · 2.83 KB
/
GDMService.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
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.net.DhcpInfo;
import android.net.wifi.WifiManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
public class GDMService extends IntentService {
public static final String MSG_RECEIVED = ".GDMService.MESSAGE_RECEIVED";
public static final String SOCKET_CLOSED = ".GDMService.SOCKET_CLOSED";
private static final String multicast = "239.0.0.250";
public GDMService() {
super("GDMService");
}
@Override
protected void onHandleIntent(Intent intent) {
try {
DatagramSocket socket = new DatagramSocket(32414);
socket.setBroadcast(true);
String data = "M-SEARCH * HTTP/1.1\r\n\r\n";
DatagramPacket packet = new DatagramPacket(data.getBytes(),
data.length(), useMultiCastAddress(), 32414);
// DatagramPacket packet = new DatagramPacket(data.getBytes(),
// data.length(), getBroadcastAddress(), 32414);
socket.send(packet);
Log.d("GDMService", "Search Packet Broadcasted");
byte[] buf = new byte[256];
packet = new DatagramPacket(buf, buf.length);
socket.setSoTimeout(2000);
boolean listening = true;
while (listening) {
try {
socket.receive(packet);
String packetData = new String(packet.getData());
if (packetData.contains("HTTP/1.0 200 OK")) {
Log.d("GDMService", "PMS Packet Received");
// Broadcast Received Packet
Intent packetBroadcast = new Intent(
GDMService.MSG_RECEIVED);
packetBroadcast.putExtra("data", packetData);
packetBroadcast.putExtra("ipaddress", packet
.getAddress().toString());
LocalBroadcastManager.getInstance(this).sendBroadcast(
packetBroadcast);
}
} catch (SocketTimeoutException e) {
Log.d("GDMService", "Socket Timeout");
socket.close();
listening = false;
Intent socketBroadcast = new Intent(
GDMService.SOCKET_CLOSED);
LocalBroadcastManager.getInstance(this).sendBroadcast(
socketBroadcast);
}
}
} catch (IOException e) {
Log.e("GDMService", e.toString());
}
}
// Builds the broadcast address based on the local network
protected InetAddress getBroadcastAddress() throws IOException {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifi.getDhcpInfo();
// handle null somehow
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);
return InetAddress.getByAddress(quads);
}
protected InetAddress useMultiCastAddress() throws IOException {
return InetAddress.getByName(multicast);
}
}