Reputation: 721
This is the code snippet that I use
public static String getMACAddressOfDevice(Context context){
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String macAddress = wifiInfo.getMacAddress();
return macAddress;
}
sometime it returns null.I don't understand why ! Can anybody explain me ?
Upvotes: 3
Views: 2855
Reputation: 32270
When wifi is switched off , some android device returns null.
It's in current design.
If the device doesn't have wifi hardware , then this approach doesn't work.
There are some other alternatives for unique identification of devices
IMEI:
TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String m_deviceId = TelephonyMgr.getDeviceId();
Drawbacks:
It is sim card dependent so
If there is no sim card then we're doomed
If there is dual sim then we're dommed
Bluetooth address:
BluetoothAdapter m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String m_bluetoothAdd = m_BluetoothAdapter.getAddress();
Drawbacks:
Serial:
We can read the hardware serial number.It can be seen in the device settings.
Build.SERIAL
Drawbacks:
Android_ID:
You can try using the ANDROID_ID its good for unique identification of the devices since its not dependent on any hardware IMEI
String m_androidId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
Drawbacks:
Suggestion:
The safest bet is to use Android_ID because it's not dependent on availability of any hardware
Upvotes: 2