Liangxiaoman
Liangxiaoman
  • 发布:2020-12-08 12:39
  • 更新:2021-09-02 17:24
  • 阅读:2275

uniapp获取设备的ip和Mac----仅支持安卓

分类:uni-app
                          //获取Ip地址  
            getDeviceIp(){  
                           var deviceIp = ‘’   
                if(plus.os.name=="Android"){  
                    var Context = plus.android.importClass("android.content.Context");  
                    var wifiManager =plus.android.runtimeMainActivity().getSystemService(Context.WIFI_SERVICE);  
                    var wifiInfo = plus.android.invoke(wifiManager, "getConnectionInfo");  
                    var ipAddress = plus.android.invoke(wifiInfo, "getIpAddress");  
                    deviceIp = '';  
                    if (ipAddress != 0) {  
                        deviceIp = ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "." + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));  
                    }  
                }  
                console.log(deviceIp)  
            return deviceIp;  
            },
                           //获取mac地址  
            getDeviceMac(){  
                                var deviceMac = ‘’   
                var net = plus.android.importClass("java.net.NetworkInterface")  
                var wl0 = net.getByName('wlan0')  
                var macByte = wl0.getHardwareAddress()  
                deviceMac = ''  
                for (var i = 0; i < macByte.length; i++) {  
                    var tmp = "";  
                    var num = macByte[i];  
                    if (num < 0) {  
                        tmp = (255 + num + 1).toString(16);  
                    } else {  
                        tmp = num.toString(16);  
                    }  
                    if (tmp.length == 1) {  
                        tmp = "0" + tmp;  
                    }  
                    deviceMac += tmp;  
                }  
                console.log(deviceMac)  
            },
2 关注 分享
1***@qq.com 3***@qq.com

要回复文章请先登录注册

1***@qq.com

1***@qq.com

回复 2***@qq.com :
我这边可以 具体逻辑在下



//获取当前设备网络Ip地址

function getCurrentNetIp() {

let deviceIp = '127.0.0.1'

if (plus.os.name == "Android") {

var Context = plus.android.importClass("android.content.Context");

var wifiManager = plus.android.runtimeMainActivity().getSystemService(Context.WIFI_SERVICE);

var wifiInfo = plus.android.invoke(wifiManager, "getConnectionInfo");

var ipAddress = plus.android.invoke(wifiInfo, "getIpAddress");

deviceIp = '';

if (ipAddress != 0) {

deviceIp = ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "." + (ipAddress >> 16 & 0xff) +

"." + (ipAddress >> 24 & 0xff));

}

} else if (plus.os.name == "iOS") {



}

return deviceIp

}



// 判断字符串是否存在数组

function IsInArray(arr, val) {

var testStr = ',' + arr.join(",") + ",";

return testStr.indexOf("," + val + ",") != -1;

}

let requestRootApi = null

// 根据当前网络 判断

function currentNetType(currentNetType) {

requestRootApi = null

return new Promise((resolve, reject) => {

switch (currentNetType) {

case "wifi":

// 判断当前网络 ip是内网还是外网

let currentNetIp = getCurrentNetIp();

// 100.X.X.X 10.X.X.X 192.168.X.X 172.16.X.X-172.31.X.X 172.15.xxx 内网ip范围

let insideRang1 = ['100', '10']

let insideRang2 = ['192.168']

let insideRang3 = ['17216', '17217', '17218', '17219', '17220', '17221', '17222', '17223',

'17224', '17225',

'17226', '17227', '17228', '17229', '17230', '17231'

]

let insideRang4 = ['17215']

let splitGetIp = currentNetIp.split('.')

if (splitGetIp.length === 4) {

if (IsInArray(insideRang1, splitGetIp[0]) ||

IsInArray(insideRang2, currentNetIp.substring(0, currentNetIp.indexOf(".") + 4)) ||

IsInArray(insideRang3, currentNetIp.substring(0, currentNetIp.indexOf(".") + 4).replace(

'.'))) {

uni.request({

url: 'http://pv.sohu.com/cityjson?ie=utf-8',

method: 'POST',

success: (res) => {

requestRootApi = 'http://eafc-beta.qieguo.net/api'

resolve('http://eafc-beta.qieguo.net/api')

},

fail: (f) => {

// 内网

requestRootApi = 'http://172.28.121.124:8080'

resolve('http://172.28.121.124:8080')

}

})



} else {

requestRootApi = 'http://eafc-beta.qieguo.net/api'

resolve('http://eafc-beta.qieguo.net/api')

}

} else if (splitGetIp.length === 3 && IsInArray(insideRang4, splitGetIp[0] + splitGetIp[1])) {

// 内网

requestRootApi = 'http://172.28.121.124:8080'

resolve('http://172.28.121.124:8080')

} else {

requestRootApi = 'http://eafc-beta.qieguo.net/api'

resolve('http://eafc-beta.qieguo.net/api')

}

break;

case "none":

// 当前无网络

uni.showToast({

title: '当前网络无连接,请检查网络设置!',

duration: 5000,

icon: 'none'

});

requestRootApi = 'http://eafc-beta.qieguo.net/api'

resolve('http://eafc-beta.qieguo.net/api')

break;

default:

// 当前网络可能是2g 3g 4g 5g ethernet/有线网络 unknown/Android 下不常见的网络类型

requestRootApi = 'http://eafc-beta.qieguo.net/api'

resolve('http://eafc-beta.qieguo.net/api')

break;

}





})

}
2021-09-02 17:24
1***@qq.com

