How to Turn on LocalOnlyHotspot using WifiNetworkSpecifier.Builder as WifiConfiguration is Deprecated?

28 views Asked by At

Here is My code-

public void turnOnLocalOnlyHotspot(Context context) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    String ssid = "AndroidAP_" + new Random().nextInt(10000);
    String passphrase = getRandomPassword();
    // Create WifiNetworkSpecifier.Builder
    
    WifiNetworkSpecifier.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        builder = new WifiNetworkSpecifier.Builder();

        builder.setSsid(ssid);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            builder.setBand(ScanResult.WIFI_BAND_24_GHZ);
        }
        builder.setWpa2Passphrase(passphrase);

        // Create NetworkSpecifier
        WifiNetworkSpecifier specifier = builder.build();

        // Create network request
        NetworkRequest networkRequest = new NetworkRequest.Builder()
                .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                .setNetworkSpecifier(specifier)
                .build();

        // Connect to network
        ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback(){
            @Override
            public void onAvailable(@NonNull Network network) {
                super.onAvailable(network);
                Toast.makeText(context, "created", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onUnavailable() {
                super.onUnavailable();
                Toast.makeText(context, "failed", Toast.LENGTH_SHORT).show();
            }
        };
        connectivityManager.requestNetwork(networkRequest, networkCallback);
    }
}

I tried the above method to achieve the turn on LocalOnlyHotspot but it is not happening please fix this issue?

1

There are 1 answers

5
waseem akram On

Try this one

wifi manager

public void turnOnLocalOnlyHotspot(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    
    if (wifiManager != null) {
        wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
            @Override
            public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                super.onStarted(reservation);
                Toast.makeText(context, "Local hotspot started", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStopped() {
                super.onStopped();
                Toast.makeText(context, "Local hotspot stopped", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailed(int reason) {
                super.onFailed(reason);
                Toast.makeText(context, "Failed to start local hotspot", Toast.LENGTH_SHORT).show();
            }
        }, new Handler());
    }
}