Reputation: 13
How can I get BSSID (MAC address of the wifi router I'm connecting in, not my device MAC Address) in my Unity Game App on Android and iOS phone?
I have tried these method but none of them working.I check the result by printing it on game scene. Please help
*Method 1: This code work fine and get the correct BSSID info but it's only work on Editor, when I build it to Android App it's not working, return string is nothing (Not null but empty value).
string mac = "";
var card = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault();
if (card == null)
return null;
else
{
byte[] bytes = card.GetPhysicalAddress().GetAddressBytes();
for (int i = 0; i < bytes.Length; i++)
{
mac = string.Concat(mac + (string.Format("{0}", bytes[i].ToString("X2"))));
if (i != bytes.Length - 1)
{
//This is just for formating the final result
mac = string.Concat(mac + ":");
}
}
mac = card.GetPhysicalAddress().ToString();
return mac;
}
*Method 2: I'm using AndroidJavaClass to call an android function to get BSSID on Android Phone as in Unity document. More info about AndroidJavaClass in Unity and WifiInfo class of Android
string mac = "";
AndroidJavaClass jc = new AndroidJavaClass("android.net.wifi.WifiInfo");
mac = jc.Call<string>("getBSSID");
I also add android permission for reading wifi state by modifing "AndroidManifest.xml" file at location "[Project Location]\Assets\Plugins\Android" with the code below
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Since this method not working on Editor, I build and run this on Android Phone but no string is return. Can anyone help me please.
Upvotes: 1
Views: 4526
Reputation: 10949
Use this
string bytes2Mac(byte[] bytes) {
string[] m = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
string ret = "";
foreach (byte b in bytes)
{
int low = b & 0x0F;
int hi = (b & 0xF0) >> 4;
ret += m[hi] + m[low];
}
return ret;
}
public string ReturnMacAddress()
{
string macAddr = "";
var network = new AndroidJavaClass("java.net.NetworkInterface").CallStatic<AndroidJavaObject>("getNetworkInterfaces");
while(network.Call<Boolean>("hasMoreElements")) {
var d = network.Call<AndroidJavaObject>("nextElement");
var address = d.Call<byte[]>("getHardwareAddress");
var ifname = d.Call<string>("getName");
if (address != null && ifname == "wlan0") {
macAddr = bytes2Mac(address);
break;
}
}
return macAddr;
}
Upvotes: 0
Reputation: 125245
You do need to make plugins in order to do this on mobile devices.
For iOS, you have to convert this answer into a plugin then call it from C#. There are many Unity iOS plugin tutorials out there that can get you started on this.
For Android, you should first check if Wifi is enabled before getting the BSSID with the code below:
public static string getSSID()
{
string tempSSID = "";
try
{
using (var activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
{
using (var wifiManager = activity.Call<AndroidJavaObject>("getSystemService", "wifi"))
{
tempSSID = wifiManager.Call<AndroidJavaObject>("getConnectionInfo").Call<string>("getSSID");
}
}
}
catch (Exception e)
{
}
return tempSSID;
}
public static string getBSSID()
{
string tempBSSID = "";
try
{
using (var activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
{
using (var wifiManager = activity.Call<AndroidJavaObject>("getSystemService", "wifi"))
{
tempBSSID = wifiManager.Call<AndroidJavaObject>("getConnectionInfo").Call<string>("getBSSID");
}
}
}
catch (Exception e)
{
}
return tempBSSID;
}
Don't forget to include the permission <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
.
Upvotes: 1