uni-app获取mac地址

plus依赖真机,真机上运行才会起效果,测试了Android 5到9,完全没问题。

/**
     * 获取Androoid设备mac地址
     * */
    function getMacAddress(){
        var net = plus.android.importClass("java.net.NetworkInterface")  
        var wl0 = net.getByName('wlan0')  
        var macByte = wl0.getHardwareAddress()  
        var str = ''  
        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;  
            }  
            if(i == macByte.length-1){
                str += tmp; 
            }else{
                str = str + tmp + "-"; 
            }
        }  
        console.log('mac', str.toUpperCase())
        return str.toUpperCase()
    }

你可能感兴趣的:(uni-app获取mac地址)