I have two methods that will try to start the WiFi hotspot on the android device so that others can connect and play the game.the problem is both method have problems and don't work. the first method:
public void ConnectWiFi()
{
try
{
using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity"))
{
using (var wifiManager = activity.Call("getSystemService", "wifi"))
{
AndroidJavaObject wifiConfiguration = new AndroidJavaClass("android.net.wifi.WifiConfiguration");
wifiConfiguration.Set("SSID", meSSID); // string
wifiConfiguration.Set("preSharedKey", mePassword); // string
wifiManager.Call("setWifiApEnabled", wifiConfiguration, enabled);
}
}
}
catch (System.Exception E)
{
T.text += "\nE - " + E.Message;
T.text += "\n\t" + E.StackTrace+"\n";
}
}
it crashes the game and I can't debug it.
the secont method:
public void ConnectWiFiHardWay()
{
try
{
using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity"))
{
using (var wifiManager = activity.Call("getSystemService", "wifi"))
{
using (AndroidJavaObject classObj = wifiManager.Call("getClass"))
{
using (AndroidJavaObject wifiConfiguration = new AndroidJavaObject("setWifiApEnabled"))
{
wifiConfiguration.Set("SSID", meSSID); // string
wifiConfiguration.Set("preSharedKey", mePassword);
using (AndroidJavaObject wifiCfgClass = wifiConfiguration.Call("getClass"))
{
using (AndroidJavaObject booleanObj = new AndroidJavaObject("java.lang.Boolean"))
{
using (AndroidJavaObject booleanClass = booleanObj.Call("getClass"))
using (AndroidJavaObject methodObj = classObj.Call("getMethod", "setWifiApEnabled", wifiCfgClass, booleanClass))
{
methodObj.Call("invoke", wifiManager, wifiConfiguration, enabled);
}
}
}
}
}
}
}
}
catch (System.Exception E)
{
T.text += "\nH - " + E.Message;
T.text += "\n\t" + E.StackTrace+"\n";
}
this one throws an exception that says setWifiApEnabled
Cannot be found
I can post the complete error text if that helps.
Also I followed this questions instructions.