LAMW - Creating WiFi Access Point in Android lazarus / 43-Android ---------------------------------------------------------------------- software_developer · 2020-10-27 06:36 I was trying to create WiFi Access Point/Hotspot, however, I couldn't find any component that could create it. I have also tried the jWiFiManager component, but could not create the access point. Is there any possibility to create WiFi Access Point? ---------------------------------------------------------------------- jmpessoa · 2020-10-27 20:27 Please,  can you point out some java/android  example/tutorial ? I can try implement a LAMW  api wrapper. ---------------------------------------------------------------------- software_developer · 2020-10-28 08:04 Hi jmpessoa, I have found some resources that may help in the development of LAMW API. GITHUB Toggle Wi Fi Hotspot / Access Point https://github.com/aegis1980/WifiHotSpot Other Projects https://github.com/mvdan/accesspoint https://github.com/geekywoman/ultimate-hotspot STACKOVERFLOW Create Access Point https://stackoverflow.com/questions/12926738/how-to-create-access- point-programmatically Change Wi Fi Configuration https://stackoverflow.com/questions/54500228/how-to-change- wificonfigration-in-oreo-device Turn On/Off Wi Fi Hotspot https://stackoverflow.com/questions/45984345/how-to-turn-on-off-wifi- hotspot-programmatically-in-android-8-0-oreo Get SSID and Password https://stackoverflow.com/questions/25173596/android-get-ssid-and- password-of-a-phone-which-is-a-portable-wifi-hotspot Change Hotspot Settings https://stackoverflow.com/questions/46392277/changing-android-hotspot- settings Create Access Point https://stackoverflow.com/questions/27653084/creating-wpa2-psk-access- point-in-android-programmatically ANDOIRD STUDIO DOCUMENTATION WifiManager.LocalOnlyHotspotCallback https://developer.android.com/reference/android/net/wifi/WifiManager.L ocalOnlyHotspotCallback WifiManager.LocalOnlyHotspotReservation https://developer.android.com/reference/android/net/wifi/WifiManager.L ocalOnlyHotspotReservation#getWifiConfiguration() SoftApConfiguration https://developer.android.com/reference/android/net/wifi/SoftApConfigu ration ---------------------------------------------------------------------- jmpessoa · 2020-10-30 01:30 Thank you! But as you may have noticed, the solutions are partial and need some "hack" or "rooted"  device or undocumented [reflection]  api. ---------------------------------------------------------------------- software_developer · 2020-10-31 06:45 Hi jmpessoa, I have used the below code for starting/stopping the Hotspot. I observed that it doesn't require a rooted device. MainActivity Code: Java  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i package com.application.wifitoggle; import android.content.Context;import android.content.Intent;import android.net.Uri;import android.net.wifi.WifiConfiguration;import android.net.wifi.WifiManager;import android.os.Build;import android.os.Bundle;import android.provider.Settings;import android.view.View;import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method; public class MainActivity extends AppCompatActivity {     //WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);    //WifiConfiguration wifiConfiguration = new WifiConfiguration();    //     public static String TAG = "MainActivity";     private Button hotspotenable;     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);         hotspotenable = (Button) findViewById(R.id.button);          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {            if (Settings.System.canWrite(this.getApplicationContext())) {           } else {                Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);              intent.setData(Uri.parse("package:" + this.getPackageName()));                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                startActivity(intent);            }        }        hotspotenable.setOnClickListener(new View.OnClickListener() {          @Override            public void onClick(View v) {                try {                    CreateNewWifiApNetwork();                } catch (InvocationTargetException e) {                    e.printStackTrace();                } catch (IllegalAccessException e) {                    e.printStackTrace();                }            }        });    }      public void CreateNewWifiApNetwork() throws InvocationTargetException, IllegalAccessException {        String ssid = null;        String password = null;        apManager ap = new apManager(this.getApplicationContext());        if (ap.isApOn() == true)            hotspotenable.setText("Turn On");        else          hotspotenable.setText("Turn Off");         WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);      Method[] methods = wifiManager.getClass().getDeclaredMethods();      for (Method m: methods) {            if (m.getName().equals("getWifiApConfiguration")) {                WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);                ssid = config.SSID;                password = config.preSharedKey;                //or other info about the current hotspot, accessible through config            }        }         ap.createNewNetwork(ssid, password);     }  //    private void changeStateWifiAp(boolean activated) {//        Method method;//      try {//            method = wifiManager.getClass().getDeclaredMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);//            method.invoke(wifiManager, wifiConfiguration, activated);//        } catch (Exception e) {//            e.printStackTrace();//        }//  }}  ApManager Code: Java  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i package com.application.wifitoggle; import android.content.Context;import android.content.Intent;import android.net.Uri;import android.net.wifi.WifiConfiguration;import android.net.wifi.WifiManager;import android.os.Build;import android.provider.Settings;import android.util.Log; import java.lang.reflect.Method; public class apManager {     public static final String TAG = "ApManager";    private final WifiManager mWifiManager;    private Context context;     public apManager(Context context) {        this.context = context;        mWifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);    }     public apManager(WifiManager mWifiManager) {        this.mWifiManager = mWifiManager;    }     public void showWritePermissionSettings(boolean force) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {            if (force || !Settings.System.canWrite(this.context)) {                Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);              intent.setData(Uri.parse("package:" + this.context.getPackageName()));                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                this.context.startActivity(intent);            }        }    }    //check whether wifi hotspot on or off    public boolean isApOn() {      try {            Method method = mWifiManager.getClass().getDeclaredMethod("isWifiApEnabled");          method.setAccessible(true);            return (Boolean) method.invoke(mWifiManager);        } catch (Exception e) {          e.printStackTrace();        }        return false;    }    // Turn wifiAp hotspot on    public boolean turnWifiApOn() {        WifiConfiguration wificonfiguration = null;        try {            Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);            method.invoke(mWifiManager, wificonfiguration, true);            return true;        } catch (Exception e) {            e.printStackTrace();        }        return false;    }     // Turn wifiAp hotspot off    public boolean turnWifiApOff() {        WifiConfiguration wificonfiguration = null;        try {            Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);            method.invoke(mWifiManager, wificonfiguration, false);            return true;        } catch (Exception e) {            e.printStackTrace();        }        return false;    }      public boolean createNewNetwork(String ssid, String password) {        mWifiManager.setWifiEnabled(false); // turn off Wifi        if (isApOn()) {            turnWifiApOff();            return false;      } else {            Log.e(TAG, "WifiAp is turned off");         }       // creating new wifi configuration        WifiConfiguration myConfig = new WifiConfiguration();        myConfig.SSID = ssid; // SSID name of netwok        myConfig.preSharedKey = password; // password for network        myConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); // 4 is for KeyMgmt.WPA2_PSK which is not exposed by android KeyMgmt class        myConfig.allowedAuthAlgorithms.set(WifiConfiguration.Auth Algorithm.OPEN); // Set Auth Algorithms to open        try {          Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);            return (Boolean) method.invoke(mWifiManager, myConfig, true);  // setting and turing on android wifiap with new configrations        } catch (Exception e) {          e.printStackTrace();        }        return false;     }  } ---------------------------------------------------------------------- jmpessoa · 2020-10-31 16:56 Nice!! I will try your code!!! edited: What android manifest permission we need? ---------------------------------------------------------------------- jmpessoa · 2020-11-01 01:41 Done! Improved jWifiManager component! Please,  test and improve the new stuff Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     function NeedWriteSettingsPermission(): boolean;    procedure RequestWriteSettingsPermission();    procedure SetWifiHotspotOn();    procedure SetWifiHotspotOff();    function IsWifiHotspotEnable(): boolean;    function CreateNewWifiNetwork(): boolean;  and demo "AppWifiManagerDemo2" Thank you! ---------------------------------------------------------------------- software_developer · 2020-11-01 08:44 Hi jmpessoa, It worked, thanks for adding this! I have created some more functions. Code: Java  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i public void CreateDefaultWifiApNetwork()public void CreateNewWifiApNetwork(String ssid, String password)private String getCurrentHotspotSSID()private String getCurrentHotspotPassword() MainActivity Code: Java  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i package com.application.wifitoggle; import android.content.Context;import android.content.Intent;import android.net.Uri;import android.net.wifi.WifiConfiguration;import android.net.wifi.WifiManager;import android.os.Build;import android.os.Bundle;import android.provider.Settings;import android.view.View;import android.widget.Button;import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method; public class MainActivity extends AppCompatActivity {     //WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);    //WifiConfiguration wifiConfiguration = new WifiConfiguration();    //     public static String TAG = "MainActivity";     private Button hotspotenable;     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);         hotspotenable = (Button) findViewById(R.id.button);         try {            Toast.makeText(getApplicationContext(),getCurrentHotspotSSID() + getCurrentHotspotPassword(),Toast.LENGTH_SHORT).show();        } catch (InvocationTargetException e) {            e.printStackTrace();      } catch (IllegalAccessException e) {            e.printStackTrace();      }         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {          if (Settings.System.canWrite(this.getApplicationContext())) {             } else {                Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);              intent.setData(Uri.parse("package:" + this.getPackageName()));                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                startActivity(intent);            }        }        hotspotenable.setOnClickListener(new View.OnClickListener() {          @Override            public void onClick(View v) {                try {                    CreateNewWifiApNetwork(getCurrentHotspotSSID(), getCurrentHotspotPassword());                } catch (InvocationTargetException e) {                    e.printStackTrace();                } catch (IllegalAccessException e) {                    e.printStackTrace();                }            }        });    }     public void CreateDefaultWifiApNetwork() throws InvocationTargetException, IllegalAccessException {        String ssid = null;        String password = null;        apManager ap = new apManager(this.getApplicationContext());        if (ap.isApOn() == true)            hotspotenable.setText("Turn On");        else          hotspotenable.setText("Turn Off");         WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);      Method[] methods = wifiManager.getClass().getDeclaredMethods();      for (Method m: methods) {            if (m.getName().equals("getWifiApConfiguration")) {                WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);                ssid = config.SSID;                password = config.preSharedKey;                //or other info about the current hotspot, accessible through config            }        }         ap.createNewNetwork(ssid, password);     }      public void CreateNewWifiApNetwork(String ssid, String password) throws InvocationTargetException, IllegalAccessException {        //String ssid = null;        //String password = null;        apManager ap = new apManager(this.getApplicationContext());        if (ap.isApOn() == true)            hotspotenable.setText("Turn On");        else          hotspotenable.setText("Turn Off");        /*        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);      Method[] methods = wifiManager.getClass().getDeclaredMethods();      for (Method m: methods) {            if (m.getName().equals("getWifiApConfiguration")) {                WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);                ssid = config.SSID;                password = config.preSharedKey;                //or other info about the current hotspot, accessible through config            }        }        */      ap.createNewNetwork(ssid, password);     }     private String getCurrentHotspotSSID() throws InvocationTargetException, IllegalAccessException {        String strSSID=null;        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);      Method[] methods = wifiManager.getClass().getDeclaredMethods();      for (Method m: methods) {            if (m.getName().equals("getWifiApConfiguration")) {                WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);                strSSID = config.SSID;            }        }        return strSSID;    }     private String getCurrentHotspotPassword() throws InvocationTargetException, IllegalAccessException {        String strPassword=null;        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);      Method[] methods = wifiManager.getClass().getDeclaredMethods();      for (Method m: methods) {            if (m.getName().equals("getWifiApConfiguration")) {                WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);                strPassword=config.preSharedKey;            }        }      return strPassword;    }    //    private void changeStateWifiAp(boolean activated) {//        Method method;//      try {//            method = wifiManager.getClass().getDeclaredMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);//            method.invoke(wifiManager, wifiConfiguration, activated);//        } catch (Exception e) {//            e.printStackTrace();//        }//  }}  Manifest Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i                                                                                                    ---------------------------------------------------------------------- jmpessoa · 2020-11-01 19:19 Done!! Improved jWifiManager component: Added news methods as [public]:     function CreateNewWifiNetwork(_ssid: string; _password: string): boolean; overload;     function GetCurrentHotspotSSID(): string;     function GetCurrentHotspotPassword(): string;