2017-09-15 205 views
0

我想使用自己的設備的無線MAC地址,並張貼到服務器,我用下面的方法:獲取我設備的wifi mac地址?

public void accessAdress(){ 
    //Detect Bluetooth Address 
    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); 
    WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 
    String address = wifiInfo.getMacAddress(); 
    Toast.makeText(this, ""+address, Toast.LENGTH_SHORT).show(); 

} 

但它給了我這樣的:02:00:00:00:00: 00。 我應該怎麼做才能訪問我真正的wifi mac地址? (我的設備的Android版本:7.0)

回答

0

的MAC地址進行模糊處理由於Android 6.0的

爲用戶提供更高的數據保護的用戶,在這個版本開始,Android的刪除該程序訪問設備使用Wi-Fi和藍牙API的應用程序本地硬件標識符。 WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法現在返回恆定值02:00:00:00:00:00。

要通過藍牙和Wi-Fi掃描訪問附近外部設備的硬件標識符,您的應用必須具有ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION權限。

請參閱documentation

人們已經用這種work around

public static String getMacAddr() { 
    try { 
     List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces()); 
     for (NetworkInterface nif : all) { 
      if (!nif.getName().equalsIgnoreCase("wlan0")) continue; 

      byte[] macBytes = nif.getHardwareAddress(); 
      if (macBytes == null) { 
       return ""; 
      } 

      StringBuilder res1 = new StringBuilder(); 
      for (byte b : macBytes) { 
       res1.append(Integer.toHexString(b & 0xFF) + ":"); 
      } 

      if (res1.length() > 0) { 
       res1.deleteCharAt(res1.length() - 1); 
      } 
      return res1.toString(); 
     } 
    } catch (Exception ex) { 
     //handle exception 
    } 
    return ""; 
} 
+0

這不是爲我工作,你知道如何獲得藍牙地址,而不是無線MAC地址? – amirofff