'How to turn on hotspot with Android board?

I am using the Android board Tinker Board 2S(Android 11).

I'm going to run an app that turns on and off hotspots on this board.

Here's the my code.

public class MainActivity extends AppCompatActivity {

    String TAG = "MYLOG";

    @RequiresApi(api = Build.VERSION_CODES.R)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button)findViewById(R.id.button);
        Button button2 = (Button)findViewById(R.id.button2);

        button.setOnClickListener(view -> turnOnHotspot());

        button2.setOnClickListener(view -> turnOffHotspot());
    }

    private WifiManager.LocalOnlyHotspotReservation mReservation;

    @RequiresApi(api = Build.VERSION_CODES.R)
    private void turnOnHotspot() {
        WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }

        manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
            @Override
            public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                super.onStarted(reservation);
                Log.d(TAG, "Wifi Hotspot is on now");
                mReservation = reservation;
            }

            @Override
            public void onStopped() {
                super.onStopped();
                Log.d(TAG, "onStopped: ");
            }

            @Override
            public void onFailed(int reason) {
                super.onFailed(reason);
                Log.d(TAG, "onFailed: ");
            }
        }, new Handler());
    }

    @RequiresApi(api = Build.VERSION_CODES.R)
    private void turnOffHotspot() {
        if (mReservation != null) {
            mReservation.close();
        }
    }
}

There is no error running this app.

However, even if I press the hotspot button, the Wi-Fi turns off, but the hotspot does not turn on.

Is there a difference between Android board and mobile phone when applying this code?

Plz help me.

++ This is the information found using getSoftApConfiguration() function.

ssid=AndroidShare_6368 
     Passphrase =<non-empty> 
     HiddenSsid =false 
     Band =1 
     Channel =0 
     SecurityType=1 
     MaxClient=0 
     AutoShutdownEnabled=false 
     ShutdownTimeoutMillis=0 
     ClientControlByUser=false 
     BlockedClientList=[] 
     AllowedClientList=[]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source