// check whether wifi hotspot on or off public static boolean isApOn(Context context) { WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE); try { Method method = wifimanager.getClass().getDeclaredMethod("isWifiApEnabled"); method.setAccessible(true); return (Boolean) method.invoke(wifimanager); } catch (Throwable ignored) { } return false; }
@Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { this.callbackContext = callbackContext; Log.d("WIFI", action); if(ACTION_START.equals(action)){ PluginResult pgRes = new PluginResult(PluginResult.Status.OK, "Registered"); pgRes.setKeepCallback(true); mWifiManager = (WifiManager) this.cordova.getActivity().getSystemService(Context.WIFI_SERVICE); this.reciever = new BroadcastReceiver() { @Override public void onReceive(Context c, Intent intent) { List<ScanResult> scanResults = mWifiManager.getScanResults(); handleResults(scanResults); } }; this.cordova.getActivity().registerReceiver(this.reciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); callbackContext.sendPluginResult(pgRes); Log.d("WIFI", "inside " + action); return true; } else if (ACTION_SCAN.equals(action)){ PluginResult pgRes = new PluginResult(PluginResult.Status.OK, []); pgRes.setKeepCallback(true); mWifiManager = (WifiManager) this.cordova.getActivity().getSystemService(Context.WIFI_SERVICE); mWifiManager.startScan(); callbackContext.sendPluginResult(pgRes); Log.d("WIFI", "inside " + action); return true; } else if (ACTION_STOP.equals(action)){
// toggle wifi hotspot on or off public static boolean configApState(Context context) { WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE); WifiConfiguration wificonfiguration = null; try { // if WiFi is on, turn it off if (isApOn(context)) { wifimanager.setWifiEnabled(false); } Method method = wifimanager .getClass() .getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); method.invoke(wifimanager, wificonfiguration, !isApOn(context)); return true; } catch (Exception e) { e.printStackTrace(); } return false; }