getMacAddress() 返回 null

getMacAddress() is returning null

这是我使用的代码片段

public static String getMACAddressOfDevice(Context context){
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        String macAddress = wifiInfo.getMacAddress();
        return macAddress;
    }

有时 returns null.I 不明白为什么! 谁能给我解释一下?

wifi关闭时,部分android设备returns无效。

它在当前设计中。

如果设备没有 wifi 硬件,则此方法无效。

还有一些其他的设备唯一标识方法

IMEI:

TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
String m_deviceId = TelephonyMgr.getDeviceId();

缺点:

依赖SIM卡所以

  • 没有sim卡就完蛋了

  • 如果有双卡就完蛋了

蓝牙地址:

BluetoothAdapter m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
   String m_bluetoothAdd = m_BluetoothAdapter.getAddress();

缺点:

  • 如果没有蓝牙硬件,我们就完蛋了。
  • 将来在某些新设备中,如果它关闭,我们可能无法读取它。

序列号:

我们可以读取硬件串口number.It可以在设备设置中看到

 Build.SERIAL

缺点:

  • 如果设备没有电话,这不可能used.There有些wifi-only设备有售。

Android_ID:

您可以尝试使用 ANDROID_ID 它有利于设备的唯一标识,因为它不依赖于任何硬件 IMEI

String m_androidId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);

缺点:

  • 如果 OS 版本升级那么它可能会改变
  • 如果设备已获得 root 权限,它会发生变化
  • 不能保证 device_id 是唯一的有一些报告说一些制造商有重复的 device_id

建议:

最安全的选择是使用Android_ID,因为它不依赖于任何硬件的可用性

Reference