Tuesday, January 13, 2015

Android create Secured Wi-Fi hotspot

Here I'm going to give you a sample for create wifi tethering hotspot programmatically using android WifiManager hotspot.

Following example can directly use for create a WPA_PSK secured wifi hotspot programmatically. But this won't create the hotspot that you set in this code if you have switch on the tethering hotspot in your device before executing this code.


---------------------------------------------------------<for more>-----------------------------------------------------------

For further references follow this link.

WifiManager wifiManager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);;
  
 if (wifiManager.isWifiEnabled()) {
          wifiManager.setWifiEnabled(false);
      }
      Method[] wmMethods = wifiManager.getClass().getDeclaredMethods();
      boolean methodFound = false;
      for (Method method: wmMethods) {
          if (method.getName().equals("setWifiApEnabled")) {
              methodFound = true;
              WifiConfiguration netConfig = new WifiConfiguration();
              netConfig.SSID = "dataanywhere";
              netConfig.preSharedKey="DataAnywhere";
              netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
              netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
              try {
                  boolean apstatus = (Boolean) method.invoke(wifiManager, netConfig, true);
                  
                  for (Method isWifiApEnabledmethod: wmMethods) {
                      if (isWifiApEnabledmethod.getName().equals("isWifiApEnabled")) {
                          while (!(Boolean) isWifiApEnabledmethod.invoke(wifiManager)) {};
                          for (Method method1: wmMethods) {
                              if (method1.getName().equals("getWifiApState")) {
                                  int apstate;
                                  apstate = (Integer) method1.invoke(wifiManager);
                                  Log.i(this.getClass().toString(), "Apstate ::: "+apstate);
                              }
                          }
                      }
                  }
                  if (apstatus) {
                      Log.d("Splash Activity", "Access Point created");
                  } else {
                      Log.d("Splash Activity", "Access Point creation failed");
                  }

              } catch (IllegalArgumentException e) {
                  e.printStackTrace();
              } catch (IllegalAccessException e) {
                  e.printStackTrace();
              } catch (InvocationTargetException e) {
                  e.printStackTrace();
              }
          }
      }
      if (!methodFound) {
          Log.d("Splash Activity",
              "cannot configure an access point");
      }
  }

/**
 * Switching On data
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void setMobileDataEnabled(Context context, boolean enabled) {
 Log.i("NetworkUtil", "Mobile data enabling" + enabled);
    final ConnectivityManager conman = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);
 
    try {
  Class conmanClass = Class.forName(conman.getClass().getName());
  final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
     connectivityManagerField.setAccessible(true);
     final Object connectivityManager = connectivityManagerField.get(conman);
     final Class connectivityManagerClass =  Class.forName(connectivityManager.getClass().getName());
    
  final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
     setMobileDataEnabledMethod.setAccessible(true);

     setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
 } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
Happy Hacking!!!

No comments:

Post a Comment