Recent

Author Topic: LAMW - Creating WiFi Access Point in Android  (Read 611 times)

software_developer

  • New member
  • *
  • Posts: 7
LAMW - Creating WiFi Access Point in Android
« on: October 27, 2020, 07:36:36 am »
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

  • Hero Member
  • *****
  • Posts: 1662
Re: LAMW - Creating WiFi Access Point in Android
« Reply #1 on: October 27, 2020, 09:27:57 pm »

Please,  can you point out some java/android  example/tutorial ?

I can try implement a LAMW  api wrapper.
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

software_developer

  • New member
  • *
  • Posts: 7
Re: LAMW - Creating WiFi Access Point in Android
« Reply #2 on: October 28, 2020, 09:04:04 am »

jmpessoa

  • Hero Member
  • *****
  • Posts: 1662
Re: LAMW - Creating WiFi Access Point in Android
« Reply #3 on: October 30, 2020, 02:30:47 am »

Thank you!

But as you may have noticed, the solutions are partial and need some "hack" or "rooted"  device or
undocumented [reflection]  api.
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

software_developer

  • New member
  • *
  • Posts: 7
Re: LAMW - Creating WiFi Access Point in Android
« Reply #4 on: October 31, 2020, 07:45:33 am »
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][+][-]
  1. package com.application.wifitoggle;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.net.wifi.WifiConfiguration;
  7. import android.net.wifi.WifiManager;
  8. import android.os.Build;
  9. import android.os.Bundle;
  10. import android.provider.Settings;
  11. import android.view.View;
  12. import android.widget.Button;
  13.  
  14. import androidx.appcompat.app.AppCompatActivity;
  15.  
  16. import java.lang.reflect.InvocationTargetException;
  17. import java.lang.reflect.Method;
  18.  
  19. public class MainActivity extends AppCompatActivity {
  20.  
  21.     //WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  22.     //WifiConfiguration wifiConfiguration = new WifiConfiguration();
  23.     //
  24.  
  25.     public static String TAG = "MainActivity";
  26.  
  27.     private Button hotspotenable;
  28.  
  29.     @Override
  30.     protected void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.activity_main);
  33.  
  34.         hotspotenable = (Button) findViewById(R.id.button);
  35.  
  36.  
  37.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  38.             if (Settings.System.canWrite(this.getApplicationContext())) {
  39.  
  40.             } else {
  41.                 Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
  42.                 intent.setData(Uri.parse("package:" + this.getPackageName()));
  43.                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  44.                 startActivity(intent);
  45.             }
  46.         }
  47.         hotspotenable.setOnClickListener(new View.OnClickListener() {
  48.             @Override
  49.             public void onClick(View v) {
  50.                 try {
  51.                     CreateNewWifiApNetwork();
  52.                 } catch (InvocationTargetException e) {
  53.                     e.printStackTrace();
  54.                 } catch (IllegalAccessException e) {
  55.                     e.printStackTrace();
  56.                 }
  57.             }
  58.         });
  59.     }
  60.  
  61.  
  62.     public void CreateNewWifiApNetwork() throws InvocationTargetException, IllegalAccessException {
  63.         String ssid = null;
  64.         String password = null;
  65.         apManager ap = new apManager(this.getApplicationContext());
  66.         if (ap.isApOn() == true)
  67.             hotspotenable.setText("Turn On");
  68.         else
  69.             hotspotenable.setText("Turn Off");
  70.  
  71.         WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  72.         Method[] methods = wifiManager.getClass().getDeclaredMethods();
  73.         for (Method m: methods) {
  74.             if (m.getName().equals("getWifiApConfiguration")) {
  75.                 WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);
  76.                 ssid = config.SSID;
  77.                 password = config.preSharedKey;
  78.                 //or other info about the current hotspot, accessible through config
  79.             }
  80.         }
  81.  
  82.         ap.createNewNetwork(ssid, password);
  83.  
  84.     }
  85.  
  86.  
  87. //    private void changeStateWifiAp(boolean activated) {
  88. //        Method method;
  89. //        try {
  90. //            method = wifiManager.getClass().getDeclaredMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
  91. //            method.invoke(wifiManager, wifiConfiguration, activated);
  92. //        } catch (Exception e) {
  93. //            e.printStackTrace();
  94. //        }
  95. //    }
  96. }
  97.  

