支持低版本的安卓手机,以及模拟器
[JavaScript] 纯文本查看 复制代码 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 > 0) {
let str = Number(addr).toString(16).toUpperCase();
buf.append(str.length == 1 ?"0"+ str+":":str+":");
} else {
let str = Number(256 + addr).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
}
}
|