Files
pda-web/app/src/main/java/chaoran/business/utils/LocalAddressUtil.java

122 lines
4.3 KiB
Java
Raw Normal View History

2022-10-20 14:22:04 +08:00
package chaoran.business.utils;
import android.annotation.SuppressLint;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
2022-10-20 14:22:04 +08:00
import android.os.Build;
import android.util.Log;
import android.webkit.JavascriptInterface;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class LocalAddressUtil {
private Context context;
private int[] heights;
public LocalAddressUtil(Context context) {
this.context = context;
this.heights = StatusBarUtil.getStatusBarHeight(context);
}
2022-10-20 14:22:04 +08:00
@SuppressLint("JavascriptInterface")
@JavascriptInterface
public String getIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
// if(!intf.getDisplayName().equals("eth0")) continue;
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
Log.e("address: local", ex.toString());
}
return null;
}
@SuppressLint("JavascriptInterface")
@JavascriptInterface
public String getMacAddress(){//可以兼容安卓7以下
String macAddress = null;
StringBuffer buf = new StringBuffer();
NetworkInterface networkInterface = null;
try {
networkInterface = NetworkInterface.getByName("eth1");
if (networkInterface == null) {
networkInterface = NetworkInterface.getByName("wlan0");
}
if (networkInterface == null) {
return "02:00:00:00:00:02";
}
byte[] addr = networkInterface.getHardwareAddress();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
macAddress = buf.toString();
} catch (SocketException e) {
e.printStackTrace();
return "02:00:00:00:00:02";
}
return macAddress;
}
@SuppressLint("JavascriptInterface")
@JavascriptInterface
public String getInfo(int type){
String info;
switch (type) {
case 1: info = Build.BRAND;break;//品牌
case 2: info = Build.MODEL;break;//型号
case 3: info = Build.MANUFACTURER;break;//厂商
case 4: info = Build.DEVICE;break;//设备名
case 5: info = Build.ID;break;//设备硬件id
// case 6: info = Build.SERIAL;break;//序列号,可能获取不到
default: info = "";break;
}
return info;
}
public String getHeight(int type) {
String info = "";
switch (type) {
case 1 : info = String.valueOf(this.heights[0]); break;
case 2 : info = String.valueOf(this.heights[1]); break;
}
return info;
2022-10-20 14:22:04 +08:00
}
public void test(){
Log.e("test", "MANUFACTURER=" + Build.MANUFACTURER);
Log.e("test", "BRAND=" + Build.BRAND);
Log.e("test", "MODEL=" + Build.MODEL);
Log.e("test", "VERSION.RELEASE=" + Build.VERSION.RELEASE);
Log.e("test", "VERSION.SDK_INT=" + Build.VERSION.SDK_INT);
Log.e("test", "DEVICE=" + Build.DEVICE);
Log.e("test", "HOST=" + Build.HOST);
Log.e("test", "ID=" + Build.ID);
Log.e("test", "TIME=" + Build.TIME);
Log.e("test", "TYPE=" + Build.TYPE);
Log.e("test", "PRODUCT=" + Build.PRODUCT);
Log.e("test", "BOARD=" + Build.BOARD);
Log.e("test", "DISPLAY=" + Build.DISPLAY);
Log.e("test", "FINGERPRINT=" + Build.FINGERPRINT);
Log.e("test", "HARDWARE=" + Build.HARDWARE);
Log.e("test", "BOOTLOADER=" + Build.BOOTLOADER);
Log.e("test", "TAGS=" + Build.TAGS);
Log.e("test", "UNKNOWN=" + Build.UNKNOWN);
Log.e("test", "USER=" + Build.USER);
}
}