androidandroid-wifi

Android Wifi DPP (Wi-Fi Easy Connect)


Since Android 10+ doesn't let you connect to wifi networks programmatically anymore (you can only suggest/add networks to the list but they may never connect if you have an existing wifi connect), I'd like to use WiFi Easy Connect (https://source.android.com/devices/tech/connect/wifi-easy-connect) I'm assuming this is basically what the qr code scanning option is, that's accessed from wifi settings.

The documentation states that you should check whether it's supported:

Public APIs are available in Android 10 for use by apps:

    WifiManager#isEasyConnectSupported: Queries the framework to determine whether the device supports Wi-Fi Easy Connect.
    Activity#startActivityForResult(ACTION_PROCESS_WIFI_EASY_CONNECT_URI): Allows apps to integrate Wi-Fi Easy Connect into their onboarding/setup flow.

Which I've done as:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
    {
        val wifiManager = getSystemService(Context.WIFI_SERVICE) as WifiManager
        if (wifiManager.isEasyConnectSupported)
        {
            startActivityForResult(Intent(android.provider.Settings.ACTION_PROCESS_WIFI_EASY_CONNECT_URI), 1237)
        }
    }

However, this crashes as the activity is not found (I have tested on a Pixel 4XL and emulator both running R):

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: xxx, PID: 8498
    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.PROCESS_WIFI_EASY_CONNECT_URI }

Is this possible to be used by apps? Is there another way to actually connect to a wifi network reliably? Is this the correct approach? or is there a way to launch the setting screen directly?

I found these in an android manifest but I can't find a way to launch them:

 <activity
    android:name=".wifi.dpp.WifiDppConfiguratorActivity">
    <intent-filter>
        <action android:name="android.settings.WIFI_DPP_CONFIGURATOR_QR_CODE_SCANNER"/>
        <action android:name="android.settings.WIFI_DPP_CONFIGURATOR_QR_CODE_GENERATOR"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.settings.PROCESS_WIFI_EASY_CONNECT_URI"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:scheme="DPP"/>
    </intent-filter>
</activity>

<activity
    android:name=".wifi.dpp.WifiDppEnrolleeActivity">
    <intent-filter>
        <action android:name="android.settings.WIFI_DPP_ENROLLEE_QR_CODE_SCANNER"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

Solution

  • If you want to connect to Wifi using QR Code scanner, following code will work fine

    @RequiresApi(api = Build.VERSION_CODES.Q)
    private void startWifiQRCodeScanner(Context context)
    {
        final String INTENT_ACTION_WIFI_QR_SCANNER = "android.settings.WIFI_DPP_ENROLLEE_QR_CODE_SCANNER"; 
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    
        if(wifiManager.isEasyConnectSupported())
        {
            final Intent intent = new Intent(INTENT_ACTION_WIFI_QR_SCANNER);
            startActivityForResult(intent, 5000);
        }
    }
    

    I have found following code in packages/apps/Settings/AndroidManifest.xml:

            <activity
                android:name=".wifi.dpp.WifiDppEnrolleeActivity">
                <intent-filter>
                    <action android:name="android.settings.WIFI_DPP_ENROLLEE_QR_CODE_SCANNER"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                </intent-filter>
            </activity>