飞道的博客

安卓rk主板获取MAC地址(包括以太网)

399人阅读  评论(0)

此类封装普通网络mac地址以及以太网mac地址的获取,还有一个网络变化的广播,看你们所需:


  
  1. package com.airiche.httplibrary.http.rk3288Temp.utils.manager
  2. import android.content.BroadcastReceiver
  3. import android.content.Context
  4. import android.content.Intent
  5. import android.content.IntentFilter
  6. import android.net.wifi.WifiManager
  7. import android.util.Log
  8. import com.airiche.httplibrary.http.rk3288Temp.utils.Listen.WifiEnListen
  9. import java.io.BufferedReader
  10. import java.io.FileReader
  11. import java.io.IOException
  12. import java.net.Inet6Address
  13. import java.net.InetAddress
  14. import java.net.NetworkInterface
  15. import java.util.*
  16. import kotlin.collections.ArrayList
  17. class WiFiEnManger {
  18. var connectFilter: IntentFilter? = null
  19. var EthernetBroadcastReceiver: BroadcastReceiver? = null
  20. fun getWiFiEthernet(context: Context,vWiFiEnlisten: WifiEnListen) {
  21. mWiFiEnlisten = vWiFiEnlisten
  22. mContext = context
  23. EthernetBroadcastReceiver = object : BroadcastReceiver() {
  24. override fun onReceive(context: Context?, intent: Intent?) {
  25. val EtherState = intent!!.getIntExtra( "ethernet_state", - 1)
  26. Log.e(
  27. "TAG",
  28. "EtherState=============================================:" + EtherState
  29. )
  30. Enumeration(EtherState)
  31. }
  32. }
  33. connectFilter = IntentFilter()
  34. connectFilter!!.addAction( "android.net.ethernet.ETHERNET_STATE_CHANGED")
  35. context!!.registerReceiver(EthernetBroadcastReceiver, connectFilter)
  36. }
  37. //获取以太网的mac地址
  38. fun getEthernetMac(): String {
  39. var reader: BufferedReader? = null
  40. var ethernetMac: String? = ""
  41. try {
  42. reader = BufferedReader(
  43. FileReader(
  44. "sys/class/net/eth0/address"
  45. )
  46. )
  47. ethernetMac = reader.readLine()
  48. Log.v( "TAG", "ethernetMac: $ethernetMac")
  49. } catch (e: java.lang.Exception) {
  50. ethernetMac = "00:00:00:00:00:00"
  51. Log.e( "TAG", "open sys/class/net/eth0/address failed : $e")
  52. } finally {
  53. try {
  54. reader?.close()
  55. } catch (e: IOException) {
  56. Log.e( "TAG", "close sys/class/net/eth0/address failed : $e")
  57. }
  58. }
  59. return ethernetMac.toString()
  60. }
  61. //获取本地wifi mac地址
  62. fun getMac(context: Context): String? {
  63. var mac: String? = null
  64. if (mac == null) {
  65. try {
  66. val networkInterface = NetworkInterface.getByName( "wlan0")
  67. val addrByte = networkInterface.hardwareAddress
  68. val sb = StringBuilder()
  69. for (b in addrByte) {
  70. sb.append(String.format( "%02X:", b))
  71. }
  72. if (sb.length > 0) {
  73. sb.deleteCharAt(sb.length - 1)
  74. }
  75. mac = sb.toString()
  76. } catch (e: Exception) {
  77. e.printStackTrace()
  78. val wifiM =
  79. context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
  80. try {
  81. val wifiI = wifiM.connectionInfo
  82. mac = wifiI.macAddress
  83. } catch (e1: NullPointerException) {
  84. e1.printStackTrace()
  85. mac = "02:00:00:00:00:00"
  86. }
  87. }
  88. }
  89. Log.e(TAG, "MAC:" + mac!!)
  90. return mac
  91. }
  92. private fun Enumeration(EtherState: Int) {
  93. var hostIp = ArrayList<String>()
  94. hostIp.clear()
  95. try {
  96. val nis: Enumeration<NetworkInterface> = NetworkInterface.getNetworkInterfaces()
  97. var ia: InetAddress? = null
  98. while (nis.hasMoreElements()) {
  99. val ni = nis.nextElement() as NetworkInterface
  100. val ias: Enumeration<InetAddress> = ni.inetAddresses
  101. while (ias.hasMoreElements()) {
  102. ia = ias.nextElement()
  103. if (ia is Inet6Address) {
  104. continue // skip ipv6
  105. }
  106. val ip: String = ia.getHostAddress()
  107. if ( "127.0.0.1" != ip) {
  108. hostIp!!.add(ip)
  109. break
  110. }
  111. }
  112. if (EtherState == 2) {
  113. //以太网已经连接
  114. if (hostIp.size == 1) {
  115. //只有一个的话那么就只有以太网连接
  116. mWiFiEnlisten!!.setEthernetListen(getEthernetMac(), hostIp[ 0])
  117. mWiFiEnlisten!!.setWiFiListen( "00:00:00:00:00", "0.0.0.0")
  118. } else {
  119. //连接了wifi又连接了以太网
  120. mWiFiEnlisten!!.setEthernetListen(getEthernetMac(), hostIp[ 0])
  121. getMac(mContext!!)?.let { mWiFiEnlisten!!.setWiFiListen(it, hostIp[ 1]) }
  122. }
  123. } else {
  124. if (hostIp.size == 1) {
  125. //只有wifi
  126. mWiFiEnlisten!!.setEthernetListen( "00:00:00:00:00", "0.0.0.0")
  127. mWiFiEnlisten!!.setWiFiListen(getMac(mContext!!)!!, hostIp[ 0])
  128. } else {
  129. //此处只有为空的时候了
  130. mWiFiEnlisten!!.setEthernetListen( "00:00:00:00:00", "0.0.0.0")
  131. mWiFiEnlisten!!.setWiFiListen( "00:00:00:00:00", "0.0.0.0")
  132. }
  133. }
  134. }
  135. } catch (e: Exception) {
  136. Log.i( "TAG", "SocketException")
  137. }
  138. }
  139. companion object {
  140. var mWiFiEnManger: WiFiEnManger? = WiFiEnManger()
  141. val instance: WiFiEnManger?
  142. get() {
  143. if (mWiFiEnManger == null) {
  144. mWiFiEnManger = WiFiEnManger()
  145. }
  146. return mWiFiEnManger
  147. }
  148. var mWiFiEnlisten: WifiEnListen? = null
  149. var TAG = "WiFiEnManger"
  150. var mContext: Context? = null
  151. }
  152. }

 


转载:https://blog.csdn.net/qq_36333309/article/details/112230732
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场