ApManager
Code: Java  [Select][+][-]
  1. package com.application.wifitoggle;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.net.wifi.WifiConfiguration;
  7. import android.net.wifi.WifiManager;
  8. import android.os.Build;
  9. import android.provider.Settings;
  10. import android.util.Log;
  11.  
  12. import java.lang.reflect.Method;
  13.  
  14. public class apManager {
  15.  
  16.     public static final String TAG = "ApManager";
  17.     private final WifiManager mWifiManager;
  18.     private Context context;
  19.  
  20.     public apManager(Context context) {
  21.         this.context = context;
  22.         mWifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
  23.     }
  24.  
  25.     public apManager(WifiManager mWifiManager) {
  26.         this.mWifiManager = mWifiManager;
  27.     }
  28.  
  29.     public void showWritePermissionSettings(boolean force) {
  30.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  31.             if (force || !Settings.System.canWrite(this.context)) {
  32.                 Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
  33.                 intent.setData(Uri.parse("package:" + this.context.getPackageName()));
  34.                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  35.                 this.context.startActivity(intent);
  36.             }
  37.         }
  38.     }
  39.     //check whether wifi hotspot on or off
  40.     public boolean isApOn() {
  41.         try {
  42.             Method method = mWifiManager.getClass().getDeclaredMethod("isWifiApEnabled");
  43.             method.setAccessible(true);
  44.             return (Boolean) method.invoke(mWifiManager);
  45.         } catch (Exception e) {
  46.             e.printStackTrace();
  47.         }
  48.         return false;
  49.     }
  50.     // Turn wifiAp hotspot on
  51.     public boolean turnWifiApOn() {
  52.         WifiConfiguration wificonfiguration = null;
  53.         try {
  54.             Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
  55.             method.invoke(mWifiManager, wificonfiguration, true);
  56.             return true;
  57.         } catch (Exception e) {
  58.             e.printStackTrace();
  59.         }
  60.         return false;
  61.     }
  62.  
  63.     // Turn wifiAp hotspot off
  64.     public boolean turnWifiApOff() {
  65.         WifiConfiguration wificonfiguration = null;
  66.         try {
  67.             Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
  68.             method.invoke(mWifiManager, wificonfiguration, false);
  69.             return true;
  70.         } catch (Exception e) {
  71.             e.printStackTrace();
  72.         }
  73.         return false;
  74.     }
  75.  
  76.  
  77.     public boolean createNewNetwork(String ssid, String password) {
  78.         mWifiManager.setWifiEnabled(false); // turn off Wifi
  79.         if (isApOn()) {
  80.             turnWifiApOff();
  81.             return false;
  82.         } else {
  83.             Log.e(TAG, "WifiAp is turned off");
  84.  
  85.         }
  86.  
  87.         // creating new wifi configuration
  88.         WifiConfiguration myConfig = new WifiConfiguration();
  89.         myConfig.SSID = ssid; // SSID name of netwok
  90.         myConfig.preSharedKey = password; // password for network
  91.         myConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); // 4 is for KeyMgmt.WPA2_PSK which is not exposed by android KeyMgmt class
  92.         myConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); // Set Auth Algorithms to open
  93.         try {
  94.             Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
  95.             return (Boolean) method.invoke(mWifiManager, myConfig, true);  // setting and turing on android wifiap with new configrations
  96.         } catch (Exception e) {
  97.             e.printStackTrace();
  98.         }
  99.         return false;
  100.  
  101.     }
  102.  
  103.  
  104. }

jmpessoa

  • Hero Member
  • *****
  • Posts: 1662
Re: LAMW - Creating WiFi Access Point in Android
« Reply #5 on: October 31, 2020, 05:56:15 pm »
Nice!!

I will try your code!!!

edited: What android manifest permission we need?
« Last Edit: October 31, 2020, 08:04:43 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

jmpessoa

  • Hero Member
  • *****
  • Posts: 1662
