當(dāng)前位置:
首頁 >
kotlin获取属性_Kotlin程序获取系统MAC地址
發(fā)布時(shí)間:2023/12/1
41
豆豆
生活随笔
收集整理的這篇文章主要介紹了
kotlin获取属性_Kotlin程序获取系统MAC地址
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
kotlin獲取屬性
The task is to get system MAC address.
任務(wù)是獲取系統(tǒng)MAC地址。
package com.includehelpimport 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 Nameif (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.hardwareAddressval 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.hardwareAddressval 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 Addressval macAddress = getSystemMac()//Print MAC Addressprintln("System Mac Address : $macAddress") }Output
輸出量
System Mac Address : 02-12-FE-11-00-05翻譯自: https://www.includehelp.com/kotlin/get-system-mac-address.aspx
kotlin獲取屬性
總結(jié)
以上是生活随笔為你收集整理的kotlin获取属性_Kotlin程序获取系统MAC地址的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 摩尔庄园水壶怎么升级
- 下一篇: 操作系统大内核和微内核_操作系统中的内核