1***@qq.com

//获取MAC地址
getMacAddress() {
var macAddress = '';
if (plus.os.name == 'Android') {  
    console.log("plus.os.version", plus.os.version)  
    // 6.0以上版本获取MAX地址的方法
    if(plus.os.version >= 6.0){  
        var StringBuffer = plus.android.importClass("java.lang.StringBuffer");  
        var buf = new StringBuffer();  
        var NetworkInterface = plus.android.importClass("java.net.NetworkInterface");  
        var networkInterface = null;  
        try {  
            networkInterface = NetworkInterface.getByName("eth1");  
            if (networkInterface == null) {  
                networkInterface = NetworkInterface.getByName("wlan0");  
            }  
            if (networkInterface == null) {  
                return "02:00:00:00:00:02";  
            }  
            var addr = networkInterface.getHardwareAddress();  
            // 这个获取服务器的addr没看懂是什么意思
            console.log("addr", addr);  
            
            for (let i = 0; i < addr.length; i++) {  
                if (addr[i] > 0) {  
                    let str = Number(addr[i]).toString(16).toUpperCase();  
                    buf.append(str.length == 1 ?"0"+ str+":":str+":");  
                } else {  
                    let str = Number(256 + addr[i]).toString(16).toUpperCase();  
                    buf.append(str.length == 1 ?"0"+str+":":str+":");  
                }  
            }  
            if (buf.length() > 0) {  
                buf.deleteCharAt(buf.length() - 1);  
            }  
            macAddress = buf.toString();  
            console.log("macAddress >= 6.0", macAddress);  
        } catch (err) {  
            console.log("err", err);  
        }  
    }  
    // 6.0以下版本设备获取MAX地址的方法
    if(plus.os.version < 6.0){  
        var Context = plus.android.importClass("android.content.Context");  
        var WifiManager = plus.android.importClass("android.net.wifi.WifiManager");  
        var wifiManager = plus.android.runtimeMainActivity().getSystemService(Context.WIFI_SERVICE);  
        var WifiInfo = plus.android.importClass("android.net.wifi.WifiInfo");  
        var wifiInfo = wifiManager.getConnectionInfo();  
        macAddress = wifiInfo.getMacAddress();  
        console.log("macAddress < 6.0", macAddress);  
    }      
    uni.setStorageSync('macAddress', macAddress);
    console.log('打印数据 ----B----')
    console.log(uni.getStorageSync('macAddress'))
    return macAddress
}  
},
2021-06-18 13:45
2***@qq.com

2***@qq.com

回复 1***@qq.com :
你后面好了么
2021-05-18 12:40
2***@qq.com

2***@qq.com

我的获取到的是0.0.0.0,不对额
2021-05-18 11:50
1***@qq.com

1***@qq.com

获取到的有点出入哈 获取到的ip是10.160.149.39 但实际是10.160.148.156 应该会有影响吧 还有就是获取到了这个ip怎么判断是否是公网ip还是内网ip
2021-05-16 11:58