i am working on Android with Java (Android studio) to create Android app so i want to get Mac Address. so my Question is how i can find Mac address? Mac address will be Unique for each User. User will download the app then he should display the mac address so he can use same mac address to activate the device from website. so i just want to get MAC Address and it will be unique for each user.
CodePudding user response:
Step 1: Add the permissions. In Manifest.xml
file add below lines
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Step 2: Create the method to get the MAC address
public String getMacAddress(){
try{
List<NetworkInterface> networkInterfaceList = Collections.list(NetworkInterface.getNetworkInterfaces());
String stringMac = "";
for(NetworkInterface networkInterface : networkInterfaceList){
if(networkInterface.getName().equalsIgnoreCase("wlon0"));
{
for(int i=0;i <networkInterface.getHardwareAddress().length; i ){
String stringMacByte = Integer.toHexString(networkInterface.getHardwareAddress()[i]& 0xFF);
if(stringMacByte.length()==1){
stringMacByte = "0" stringMacByte;
}
stringMac = stringMacByte.toUpperCase() ":";
} break;
}
}
return stringMac;
}catch (SocketException e)
{
e.printStackTrace();
}
return "0";
}
Step 3: Call the method to get the MAC Address
String mobile_mac_addres = getMacAddress(); //call the method that return mac address
Log.d("MyMacIS",mobile_mac_address); // this prints the MAC Address
CodePudding user response:
use this code programatically get macAddress
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress();