Android 6.0及更高版本的Mac地址显示为null

时间:2017-01-27 12:40:03

标签: android mac-address android-6.0.1-marshmallow

我正在使用此代码获取MAC ADDRESS并在我的应用中显示它。该代码适用于除大多数最新设备和ANDROID BOX之外的所有设备。

它为ANDROID BOX和其他最新设备显示为null。

这是代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<section class="content col-lg-10 col-md-12 col-xs-12 col-lg-offset-1">
           
                    <section class="employees">
                        <div class="row">
                          <div class="box-container col-lg-3 col-md-3 col-sm-3 col-xs-12">
                            <div class="employee-box">
                              <img src="assets/img/employees/avatar-employee.png" alt="">
                              <h3>Raúl García Boleda</h3>
                              <h4 class="position">Asset Manager</h4>
                              <h5 class="area">Recovery</h5>
                              <h5 class="area">Recovery</h5>
                              <h5 class="area">Recovery</h5>
                              <div class="extra-info">
                                <h3>Teléfonos</h3>
                                <h5 class="area">M. corto: 20 453 7621</h5>
                                <h5 class="area">Móvil: 20 453 7620</h5>
                                <h5 class="area">Ext.: 20 750 9441</h5>
                                <h5 class="area">Fijo: 20 750 9442</h5>
                                <h3>Email</h3>
                                <h5>cgomez@anticipa.com</h5>
                                <h3>Ubicación</h3>
                                <h5 class="area">Planta 2, Área 5</h5>
                              </div>                          
                              <span class="dropdown">más información</span>
                            </div>
                          </div>
                          <div class="box-container col-lg-3 col-md-3 col-sm-3 col-xs-12">
                            <div class="employee-box">
                              <img src="assets/img/employees/avatar-employee.png" alt="">
                              <h3>Raúl García Boleda Raúl García Boleda Raúl García Boleda Raúl García Boleda Raúl García Boleda Raúl García BoledaRaúl García Boleda Raúl García Boleda Raúl García BoledaRaúl García Boleda Raúl García Boleda Raúl García Boleda</h3>
                              <h4 class="position">Asset Manager</h4>
                              <h5 class="area">Recovery</h5>
                              <div class="extra-info">
                                <h3>Teléfonos</h3>
                                <h5 class="area">M. corto: 20 453 7621</h5>
                                <h5 class="area">Móvil: 20 453 7620</h5>
                                <h5 class="area">Ext.: 20 750 9441</h5>
                                <h5 class="area">Fijo: 20 750 9442</h5>
                                <h3>Email</h3>
                                cgomez@anticipa.com
                                <h3>Ubicación</h3>
                                <h5 class="area">Planta 2, Área 5</h5>
                              </div>                          
                              <span class="dropdown">más información</span>
                            </div>
                          </div>
                          <div class="box-container col-lg-3 col-md-3 col-sm-3 col-xs-12">
                            <div class="employee-box">
                              <img src="assets/img/employees/avatar-employee.png" alt="">
                              <h3>Raúl García Boleda</h3>
                              <h4 class="position">Asset Manager</h4>
                              <h5 class="area">Recovery</h5>
                              <h5 class="area">Recovery</h5>
                              <h5 class="area">Recovery</h5>
                              <div class="extra-info">
                                <h3>Teléfonos</h3>
                                <h5 class="area">M. corto: 20 453 7621</h5>
                                <h5 class="area">Móvil: 20 453 7620</h5>
                                <h5 class="area">Ext.: 20 750 9441</h5>
                                <h5 class="area">Fijo: 20 750 9442</h5>
                                <h3>Email</h3>
                                cgomez@anticipa.com
                                <h3>Ubicación</h3>
                                <h5 class="area">Planta 2, Área 5</h5>
                              </div>                          
                              <span class="dropdown">más información</span>
                            </div>
                          </div>
                          <div class="box-container col-lg-3 col-md-3 col-sm-3 col-xs-12">
                            <div class="employee-box">
                              <img src="assets/img/employees/avatar-employee.png" alt="">
                              <h3>Raúl García Boleda</h3>
                              <h4 class="position">Asset Manager</h4>
                              <h5 class="area">Recovery</h5>
                              <div class="extra-info">
                                <h3>Teléfonos</h3>
                                <h5 class="area">M. corto: 20 453 7621</h5>
                                <h5 class="area">Móvil: 20 453 7620</h5>
                                <h5 class="area">Ext.: 20 750 9441</h5>
                                <h5 class="area">Fijo: 20 750 9442</h5>
                                <h3>Email</h3>
                                cgomez@anticipa.com
                                <h3>Ubicación</h3>
                                <h5 class="area">Planta 2, Área 5</h5>
                              </div>                          
                              <span class="dropdown">más información</span>
                            </div>
                          </div>         
                        </div>    
                    </section>
                    <section class="employees">
                      <div class="row">                      
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                          <div class="employee-box">
                            <img src="assets/img/employees/avatar-employee.png" alt="">
                            <h3>Raúl García Boleda</h3>
                            <h4 class="position">Asset Manager</h4>
                            <h5 class="area">Recovery</h5>
                            <div class="extra-info">
                              <h3>Teléfonos</h3>
                              <h5 class="area">M. corto: 20 453 7621</h5>
                              <h5 class="area">Móvil: 20 453 7620</h5>
                              <h5 class="area">Ext.: 20 750 9441</h5>
                              <h5 class="area">Fijo: 20 750 9442</h5>
                              <h3>Email</h3>
                              cgomez@anticipa.com
                              <h3>Ubicación</h3>
                              <h5 class="area">Planta 2, Área 5</h5>
                            </div>                          
                            <span class="dropdown">más información</span>
                          </div>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                          <div class="employee-box">
                            <img src="assets/img/employees/avatar-employee.png" alt="">
                            <h3>Raúl García Boleda</h3>
                            <h4 class="position">Asset Manager</h4>
                            <h4 class="position">Asset Manager</h4>
                            <h4 class="position">Asset Manager</h4>
                            <h5 class="area">Recovery</h5>
                            <div class="extra-info">
                              <h3>Teléfonos</h3>
                              <h5 class="area">M. corto: 20 453 7621</h5>
                              <h5 class="area">Móvil: 20 453 7620</h5>
                              <h5 class="area">Ext.: 20 750 9441</h5>
                              <h5 class="area">Fijo: 20 750 9442</h5>
                              <h3>Email</h3>
                              cgomez@anticipa.com
                              <h3>Ubicación</h3>
                              <h5 class="area">Planta 2, Área 5</h5>
                            </div>                          
                            <span class="dropdown">más información</span>
                          </div>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                          <div class="employee-box">
                            <img src="assets/img/employees/avatar-employee.png" alt="">
                            <h3>Raúl García Boleda</h3>
                            <h4 class="position">Asset Manager</h4>
                            <h5 class="area">Recovery</h5>
                            <div class="extra-info">
                              <h3>Teléfonos</h3>
                              <h5 class="area">M. corto: 20 453 7621</h5>
                              <h5 class="area">Móvil: 20 453 7620</h5>
                              <h5 class="area">Ext.: 20 750 9441</h5>
                              <h5 class="area">Fijo: 20 750 9442</h5>
                              <h3>Email</h3>
                              cgomez@anticipa.com
                              <h3>Ubicación</h3>
                              <h5 class="area">Planta 2, Área 5</h5>
                            </div>                          
                            <span class="dropdown">más información</span>
                          </div>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                          <div class="employee-box">
                            <img src="assets/img/employees/avatar-employee.png" alt="">
                            <h3>Raúl García Boleda</h3>
                            <h4 class="position">Asset Manager</h4>
                            <h5 class="area">Recovery</h5>
                            <div class="extra-info">
                              <h3>Teléfonos</h3>
                              <h5 class="area">M. corto: 20 453 7621</h5>
                              <h5 class="area">Móvil: 20 453 7620</h5>
                              <h5 class="area">Ext.: 20 750 9441</h5>
                              <h5 class="area">Fijo: 20 750 9442</h5>
                              <h3>Email</h3>
                              cgomez@anticipa.com
                              <h3>Ubicación</h3>
                              <h5 class="area">Planta 2, Área 5</h5>
                            </div>                          
                            <span class="dropdown">más información</span>
                          </div>
                        </div>
                      </div>                                    
                    </section>                                                                                                
                </section>

