1

Following the answer to this question I could turn on the hotspot on an android 8 device. But the same code wont turn on the hotspot in an android 12 device.

Here is my MainActivity.java:

package io.ionic.starter;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Handler; 
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import com.getcapacitor.BridgeActivity;

public class MainActivity extends BridgeActivity {
  private static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
  private WifiManager.LocalOnlyHotspotReservation mReservation;
  @Override
  public void onStart() { 
    super.onStart();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      turnOnHotspot();     
    }
  }

  @RequiresApi(api = Build.VERSION_CODES.O)
  private void turnOnHotspot() {
    WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(this, 
        new String[]{(Manifest.permission.ACCESS_FINE_LOCATION)},
        MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
      return;
    }
    manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {

      @Override
      public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
        System.out.println("-------I'm going to run onStarted(reservation)-------");
        super.onStarted(reservation);
        mReservation = reservation;
        System.out.println("------OnStarted(reservation) has been run -------");
      }

      @Override
      public void onStopped() {
        super.onStopped();
      }

      @Override
      public void onFailed(int reason) {
        super.onFailed(reason);
      }
    }, new Handler());
  }  
}

In wifiManager docs it is mentioned that :

LocalOnlyHotspotCallback#onStarted(LocalOnlyHotspotReservation) is called when the hotspot is ready for use by the application.

Running the code, I can see both messages (OnStarted is going and has been run) which, based on the docs, means the hotspot is ready for use, but at the end the hotspot is not turning on.

I've granted required permissions, e.g.: ACCESS_FINE_LOCATION and CHANGE_WIFI_STATE. Also location is set as on while testing the app.

Also on android 8 device's log I can see the following, which I don't see for android 12:


D/WifiManager: LocalOnlyHotspotCallbackProxy: handle message what: 0 msg: { when=0 what=0 obj=* ID: -2 SSID: AndroidShare_3018 PROVIDER-NAME: null BSSID: null FQDN: null PRIO: 0 HIDDEN: false
     NetworkSelectionStatus NETWORK_SELECTION_ENABLED
     hasEverConnected: false
     KeyMgmt: WPA2_PSK Protocols:
     AuthAlgorithms:
     PairwiseCiphers:
     GroupCiphers:
     PSK: *
    Enterprise config:
    eap NULL
    phase2 "auth=NULL"
    IP config:
    IP assignment: UNASSIGNED
    Proxy settings: UNASSIGNED
     cuid=-1 luid=-1 lcuid=0 userApproved=USER_UNSPECIFIED noInternetAccessExpected=false roamingFailureBlackListTimeMilli: 1000
    samsungSpecificFlags:
    semAutoWifiScore: 0
    isVendorAp : false
    recoverableRSSI: -200
    inRecoverArea: false
    disabledTime: 0
    notInRangeTime: 0
    isUsableInternet: false
    skipInternetCheck: -1
    nextTargetRssi: 0
    isCaptivePortal: false
    isAuthenticated: false
    loginUrl: null
    autoReconnect: 1
    isHomeProviderNetwork: false
    isSpecialSsid: false
    isWeChatAp : false target=android.net.wifi.WifiManager$LocalOnlyHotspotCallbackProxy$1 }

Does anyone know what is the problem and how can be solved? Thanks in advance.

user6781
  • 25
  • 4

0 Answers0