|
@ -1,18 +1,13 @@
|
1
|
1
|
package com.ai.ipu.mobile.plugin;
|
2
|
2
|
|
3
|
|
import java.net.InetAddress;
|
4
|
|
import java.net.NetworkInterface;
|
5
|
|
import java.net.SocketException;
|
6
|
|
import java.util.Enumeration;
|
7
|
|
import java.util.TimeZone;
|
8
|
|
|
9
|
|
import org.json.JSONArray;
|
10
|
|
|
|
3
|
import android.annotation.SuppressLint;
|
11
|
4
|
import android.content.Context;
|
12
|
5
|
import android.net.wifi.WifiInfo;
|
13
|
6
|
import android.net.wifi.WifiManager;
|
|
7
|
import android.os.Build;
|
14
|
8
|
import android.provider.Settings;
|
15
|
9
|
import android.telephony.TelephonyManager;
|
|
10
|
import android.text.TextUtils;
|
16
|
11
|
|
17
|
12
|
import com.ai.ipu.mobile.frame.IIpuMobile;
|
18
|
13
|
import com.ai.ipu.mobile.frame.plugin.Plugin;
|
|
@ -23,9 +18,26 @@ import com.ailk.common.data.IData;
|
23
|
18
|
import com.ailk.common.data.impl.DataMap;
|
24
|
19
|
import com.mashape.relocation.conn.util.InetAddressUtils;
|
25
|
20
|
|
|
21
|
import org.json.JSONArray;
|
|
22
|
|
|
23
|
import java.io.BufferedReader;
|
|
24
|
import java.io.File;
|
|
25
|
import java.io.FileReader;
|
|
26
|
import java.io.IOException;
|
|
27
|
import java.net.InetAddress;
|
|
28
|
import java.net.NetworkInterface;
|
|
29
|
import java.net.SocketException;
|
|
30
|
import java.util.Collections;
|
|
31
|
import java.util.Enumeration;
|
|
32
|
import java.util.List;
|
|
33
|
import java.util.Locale;
|
|
34
|
import java.util.TimeZone;
|
|
35
|
|
26
|
36
|
public class MobileInfo extends Plugin {
|
27
|
37
|
public String platform = "Android";
|
28
|
38
|
|
|
39
|
public static final String MAC_FAIL = "02:00:00:00:00:00";
|
|
40
|
|
29
|
41
|
public MobileInfo(IIpuMobile ipumobile) {
|
30
|
42
|
super(ipumobile);
|
31
|
43
|
}
|
|
@ -198,9 +210,94 @@ public class MobileInfo extends Plugin {
|
198
|
210
|
}
|
199
|
211
|
|
200
|
212
|
public String getMac() {
|
201
|
|
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
202
|
|
WifiInfo info = wifi.getConnectionInfo();
|
203
|
|
return info.getMacAddress();
|
|
213
|
String mac = null;
|
|
214
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
|
215
|
mac = getMacDefault(context);
|
|
216
|
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
|
217
|
mac = getMacFromFile();
|
|
218
|
} else {
|
|
219
|
mac = getMacFromHardware();
|
|
220
|
}
|
|
221
|
return mac;
|
|
222
|
}
|
|
223
|
|
|
224
|
/**
|
|
225
|
* 遍历循环所有的网络接口,找到接口是 wlan0
|
|
226
|
* 必须的权限 <uses-permission android:name="android.permission.INTERNET" />
|
|
227
|
* @return
|
|
228
|
*/
|
|
229
|
private String getMacFromHardware() {
|
|
230
|
try {
|
|
231
|
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
|
|
232
|
for (NetworkInterface nif : all) {
|
|
233
|
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
|
|
234
|
|
|
235
|
byte[] macBytes = nif.getHardwareAddress();
|
|
236
|
if (macBytes == null) {
|
|
237
|
return MAC_FAIL;
|
|
238
|
}
|
|
239
|
|
|
240
|
StringBuilder res1 = new StringBuilder();
|
|
241
|
for (byte b : macBytes) {
|
|
242
|
res1.append(String.format("%02X:", b));
|
|
243
|
}
|
|
244
|
|
|
245
|
if (res1.length() > 0) {
|
|
246
|
res1.deleteCharAt(res1.length() - 1);
|
|
247
|
}
|
|
248
|
return res1.toString();
|
|
249
|
}
|
|
250
|
} catch (Exception e) {
|
|
251
|
e.printStackTrace();
|
|
252
|
}
|
|
253
|
return MAC_FAIL;
|
|
254
|
}
|
|
255
|
|
|
256
|
/**
|
|
257
|
* Android 6.0(包括) - Android 7.0(不包括)
|
|
258
|
* @return
|
|
259
|
*/
|
|
260
|
private String getMacFromFile() {
|
|
261
|
String WifiAddress = MAC_FAIL;
|
|
262
|
try {
|
|
263
|
WifiAddress = new BufferedReader(new FileReader(new File("/sys/class/net/wlan0/address"))).readLine();
|
|
264
|
} catch (IOException e) {
|
|
265
|
e.printStackTrace();
|
|
266
|
}
|
|
267
|
return WifiAddress;
|
|
268
|
}
|
|
269
|
|
|
270
|
/**
|
|
271
|
* Android 6.0 之前(不包括6.0)
|
|
272
|
* 必须的权限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
|
273
|
* @param context
|
|
274
|
* @return
|
|
275
|
*/
|
|
276
|
@SuppressLint("HardwareIds")
|
|
277
|
private String getMacDefault(Context context) {
|
|
278
|
String mac = MAC_FAIL;
|
|
279
|
if (context == null) {
|
|
280
|
return mac;
|
|
281
|
}
|
|
282
|
|
|
283
|
WifiManager wifi = (WifiManager) context.getApplicationContext()
|
|
284
|
.getSystemService(Context.WIFI_SERVICE);
|
|
285
|
if (wifi == null) {
|
|
286
|
return mac;
|
|
287
|
}
|
|
288
|
WifiInfo info = null;
|
|
289
|
try {
|
|
290
|
info = wifi.getConnectionInfo();
|
|
291
|
} catch (Exception ignored) {
|
|
292
|
}
|
|
293
|
if (info == null) {
|
|
294
|
return null;
|
|
295
|
}
|
|
296
|
mac = info.getMacAddress();
|
|
297
|
if (!TextUtils.isEmpty(mac)) {
|
|
298
|
mac = mac.toUpperCase(Locale.ENGLISH);
|
|
299
|
}
|
|
300
|
return mac;
|
204
|
301
|
}
|
205
|
302
|
|
206
|
303
|
public String getIP() throws SocketException {
|