我在清单文件中写了这些权限     

   public static String getWifiMacAddress() {
    try {
        String interfaceName = "wlan0";
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (!intf.getName().equalsIgnoreCase(interfaceName)){
                continue;
            }

            byte[] mac = intf.getHardwareAddress();
            if (mac==null){
                return "";
            }

            StringBuilder buf = new StringBuilder();
            for (byte aMac : mac) {
                buf.append(String.format("%02X:", aMac));
            }
            if (buf.length()>0) {
                buf.deleteCharAt(buf.length() - 1);
            }
            return buf.toString();
        }
    } catch (Exception ex) { } // for now eat exceptions
    return "";
}

3 个答案:

答案 0 :(得分:0)

首先,你会检查是否批准了许可?

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
 String macAddress = wInfo.getMacAddress();

此外,在清单文件中添加以下权限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Please refer this link for 6.0 marshmalow

答案 1 :(得分:0)

首先,您需要在AndroidManifest.xml中添加Internet用户权限。

Egg Tomato Salad Coffee Melon Tomato Milk Noodle Salmon

,然后参考此处获取mac地址http://robinhenniges.com/en/android6-get-mac-address-programmatically

如果不起作用,请参阅此 Android 6.0 changes 从此我得出结论,

  

为用户提供更好的数据保护,从此开始   发布时,Android会删除对设备本地的编程访问权限   使用Wi-Fi和蓝牙API的应用的硬件标识符。该   WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法   现在返回一个恒定值02:00:00:00:00:00。

     

通过访问附近外部设备的硬件标识符   蓝牙和Wi-Fi扫描,您的应用程序现在必须具有   ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION权限。   注意 即使拥有这些权限,也无法获得自己的MAC。请仔细阅读,据说您可以获得具有这些权限的其他设备MAC,但不是您自己的

答案 2 :(得分:-1)

与6.0及以上版本一样,仅在Manifest中添加权限不会起作用。您应该拥有运行时权限,如果未授予,则授予它。

检查此链接:

https://stackoverflow.com/a/30549756/3910281