Re: LAMW - Creating WiFi Access Point in Android
« Reply #6 on: November 01, 2020, 02:41:32 am »
Done!

Improved jWifiManager component!

Please,  test and improve the new stuff

Code: Pascal  [Select][+][-]
  1.     function NeedWriteSettingsPermission(): boolean;
  2.     procedure RequestWriteSettingsPermission();
  3.     procedure SetWifiHotspotOn();
  4.     procedure SetWifiHotspotOff();
  5.     function IsWifiHotspotEnable(): boolean;
  6.     function CreateNewWifiNetwork(): boolean;
  7.  

and demo "AppWifiManagerDemo2"

Thank you!
« Last Edit: November 01, 2020, 02:43:48 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

software_developer

  • New member
  • *
  • Posts: 7
Re: LAMW - Creating WiFi Access Point in Android
« Reply #7 on: November 01, 2020, 09:44:06 am »
Hi jmpessoa,

It worked, thanks for adding this!

I have created some more functions.

Code: Java  [Select][+][-]
  1. public void CreateDefaultWifiApNetwork()
  2. public void CreateNewWifiApNetwork(String ssid, String password)
  3. private String getCurrentHotspotSSID()
  4. private String getCurrentHotspotPassword()

MainActivity
Code: Java  [Select][+][-]
  1. package com.application.wifitoggle;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.net.wifi.WifiConfiguration;
  7. import android.net.wifi.WifiManager;
  8. import android.os.Build;
  9. import android.os.Bundle;
  10. import android.provider.Settings;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.Toast;
  14.  
  15. import androidx.appcompat.app.AppCompatActivity;
  16.  
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.lang.reflect.Method;
  19.  
  20. public class MainActivity extends AppCompatActivity {
  21.  
  22.     //WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  23.     //WifiConfiguration wifiConfiguration = new WifiConfiguration();
  24.     //
  25.  
  26.     public static String TAG = "MainActivity";
  27.  
  28.     private Button hotspotenable;
  29.  
  30.     @Override
  31.     protected void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.activity_main);
  34.  
  35.         hotspotenable = (Button) findViewById(R.id.button);
  36.  
  37.         try {
  38.             Toast.makeText(getApplicationContext(),getCurrentHotspotSSID() + getCurrentHotspotPassword(),Toast.LENGTH_SHORT).show();
  39.         } catch (InvocationTargetException e) {
  40.             e.printStackTrace();
  41.         } catch (IllegalAccessException e) {
  42.             e.printStackTrace();
  43.         }
  44.  
  45.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  46.             if (Settings.System.canWrite(this.getApplicationContext())) {
  47.  
  48.             } else {
  49.                 Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
  50.                 intent.setData(Uri.parse("package:" + this.getPackageName()));
  51.                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  52.                 startActivity(intent);
  53.             }
  54.         }
  55.         hotspotenable.setOnClickListener(new View.OnClickListener() {
  56.             @Override
  57.             public void onClick(View v) {
  58.                 try {
  59.                     CreateNewWifiApNetwork(getCurrentHotspotSSID(), getCurrentHotspotPassword());
  60.                 } catch (InvocationTargetException e) {
  61.                     e.printStackTrace();
  62.                 } catch (IllegalAccessException e) {
  63.                     e.printStackTrace();
  64.                 }
  65.             }
  66.         });
  67.     }
  68.  
  69.     public void CreateDefaultWifiApNetwork() throws InvocationTargetException, IllegalAccessException {
  70.         String ssid = null;
  71.         String password = null;
  72.         apManager ap = new apManager(this.getApplicationContext());
  73.         if (ap.isApOn() == true)
  74.             hotspotenable.setText("Turn On");
  75.         else
  76.             hotspotenable.setText("Turn Off");
  77.  
  78.         WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  79.         Method[] methods = wifiManager.getClass().getDeclaredMethods();
  80.         for (Method m: methods) {
  81.             if (m.getName().equals("getWifiApConfiguration")) {
  82.                 WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);
  83.                 ssid = config.SSID;
  84.                 password = config.preSharedKey;
  85.                 //or other info about the current hotspot, accessible through config
  86.             }
  87.         }
  88.  
  89.         ap.createNewNetwork(ssid, password);
  90.  
  91.     }
  92.  
  93.  
  94.     public void CreateNewWifiApNetwork(String ssid, String password) throws InvocationTargetException, IllegalAccessException {
  95.         //String ssid = null;
  96.         //String password = null;
  97.         apManager ap = new apManager(this.getApplicationContext());
  98.         if (ap.isApOn() == true)
  99.             hotspotenable.setText("Turn On");
  100.         else
  101.             hotspotenable.setText("Turn Off");
  102.         /*
  103.         WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  104.         Method[] methods = wifiManager.getClass().getDeclaredMethods();
  105.         for (Method m: methods) {
  106.             if (m.getName().equals("getWifiApConfiguration")) {
  107.                 WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);
  108.                 ssid = config.SSID;
  109.                 password = config.preSharedKey;
  110.                 //or other info about the current hotspot, accessible through config
  111.             }
  112.         }
  113.         */
  114.         ap.createNewNetwork(ssid, password);
  115.  
  116.     }
  117.  
  118.     private String getCurrentHotspotSSID() throws InvocationTargetException, IllegalAccessException {
  119.         String strSSID=null;
  120.         WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  121.         Method[] methods = wifiManager.getClass().getDeclaredMethods();
  122.         for (Method m: methods) {
  123.             if (m.getName().equals("getWifiApConfiguration")) {
  124.                 WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);
  125.                 strSSID = config.SSID;
  126.             }
  127.         }
  128.         return strSSID;
  129.     }
  130.  
  131.     private String getCurrentHotspotPassword() throws InvocationTargetException, IllegalAccessException {
  132.         String strPassword=null;
  133.         WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  134.         Method[] methods = wifiManager.getClass().getDeclaredMethods();
  135.         for (Method m: methods) {
  136.             if (m.getName().equals("getWifiApConfiguration")) {
  137.                 WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);
  138.                 strPassword=config.preSharedKey;
  139.             }
  140.         }
  141.         return strPassword;
  142.     }
  143.  
  144.  
  145.  
  146.  
  147. //    private void changeStateWifiAp(boolean activated) {
  148. //        Method method;
  149. //        try {
  150. //            method = wifiManager.getClass().getDeclaredMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
  151. //            method.invoke(wifiManager, wifiConfiguration, activated);
  152. //        } catch (Exception e) {
  153. //            e.printStackTrace();
  154. //        }
  155. //    }
  156. }
  157.  

