startLocalOnlyHotspot start the hotspot but not showing in wifi to other devices

233 Views Asked by At

I am trying to develop an app. For the app I need connect two device using wifi so I was trying to turn on hotspot from a device like "SHAREIT / SHAREME".

`private void startLocalOnlyHotspot() {
        if (wifiManager != null) {

            wifiManager.startLocalOnlyHotspot(new LocalOnlyHotspotCallback() {
                @Override
                public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                    super.onStarted(reservation);
                    Toast.makeText(MainActivity.this, "Local-only hotspot started", Toast.LENGTH_SHORT).show();
                    // Handle the hotspot reservation if needed
                }

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

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

This is my code. Here the Toast "Local-only hotspot started" appers but I am not seeing the created hostpot in other devices. BTW I am not using emulator. I am using physical device. and If I print the SSID and password they are available and when I am trying to connect using this credintial its not connecting.

Thanks in advance

I am expecting to get a proper code which can run hotspot using startLocalOnlyHotspot.

1

There are 1 best solutions below

1
Mhmd Razi On

keep WifiManager.LocalOnlyHotspotReservation in a variable :

private WifiManager.LocalOnlyHotspotReservation localreservation;

 private void startLocalOnlyHotspot() {
    if (wifiManager != null) {

        wifiManager.startLocalOnlyHotspot(new LocalOnlyHotspotCallback() {
            @Override
            public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                super.onStarted(reservation);

                 localreservation= reservation

                Toast.makeText(MainActivity.this, "Local-only hotspot started", Toast.LENGTH_SHORT).show();
                // Handle the hotspot reservation if needed
            }

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

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