详解Android MacAddress 适配心得

详解Android MacAddress 适配心得,第1张

概述android6.0以下mac地址获取我们获取mac地址一般都是这样写的:/***根据wifi信息获取本地mac

androID 6.0以下mac地址获取

我们获取mac地址一般都是这样写的:

 /**   * 根据wifi信息获取本地mac   * @param context   * @return   */  public static String getLocalMacAddressFromWifiInfo(Context context){    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);    WifiInfo winfo = wifi.getConnectionInfo();    String mac = winfo.getMacAddress();    return mac;  }

androID 6.0及以上、7.0以下

AndroID 6.0以后 将不再能通过 wifimanager 获取mac,获取到的mac将是固定的:02:00:00:00:00:00 。

然而我开发的sdk就是通过wifimanager获取的mac。

androID sdk后来做了6.0适配,通过cat /sys/class/net/wlan0/address,可以在6.0上获取mac地址。

 /**   * 获取mac地址   * @param context   * @return   */  public static  String getMacAddress(Context context){    //如果是6.0以下,直接通过wifimanager获取    if(Build.VERSION.SDK_INT<Build.VERSION_CODES.M){      String macAddress0 = getMacAddress0(context);      if(!TextUtils.isEmpty(macAddress0)){        return macAddress0;      }    }    String str="";    String macSerial="";    try {      Process pp = Runtime.getRuntime().exec(          "cat /sys/class/net/wlan0/address");      inputStreamReader ir = new inputStreamReader(pp.getinputStream());      lineNumberReader input = new lineNumberReader(ir);      for (; null != str;) {        str = input.readline();        if (str != null) {          macSerial = str.trim();// 去空格          break;        }      }    } catch (Exception ex) {      Log.e("----->" + "NetInfoManager","getMacAddress:" + ex.toString());    }    if (macSerial == null || "".equals(macSerial)) {      try {        return loadfileAsstring("/sys/class/net/eth0/address")            .toupperCase().substring(0,17);      } catch (Exception e) {        e.printstacktrace();        Log.e("----->" + "NetInfoManager","getMacAddress:" + e.toString());      }    }    return macSerial;  }   private static  String getMacAddress0(Context context) {    if (isAccessWifiStateAuthorized(context)) {      WifiManager wifimgr = (WifiManager) context          .getSystemService(Context.WIFI_SERVICE);      WifiInfo wifiInfo = null;      try {        wifiInfo = wifimgr.getConnectionInfo();        return wifiInfo.getMacAddress();      } catch (Exception e) {        Log.e("----->" + "NetInfoManager","getMacAddress0:" + e.toString());      }    }    return "";  }  /**   * Check whether accessing wifi state is permitted   *   * @param context   * @return   */  private static boolean isAccessWifiStateAuthorized(Context context) {    if (PackageManager.PERMISSION_GRANTED == context        .checkCallingOrSelfPermission("androID.permission.ACCESS_WIFI_STATE")) {      Log.e("----->" + "NetInfoManager","isAccessWifiStateAuthorized:" + "access wifi state is enabled");      return true;    } else      return false;  }  private static String loadfileAsstring(String filename) throws Exception {    fileReader reader = new fileReader(filename);    String text = loadReaderAsstring(reader);    reader.close();    return text;  }  private static  String loadReaderAsstring(Reader reader) throws Exception {    StringBuilder builder = new StringBuilder();    char[] buffer = new char[4096];    int readLength = reader.read(buffer);    while (readLength >= 0) {      builder.append(buffer,readLength);      readLength = reader.read(buffer);    }    return builder.toString();  }

androID 7.0及以上

androID 7.0 后,通过上述适配的方法,将获取不到mac地址。

经过调研和测试,7.0上仍有办法回去mac地址:

(1)通过ip地址来获取绑定的mac地址

 /**   * 获取移动设备本地IP   * @return   */  private static InetAddress getLocalinetAddress() {    InetAddress ip = null;    try {      //列举      Enumeration<NetworkInterface> en_netInterface = NetworkInterface.getNetworkInterfaces();      while (en_netInterface.hasMoreElements()) {//是否还有元素        NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();//得到下一个元素        Enumeration<InetAddress> en_ip = ni.getInetAddresses();//得到一个ip地址的列举        while (en_ip.hasMoreElements()) {          ip = en_ip.nextElement();          if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1)            break;          else            ip = null;        }        if (ip != null) {          break;        }      }    } catch (SocketException e) {      e.printstacktrace();    }    return ip;  }  /**   * 获取本地IP   * @return   */  private static String getLocalipAddress() {    try {      for (Enumeration<NetworkInterface> en = NetworkInterface          .getNetworkInterfaces(); en.hasMoreElements();) {        NetworkInterface intf = en.nextElement();        for (Enumeration<InetAddress> enumIpAddr = intf            .getInetAddresses(); enumIpAddr.hasMoreElements();) {          InetAddress inetAddress = enumIpAddr.nextElement();          if (!inetAddress.isLoopbackAddress()) {            return inetAddress.getHostAddress().toString();          }        }      }    } catch (SocketException ex) {      ex.printstacktrace();    }    return null;  }  /**   * 根据IP地址获取MAC地址   * @return   */  public static String getMacAddress(){    String strMacAddr = null;    try {      //获得IpD地址      InetAddress ip = getLocalinetAddress();      byte[] b = NetworkInterface.getByInetAddress(ip).getHarDWareAddress();      StringBuffer buffer = new StringBuffer();      for (int i = 0; i < b.length; i++) {        if (i != 0) { buffer.append(':');        }        String str = Integer.toHexString(b[i] & 0xFF);        buffer.append(str.length() == 1 ? 0 + str : str);      }      strMacAddr = buffer.toString().toupperCase();    } catch (Exception e) {    }    return strMacAddr;  }

 (2)扫描各个网络接口获取mac地址

 /**   * 获取设备HarDWareAddress地址   * @return   */  public static String getMachineHarDWareAddress(){    Enumeration<NetworkInterface> interfaces = null;    try {      interfaces = NetworkInterface.getNetworkInterfaces();    } catch (SocketException e) {      e.printstacktrace();    }    String harDWareAddress = null;    NetworkInterface iF = null;    while (interfaces.hasMoreElements()) {      iF = interfaces.nextElement();      try {        harDWareAddress = bytesToString(iF.getHarDWareAddress());        if(harDWareAddress != null)          break;      } catch (SocketException e) {        e.printstacktrace();      }    }    return harDWareAddress ;  }  /***   * byte转为String   * @param bytes   * @return   */  private static String bytesToString(byte[] bytes){    if (bytes == null || bytes.length == 0) {      return null ;    }    StringBuilder buf = new StringBuilder();    for (byte b : bytes) {      buf.append(String.format("%02X:",b));    }    if (buf.length() > 0) {      buf.deleteCharat(buf.length() - 1);    }    return buf.toString();  }

 (3)通过busyBox获取本地存储的mac地址

 /**   * 根据busyBox获取本地Mac   * @return   */  public static String getLocalMacAddressFromBusyBox(){    String result = "";    String Mac = "";    result = callCmd("busyBox ifconfig","HWaddr");    //如果返回的result == null,则说明网络不可取    if(result==null){      return "网络异常";    }    //对该行数据进行解析    //例如:eth0   link encap:Ethernet HWaddr 00:16:E8:3E:DF:67    if(result.length()>0 && result.contains("HWaddr")==true){      Mac = result.substring(result.indexOf("HWaddr")+6,result.length()-1);      result = Mac;    }    return result;  }  private static String callCmd(String cmd,String filter) {    String result = "";    String line = "";    try {      Process proc = Runtime.getRuntime().exec(cmd);      inputStreamReader is = new inputStreamReader(proc.getinputStream());      BufferedReader br = new BufferedReader (is);      while ((line = br.readline ()) != null && line.contains(filter)== false) {        result += line;      }      result = line;    }    catch(Exception e) {      e.printstacktrace();    }    return result;  }

 对比效果截图

上述三种方法,对比我开发的sdk现在使用的方法以及通过wifimanager获取mac地址的方法,效果如下(7.0设备有限,只覆盖部分机型):

结论

通过上述对比,通过ip获取mac地址和扫描网络接口获取mac结合使用,可以达到准确的效果。

通过ip获取的mac地址优先级高,只有在它获取不到的情况下,再使用扫描网络接口获取的mac地址。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的详解Android MacAddress 适配心得全部内容,希望文章能够帮你解决详解Android MacAddress 适配心得所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144726.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存