GBiradar
GBiradar

Reputation: 161

Wifi Hotspot creation fails in oreo

I'm working on a simple system app in Oreo AOSP to turn ON wifi Hotspot with predefined SSID and preshared key.

As my APP is built as a system app so i don't need reflection.

MainActivity.java

public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                boolean result = false;
                WifiManager mwifiManager;
                mwifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);

                try {
                        Method method = mwifiManager.getClass().getMethod("getWifiApConfiguration");
                        WifiConfiguration netconfig = (WifiConfiguration) method.invoke(mwifiManager);
                        netconfig.SSID = "DummyApp";
                        netconfig.preSharedKey = "1234567890";
                        netconfig.allowedKeyManagement.set(4);
                        mwifiManager.setWifiEnabled(false);
                        method = mwifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                        result = (boolean) method.invoke(mwifiManager, netconfig, true);
                        if (!result) {
                                Toast.makeText(this, "Hotspot creation failed", Toast.LENGTH_SHORT).show();
                        } else {
                                Toast.makeText(this, "Wifi Enabled", Toast.LENGTH_SHORT).show();
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
                finish();
        }

        @Override
        protected void onResume() {
                super.onResume();
        }
}

AndroidManifest.xml

    android:protectionLevel="signature|privileged"
    android:sharedUserId="android.uid.system">
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- for wifi  -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
    <uses-permission android:name="android.permission.OVERRIDE_WIFI_CONFIG" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.TETHER_PRIVILEGED" />

Wifi Should turn ON but getting following result:

Toast Message: Hotspot creation failed

Logcat: WifiManager: PACKAGE_NAME attempted call to setWifiApEnabled: enabled = true

update1: How to turn on/off wifi hotspot programmatically in Android 8.0 (Oreo)

I have tried above changes. It will turn on Hotspot locally but no custom SSID and password.

update2: After getting input from @Mr.AF.

Method setConfigMethod = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                                                                                 ......code snip.........

netconfig.allowedKeyManagement.set(4);
Method Method = mWifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);

HOTSPOT Creation FAiled

Upvotes: 3

Views: 1683

Answers (2)

Adeel Zafar
Adeel Zafar

Reputation: 371

You don't need to use reflection for versions>=Oreo. After android's public exposed API startLocalOnlyHotspot. I've explained this answer in detail on this question on Stackoveflow.

Upvotes: 1

GBiradar
GBiradar

Reputation: 161

To turn ON Portable HotSpot in Android Nougat and below following code works.

   Method method = mwifiManager.getClass().getMethod("getWifiApConfiguration");
   WifiConfiguration netconfig = (WifiConfiguration) method.invoke(mwifiManager);
   method = mwifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
   result = (boolean) method.invoke(mwifiManager, netconfig, true);

Above API is deprecated in Oreo.

Following is a @system hidden API in oreo, If the android application is built as a system app then only below API code can be accessed. In my case, I can use the below API.

   ConnectivityManager oncm = (ConnectivityManager)ontext.getSystemService(Context.CONNECTIVITY_SERVICE);

   oncm.startTethering(ConnectivityManager.TETHERING_WIFI, true, new ConnectivityManager.OnStartTetheringCallback() {

   @Override
   public void onTetheringStarted() {
          super.onTetheringStarted();
          Log.i(TAG, "Hotspot is successfully opened");
   }

   @Override
   public void onTetheringFailed() {
          super.onTetheringFailed();
          Log.e(TAG, "Hotspot failed to open");
   }
 });

Upvotes: 4

Related Questions