2

I've seen many people ask the question already. But unfortunately, my knowledge of programming is limited only to writing PowerShell scripts :) and I cannot figure out the code.

Perhaps someone will throw java and xml code, applications. Which can turn off or turn on mobile hotspot programmatically, without going into the settings.

I was able to achieve that the settings would open where the user can toggle the state of the hotspot. But I need what the user could not do, tk. the application works in kiosk mode and it only needs to access the API and switch the state of the hotspot.

1 Answer 1

-1

You can use AndroidWifiManager library to enable hotspot programmatically.

Here is how it can be used in your code (This is already available in the Git ReadMe, I am just rewriting it.)

Step 1 : Add the jcenter repository to your build file

allprojects {
    repositories {
        ...
        jcenter()
    }
}

Step 2 : Add the dependency

dependencies {
    implementation 'com.vkpapps.wifimanager:APManager:1.0.0'
}

Step 3 : Use in your app

Handle error manually
APManager apManager = APManager.getApManager(this);
apManager.turnOnHotspot(this, new APManager.OnSuccessListener() {

    @Override
    public void onSuccess(String ssid, String password) {
        //write your logic
    }

}, new APManager.OnFailureListener() {

    @Override
    public void onFailure(int failureCode, @Nullable Exception e) {
        //handle error like give access to location permission,write system setting permission,
        //disconnect wifi,turn off already created hotspot,enable GPS provider
        
        //or use DefaultFailureListener class to handle automatically
    }

});
//use this line to turn off Hotspot
//apManager.disableWifiAp();
Handle error automatically with inbuilt class
APManager apManager = APManager.getApManager(this);
apManager.turnOnHotspot(this,
        new APManager.OnSuccessListener() {

            @Override
            public void onSuccess(@NonNull String ssid, @NonNull String password) {
                //write your logic
            }

        },
        new DefaultFailureListener(this)
);

//use this line to turn off Hotspot
//apManager.disableWifiAp();
2
  • 2
    This does not work. This creates a "local hotspot only" option which does not turn on the native hotspot feature.
    – Seth
    Jul 13, 2022 at 17:18
  • it's local only hotspot :v
    – ImBIOS
    Mar 16, 2023 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.