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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
package org.fueri.reeldroid.network;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
/***
*
* DeviceManager singleton
*
* @author Patrick Fürlinger fueri@fueri.ch
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*
*/
public class DeviceManager {
private static final int MAX_PING_TIMEOUT_MS = 40;
public static interface ProgressListener {
void publish(String currentIP);
}
/**
*
* @param defaultPort
* @return
*/
public static List<String> findVDRHosts(Context context,
Integer defaultPort, ProgressListener listener) {
if (defaultPort == null) {
defaultPort = 6420;
}
List<String> list = new ArrayList<String>();
ConnectivityManager conMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
boolean netStatus = conMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()
|| conMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET)
.isConnected();
if (netStatus == false) {
return list;
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String baseIp = intToBaseIp(wifiInfo.getIpAddress());
for (Integer i = 1; i <= 254; i++) {
InetAddress ia;
try {
String ipHost = baseIp + i.toString();
if (listener != null) {
listener.publish(ipHost);
}
ia = InetAddress.getByName(ipHost);
if (findHost(ia, defaultPort, MAX_PING_TIMEOUT_MS) == false) {
continue;
}
list.add(ipHost);
} catch (UnknownHostException e) {
}
}
return list;
}
/**
*
* @param ip
* @param port
* @return
*/
public static boolean findHost(String ip, int port, int pingTimeout) {
InetAddress ia = null;
try {
ia = InetAddress.getByName(ip);
} catch (UnknownHostException e) {
return false;
}
return findHost(ia, port, pingTimeout);
}
/**
*
* @param ip
* @param port
* @return
*/
public static boolean findHost(InetAddress ip, int port, int pingTimeout) {
try {
InetAddress address = ip;
boolean reachable = address.isReachable(pingTimeout);
if (reachable == false) {
return false;
}
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), 1000);
socket.close();
return true;
} catch (Exception e) {
return false;
}
}
/**
* return base ip as a string.
*
* @param ip_address
* @return String
*/
public static String intToBaseIp(int ip_address) {
return (ip_address & 0xFF) + "." + ((ip_address >> 8) & 0xFF) + "."
+ ((ip_address >> 16) & 0xFF) + ".";
}
}
|