3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ stackoverflow.com/questions/30527188/… \$\endgroup\$ – Peter - Unban Robert Harvey May 20 '17 at 10:07
  • \$\begingroup\$ I don't know what you are implying @Peter but I added the permissions that were mentioned and it didn't work. it still crashes. I don't get any errors, the game crashes and closes before any error is displayed. \$\endgroup\$ – Henjin May 20 '17 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.