在 Xamarin 中获取 Marshmallow 上的 Mac 地址
Getting Mac Address on Marshmallow In Xamarin
我已经尝试了几种方法来在 Android 6.0 上获得 MAC 但没有成功 ):
我已经拥有此权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
这是我的代码:
public string GetMacAdress(Context context)
{
string mac = GetMacAddressLegacy(context);
if (mac == "02:00:00:00:00:00")
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface nif in interfaces)
{
if (!nif.Name.ToLower().Contains("wlan")) continue;
var physicalAddress = nif.GetPhysicalAddress();
byte[] macBytes = physicalAddress.GetAddressBytes();
if (macBytes == null) continue;
string macString = BitConverter.ToString(macBytes);
if (!string.IsNullOrWhiteSpace(macString)) mac = macString.Trim().ToUpper().Replace("-", ":");
}
}
return mac;
}
[Obsolete]
public string GetMacAddressLegacy(Context context)
{
string toReturn = "02:00:00:00:00:00";
if (DetectWifiNetwork())
{
isConected = true;
var telephonyMgr = (WifiManager)context.GetSystemService(Context.WifiService);
toReturn = telephonyMgr.ConnectionInfo.MacAddress;
if (!string.IsNullOrWhiteSpace(toReturn)) toReturn = toReturn.Trim().ToUpper();
}
else
{
isConected = false;
}
return toReturn;
}
但是这一行:byte[] macBytes = physicalAddress.GetAddressBytes();
returns 一个空数组。
谁能解决这个问题?
好吧,使用它就足以让它工作了:
权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
代码:
WifiManager wManager = (WifiManager)GetSystemService(Context.WifiService);
WifiInfo wInfo = wManager.ConnectionInfo;
button.Text = wInfo.MacAddress;
您是在真实设备上还是在模拟器上进行测试?
尝试使用此代码:
public string GetMacAdress(Context context)
{
string mac = GetMacAddressLegacy(context);
if (mac == "02:00:00:00:00:00")
{
var interfaces = Java.Net.NetworkInterface.NetworkInterfaces;
foreach (var nif in interfaces)
{
if (!nif.Name.ToLower().Contains("wlan")) continue;
byte[] macBytes = nif.GetHardwareAddress();
string macString = BitConverter.ToString(macBytes);
if (!string.IsNullOrWhiteSpace(macString))
mac = macString.Trim().ToUpper().Replace("-", ":");
}
}
return mac;
}
尝试使用这个方法
public static string getMacAddress()
{
string macAddress = string.Empty;
var all = Collections.List(Java.Net.NetworkInterface.NetworkInterfaces);
foreach (var interfaces in all)
{
if (!(interfaces as Java.Net.NetworkInterface).Name.Contains("wlan0")) continue;
var macBytes = (interfaces as
Java.Net.NetworkInterface).GetHardwareAddress();
if (macBytes == null) continue;
var sb = new System.Text.StringBuilder();
foreach (var b in macBytes)
{
string convertedByte = string.Empty;
convertedByte = (b & 0xFF).ToString("X2") + ":";
if(convertedByte.Length == 1)
{
convertedByte.Insert(0, "0");
}
sb.Append(convertedByte);
}
macAddress = sb.ToString().Remove(sb.Length - 1);
return macAddress;
}
return "02:00:00:00:00:00";
}
我已经尝试了几种方法来在 Android 6.0 上获得 MAC 但没有成功 ):
我已经拥有此权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
这是我的代码:
public string GetMacAdress(Context context)
{
string mac = GetMacAddressLegacy(context);
if (mac == "02:00:00:00:00:00")
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface nif in interfaces)
{
if (!nif.Name.ToLower().Contains("wlan")) continue;
var physicalAddress = nif.GetPhysicalAddress();
byte[] macBytes = physicalAddress.GetAddressBytes();
if (macBytes == null) continue;
string macString = BitConverter.ToString(macBytes);
if (!string.IsNullOrWhiteSpace(macString)) mac = macString.Trim().ToUpper().Replace("-", ":");
}
}
return mac;
}
[Obsolete]
public string GetMacAddressLegacy(Context context)
{
string toReturn = "02:00:00:00:00:00";
if (DetectWifiNetwork())
{
isConected = true;
var telephonyMgr = (WifiManager)context.GetSystemService(Context.WifiService);
toReturn = telephonyMgr.ConnectionInfo.MacAddress;
if (!string.IsNullOrWhiteSpace(toReturn)) toReturn = toReturn.Trim().ToUpper();
}
else
{
isConected = false;
}
return toReturn;
}
但是这一行:byte[] macBytes = physicalAddress.GetAddressBytes();
returns 一个空数组。
谁能解决这个问题?
好吧,使用它就足以让它工作了:
权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
代码:
WifiManager wManager = (WifiManager)GetSystemService(Context.WifiService);
WifiInfo wInfo = wManager.ConnectionInfo;
button.Text = wInfo.MacAddress;
您是在真实设备上还是在模拟器上进行测试?
尝试使用此代码:
public string GetMacAdress(Context context)
{
string mac = GetMacAddressLegacy(context);
if (mac == "02:00:00:00:00:00")
{
var interfaces = Java.Net.NetworkInterface.NetworkInterfaces;
foreach (var nif in interfaces)
{
if (!nif.Name.ToLower().Contains("wlan")) continue;
byte[] macBytes = nif.GetHardwareAddress();
string macString = BitConverter.ToString(macBytes);
if (!string.IsNullOrWhiteSpace(macString))
mac = macString.Trim().ToUpper().Replace("-", ":");
}
}
return mac;
}
尝试使用这个方法
public static string getMacAddress()
{
string macAddress = string.Empty;
var all = Collections.List(Java.Net.NetworkInterface.NetworkInterfaces);
foreach (var interfaces in all)
{
if (!(interfaces as Java.Net.NetworkInterface).Name.Contains("wlan0")) continue;
var macBytes = (interfaces as
Java.Net.NetworkInterface).GetHardwareAddress();
if (macBytes == null) continue;
var sb = new System.Text.StringBuilder();
foreach (var b in macBytes)
{
string convertedByte = string.Empty;
convertedByte = (b & 0xFF).ToString("X2") + ":";
if(convertedByte.Length == 1)
{
convertedByte.Insert(0, "0");
}
sb.Append(convertedByte);
}
macAddress = sb.ToString().Remove(sb.Length - 1);
return macAddress;
}
return "02:00:00:00:00:00";
}