ADVERTISEMENT
ADVERTISEMENT

Kotlin program to get system MAC address

Kotlin | Getting System MAC Address: Here, we are going to learn how to find the system MAC address in Kotlin programming language?
Submitted by IncludeHelp, on May 05, 2020

The task is to get system MAC address.

package com.includehelp

import java.net.InetAddress
import java.net.NetworkInterface

//Function to get System MAC
fun getSystemMac(): String? {
    return try {
        val OSName = System.getProperty("os.name")     //Get Operating System Name
        if (OSName.contains("Windows")) {
            getMAC4Windows()                           // Call if OS is Windows
        } else {
            var mac = getMAC4Linux("eth0")
            if (mac == null) {
                mac = getMAC4Linux("eth1")
                if (mac == null) {
                    mac = getMAC4Linux("eth2")
                    if (mac == null) {
                        mac = getMAC4Linux("usb0")
                    }
                }
            }
            mac
        }
    } catch (E: Exception) {
        System.err.println("System Mac Exp : " + E.message)
        null
    }
}

/**
 * Method for get MAc of Linux Machine
 */
fun getMAC4Linux(name: String): String? {
    return try {
        val network = NetworkInterface.getByName(name)
        val mac = network.hardwareAddress
        val sb = StringBuilder()
        for (i in mac.indices) {
            sb.append(String.format("%02X%s", mac[i], if (i < mac.size - 1) "-" else ""))
        }
        sb.toString()
    } catch (E: Exception) {
        System.err.println("System Linux MAC Exp : " + E.message)
        null
    }
}

/**
 * Method for get Mac Address of Windows Machine
*/
fun getMAC4Windows(): String? {
    return try {
        val addr = InetAddress.getLocalHost()
        val network = NetworkInterface.getByInetAddress(addr)
        val mac = network.hardwareAddress
        val sb = StringBuilder()
        for (i in mac.indices) {
            sb.append(String.format("%02X%s", mac[i], if (i < mac.size - 1) "-" else ""))
        }
        sb.toString()
    } catch (E: Exception) {
        System.err.println("System Windows MAC Exp : " + E.message)
        null
    }
}


//Main Function, Entry Point of Program
fun main(args: Array<String>) {

    //Call Function to get MAC Address
    val macAddress = getSystemMac()

    //Print MAC Address
    println("System Mac Address : $macAddress")
}

Output

System Mac Address : 02-12-FE-11-00-05
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.