取得 MAC 與 IP

1 篇文章 / 0 new
author
取得 MAC 與 IP
► 取得 Mac
public String macId = "";
public String getMacAddress1(Context ct) {
    macId = "";
    try {
        WifiManager wifiMan = (WifiManager) ct.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInf = wifiMan.getConnectionInfo();
        String id = wifiInf.getMacAddress();
        if (id != null)
            macId = id.toUpperCase(Locale.getDefault()).replaceAll(":", "-");
    } catch (Exception e) { }
    if(macId.length()==0) P.show(ct, "無法取得 MacID");
    return macId;
}
public String getMacAddress(Context ct) {
    macId = "";
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            //if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase("wlan0")) continue;
            //}
            byte[] mac = intf.getHardwareAddress();
            if (mac==null) return "";
            StringBuilder buf = new StringBuilder();
            for (int idx=0; idx<mac.length; idx++)
                buf.append(String.format("%02X-", mac[idx]));
            if (buf.length()>0) buf.deleteCharAt(buf.length()-1);
            macId = buf.toString();
            return macId;
        }
    } catch (Exception ex) { } // for now eat exceptions
    if(macId.length()==0)
        macId = getMacAddress1(ct);
    return macId;
}

► 取得 IP
public String getNetIP(Context ct) {
    ConnectivityManager connMgr = (ConnectivityManager) ct.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifi.isAvailable()) {
        WifiManager myWifiManager = (WifiManager) ct.getSystemService(Context.WIFI_SERVICE);
        WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
        int ipAddress = myWifiInfo.getIpAddress();
        return android.text.format.Formatter.formatIpAddress(ipAddress);
    } else {
        NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobile.isAvailable()) {
            return getLocalIpAddress();//"3G Available"
        } else {
            return null;//"No Network Available";
        }
    }
}
private String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf
                    .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        return null;//"Get ERROR";
    }
    return null;//"No IP";
}
► 列出裝置上所有通訊設備的 mac
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
    NetworkInterface iF = interfaces.nextElement();
    byte[] addr = iF.getHardwareAddress();
    if (addr == null || addr.length == 0) {
        continue;
    }
    StringBuilder buf = new StringBuilder();
    for (byte b : addr) {
        buf.append(String.format("%02X:", b));
    }
    if (buf.length() > 0) {
        buf.deleteCharAt(buf.length() - 1);
    }
    String mac = buf.toString();
    Log.d("mac", "interfaceName="+iF.getName()+", mac="+mac);
}
Free Web Hosting