发布于 11个月前

uniapp Andorid下实现的获取MAC地址

测了从安卓4.1的古董机到小米9,共5台手机,均能获得正确结果,参考API:https://developer.android.com/reference/java/net/NetworkInterface#getHardwareAddress()

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;  
    }  
    str += tmp;  
}  
console.log('mac', str)
©2020 edoou.com   京ICP备16001874号-3