I did this in my application. Please check my code below.
First add below permissions for your manifest.
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" />
Then create below class MyHotspotManager.java
public class MyHotspotManager { private WifiConfiguration wifiCon; private HotspotDetails hotspotDetails; private WifiManager mWifiManager; public MyHotspotManager(WifiManager wifiManager){ this.mWifiManager = wifiManager; } public boolean setHotspot(HotspotDetails hotspotDetails) { this.hotspotDetails = hotspotDetails; boolean apstatus; wifiCon = new WifiConfiguration(); wifiCon.SSID = hotspotDetails.getSsid(); wifiCon.preSharedKey = hotspotDetails.getPassword(); wifiCon.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.RSN); wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.WPA); wifiCon.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); try { Method setWifiApMethod = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
After that, you can call the setHotspot method anywhere in your activity, as shown below. Here I needed to check if the application has ACTION_MANAGE_WRITE_SETTINGS permission.
public void OnButtonClick(View view) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (!Settings.System.canWrite(getApplicationContext())) { Toast.makeText(this, "Please try again after giving access to Change system settings", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName())); startActivityForResult(intent, 200); }else{ createHotspot(); } } } private void createHotspot(){ HotspotDetails hotspotDetails = new HotspotDetails(); hotspotDetails.setSsid("TestStackOverFlow"); hotspotDetails.setPassword("654321"); new MyHotspotManager((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE)) .setHotspot(hotspotDetails); }
But if you want, you can omit part of the permission request by changing the targetSdkVersion value to 21 in build.gradle. Now you can name it below.
public void OnButtonClick(View view) { // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // if (!Settings.System.canWrite(getApplicationContext())) { // Toast.makeText(this, "Please try again after giving access to Change system settings", Toast.LENGTH_SHORT).show(); // Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName())); // startActivityForResult(intent, 200); // // }else{ // } // } createHotspot(); } private void createHotspot(){ HotspotDetails hotspotDetails = new HotspotDetails(); hotspotDetails.setSsid("TestStackOverFlow"); hotspotDetails.setPassword("654321"); new MyHotspotManager((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE)) .setHotspot(hotspotDetails); }
This class is HotspotDetails.java
public class HotspotDetails { private String ssid; private String password; public String getSsid() { return ssid; } public void setSsid(String ssid) { this.ssid = ssid; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return ssid + ":" + password; } }