此类封装普通网络mac地址以及以太网mac地址的获取,还有一个网络变化的广播,看你们所需:
-
package com.airiche.httplibrary.http.rk3288Temp.utils.manager
-
-
import android.content.BroadcastReceiver
-
import android.content.Context
-
import android.content.Intent
-
import android.content.IntentFilter
-
import android.net.wifi.WifiManager
-
import android.util.Log
-
import com.airiche.httplibrary.http.rk3288Temp.utils.Listen.WifiEnListen
-
import java.io.BufferedReader
-
import java.io.FileReader
-
import java.io.IOException
-
import java.net.Inet6Address
-
import java.net.InetAddress
-
import java.net.NetworkInterface
-
import java.util.*
-
import kotlin.collections.ArrayList
-
-
-
class WiFiEnManger {
-
-
var connectFilter: IntentFilter? =
null
-
var EthernetBroadcastReceiver: BroadcastReceiver? =
null
-
-
fun getWiFiEthernet(context: Context,vWiFiEnlisten: WifiEnListen) {
-
mWiFiEnlisten = vWiFiEnlisten
-
mContext = context
-
EthernetBroadcastReceiver =
object : BroadcastReceiver() {
-
override
fun onReceive(context: Context?, intent: Intent?) {
-
val EtherState = intent!!.getIntExtra(
"ethernet_state", -
1)
-
Log.e(
-
"TAG",
-
"EtherState=============================================:" + EtherState
-
)
-
Enumeration(EtherState)
-
}
-
}
-
connectFilter = IntentFilter()
-
connectFilter!!.addAction(
"android.net.ethernet.ETHERNET_STATE_CHANGED")
-
context!!.registerReceiver(EthernetBroadcastReceiver, connectFilter)
-
-
}
-
-
//获取以太网的mac地址
-
fun getEthernetMac(): String {
-
var reader: BufferedReader? =
null
-
var ethernetMac: String? =
""
-
try {
-
reader = BufferedReader(
-
FileReader(
-
"sys/class/net/eth0/address"
-
)
-
)
-
ethernetMac = reader.readLine()
-
Log.v(
"TAG",
"ethernetMac: $ethernetMac")
-
-
}
catch (e: java.lang.Exception) {
-
ethernetMac =
"00:00:00:00:00:00"
-
Log.e(
"TAG",
"open sys/class/net/eth0/address failed : $e")
-
}
finally {
-
try {
-
reader?.close()
-
}
catch (e: IOException) {
-
Log.e(
"TAG",
"close sys/class/net/eth0/address failed : $e")
-
}
-
}
-
return ethernetMac.toString()
-
}
-
-
-
//获取本地wifi mac地址
-
fun getMac(context: Context): String? {
-
var mac: String? =
null
-
if (mac ==
null) {
-
try {
-
val networkInterface = NetworkInterface.getByName(
"wlan0")
-
val addrByte = networkInterface.hardwareAddress
-
val sb = StringBuilder()
-
for (b
in addrByte) {
-
sb.append(String.format(
"%02X:", b))
-
}
-
if (sb.length >
0) {
-
sb.deleteCharAt(sb.length -
1)
-
}
-
mac = sb.toString()
-
}
catch (e: Exception) {
-
e.printStackTrace()
-
val wifiM =
-
context.applicationContext.getSystemService(Context.WIFI_SERVICE)
as WifiManager
-
try {
-
val wifiI = wifiM.connectionInfo
-
mac = wifiI.macAddress
-
}
catch (e1: NullPointerException) {
-
e1.printStackTrace()
-
mac =
"02:00:00:00:00:00"
-
}
-
-
}
-
-
}
-
Log.e(TAG,
"MAC:" + mac!!)
-
return mac
-
}
-
-
private
fun Enumeration(EtherState: Int) {
-
var hostIp = ArrayList<String>()
-
hostIp.clear()
-
try {
-
val nis: Enumeration<NetworkInterface> = NetworkInterface.getNetworkInterfaces()
-
var ia: InetAddress? =
null
-
while (nis.hasMoreElements()) {
-
val ni = nis.nextElement()
as NetworkInterface
-
val ias: Enumeration<InetAddress> = ni.inetAddresses
-
-
while (ias.hasMoreElements()) {
-
ia = ias.nextElement()
-
if (ia
is Inet6Address) {
-
continue
// skip ipv6
-
}
-
val ip: String = ia.getHostAddress()
-
if (
"127.0.0.1" != ip) {
-
hostIp!!.add(ip)
-
break
-
}
-
}
-
-
if (EtherState ==
2) {
-
//以太网已经连接
-
if (hostIp.size ==
1) {
-
//只有一个的话那么就只有以太网连接
-
mWiFiEnlisten!!.setEthernetListen(getEthernetMac(), hostIp[
0])
-
mWiFiEnlisten!!.setWiFiListen(
"00:00:00:00:00",
"0.0.0.0")
-
}
else {
-
//连接了wifi又连接了以太网
-
mWiFiEnlisten!!.setEthernetListen(getEthernetMac(), hostIp[
0])
-
getMac(mContext!!)?.let { mWiFiEnlisten!!.setWiFiListen(it, hostIp[
1]) }
-
}
-
}
else {
-
if (hostIp.size ==
1) {
-
//只有wifi
-
mWiFiEnlisten!!.setEthernetListen(
"00:00:00:00:00",
"0.0.0.0")
-
mWiFiEnlisten!!.setWiFiListen(getMac(mContext!!)!!, hostIp[
0])
-
}
else {
-
//此处只有为空的时候了
-
mWiFiEnlisten!!.setEthernetListen(
"00:00:00:00:00",
"0.0.0.0")
-
mWiFiEnlisten!!.setWiFiListen(
"00:00:00:00:00",
"0.0.0.0")
-
}
-
}
-
-
}
-
}
catch (e: Exception) {
-
Log.i(
"TAG",
"SocketException")
-
}
-
}
-
-
companion
object {
-
var mWiFiEnManger: WiFiEnManger? = WiFiEnManger()
-
val instance: WiFiEnManger?
-
get() {
-
if (mWiFiEnManger ==
null) {
-
mWiFiEnManger = WiFiEnManger()
-
}
-
return mWiFiEnManger
-
}
-
var mWiFiEnlisten: WifiEnListen? =
null
-
-
var TAG =
"WiFiEnManger"
-
var mContext: Context? =
null
-
}
-
}
转载:https://blog.csdn.net/qq_36333309/article/details/112230732
查看评论