Tag Archives: Androoid Device Mac Address

uni-app: How to Get the Androoid Device Mac Address

/**
* Get Androoid device mac address
* */
function getMacAddress(){
  var net = plus.android.importClass("java.net.NetworkInterface")
  console.log('mac', net)
  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()
}