Search code examples
androidandroid-wifihotspot

How to start wifi hotspot with custom (ssid and password )configuration using ConnectivityManager?


I need to create a wifi access point using custom ssid and password. I looked on the internet and most of the answers use WifiManger#setWifiApEnabled via reflection. But when i checked the source it says that api is deprecated and also inside the method it does not start Access point just gives a warning log.

 @SystemApi
    @Deprecated
    @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
    public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
        String packageName = mContext.getOpPackageName();

        Log.w(TAG, packageName + " attempted call to setWifiApEnabled: enabled = " + enabled);
        return false;
    }

In method's description it says to use ConnectivityManager#startTethering. It looks like it can start Access Point but i cannot see where i should give my WiFi configuration as that class is for multiple (bluetooth, lte etc) connectivity types.

 @SystemApi
    @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
    public void startTethering(int type, boolean showProvisioningUi,
            final OnStartTetheringCallback callback, Handler handler) {
        Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null.");

        ResultReceiver wrappedCallback = new ResultReceiver(handler) {
            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                if (resultCode == TETHER_ERROR_NO_ERROR) {
                    callback.onTetheringStarted();
                } else {
                    callback.onTetheringFailed();
                }
            }
        };

        try {
            String pkgName = mContext.getOpPackageName();
            Log.i(TAG, "startTethering caller:" + pkgName);
            mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName);
        } catch (RemoteException e) {
            Log.e(TAG, "Exception trying to start tethering.", e);
            wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
        }
    }

Also i want my app to support min api 16 (jellybean) and maximum oreo. Or should i rather force user to setup the Wifi Network ?


Solution

  • I think its not possible to start a custom wifi hotspot on oreo or later. There is a file transfer app called ShareIt which starts custom wifi hotspot when in receiving mode. If run on oreo, it creates a hotspot of name usually Android-blah with a random password and asks the sender to manually enter that password in order to send that file. May be android is forcing/suggesting its developers to use other wireless technologies (like Wifi direct) to do that kind of operations.