Manifest
Code: XML  [Select][+][-]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.    xmlns:tools="http://schemas.android.com/tools"
  4.    package="com.application.wifitoggle">
  5.     <uses-permission
  6.        android:name="android.permission.WRITE_SETTINGS"
  7.        tools:ignore="ProtectedPermissions"/>
  8.  
  9.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  10.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
  11.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  12.  
  13.     <application
  14.        android:allowBackup="true"
  15.        android:icon="@mipmap/ic_launcher"
  16.        android:label="@string/app_name"
  17.        android:roundIcon="@mipmap/ic_launcher_round"
  18.        android:supportsRtl="true"
  19.        android:theme="@style/AppTheme">
  20.         <activity android:name=".MainActivity">
  21.             <intent-filter>
  22.                 <action android:name="android.intent.action.MAIN" />
  23.  
  24.                 <category android:name="android.intent.category.LAUNCHER" />
  25.             </intent-filter>
  26.         </activity>
  27.     </application>
  28.  
  29. </manifest>
  30.  

jmpessoa

  • Hero Member
  • *****
  • Posts: 1662
Re: LAMW - Creating WiFi Access Point in Android
« Reply #8 on: November 01, 2020, 08:19:07 pm »

Done!!

Improved jWifiManager component:

Added news methods as [public]:

    function CreateNewWifiNetwork(_ssid: string; _password: string): boolean; overload;
    function GetCurrentHotspotSSID(): string;
    function GetCurrentHotspotPassword(): string;
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

 

TinyPortal © 2005-2018