1. function getIp() {
    2. var ip = "127.0.0.1";
    3. var Context = plus.android.importClass("android.content.Context");
    4. if (plus.os.name == "Android") {
    5. //WifiManager
    6. var Context = plus.android.importClass("android.content.Context");
    7. var WifiManager = plus.android.importClass("android.net.wifi.WifiManager");
    8. var wifiManager = plus.android.runtimeMainActivity().getSystemService(Context.WIFI_SERVICE);
    9. var WifiInfo = plus.android.importClass("android.net.wifi.WifiInfo");
    10. if (!wifiManager.isWifiEnabled()) { //未连接wifi
    11. var Enumeration = plus.android.importClass("java.util.Enumeration");
    12. var NetworkInterface = plus.android.importClass("java.net.NetworkInterface");
    13. var Inet4Address = plus.android.importClass("java.net.Inet4Address");
    14. var en = NetworkInterface.getNetworkInterfaces();
    15. while (plus.android.invoke(en, "hasMoreElements")) {
    16. var ni = plus.android.invoke(en, "nextElement");
    17. var interfaceName = ni.getDisplayName();
    18. //console.log("tag", "网络名字" + interfaceName);
    19. var enumIpAddr = plus.android.invoke(ni, "getInetAddresses")
    20. while (plus.android.invoke(enumIpAddr, "hasMoreElements")) {
    21. var inetAddress = plus.android.invoke(enumIpAddr, "nextElement");
    22. if (!plus.android.invoke(inetAddress, "isLoopbackAddress") && inetAddress instanceof Inet4Address) {
    23. ip = plus.android.invoke(inetAddress, "getHostAddress");
    24. return ip;
    25. }
    26. }
    27. }
    28. } else { //连接wifi
    29. var wifiInfo = wifiManager.getConnectionInfo();
    30. ip = intToIp(wifiInfo.getIpAddress());
    31. var wifiInfo = wifiManager.getConnectionInfo();
    32. var ipAddress = wifiInfo.getIpAddress();
    33. ip = intToIp(ipAddress);
    34. }
    35. }
    36. return ip;
    37. }
    38. function intToIp(i) {
    39. return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 24) & 0xFF);
    40. }