Example usage for android.net.wifi WifiManager setWifiEnabled

List of usage examples for android.net.wifi WifiManager setWifiEnabled

Introduction

In this page you can find the example usage for android.net.wifi WifiManager setWifiEnabled.

Prototype

public boolean setWifiEnabled(boolean enabled) 

Source Link

Document

Enable or disable Wi-Fi.

Usage

From source file:Main.java

public static void createWifiAccessPoint(Context context, String ntId, String password) {
    try {/* www.jav  a  2 s  . c o  m*/
        WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);

        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }

        Method[] wmMethods = wifiManager.getClass().getDeclaredMethods(); //Get all declared methods in WifiManager class     
        for (Method method : wmMethods) {
            if (method.getName().equals("setWifiApEnabled")) {
                WifiConfiguration netConfig = new WifiConfiguration();
                netConfig.SSID = ntId;
                netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                netConfig.preSharedKey = password;
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                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);
                                }
                            }
                        }
                    }
                    if (apstatus) {
                        Log.d("better", "Access Point Created");
                    } else {
                        Log.d("better", "Failed to create Access Point!");
                    }

                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * @Thach @SK29/*from w  w  w. j a v a 2 s  .  co  m*/
 * @Init MAC address and save to preference
 * @param context
 * @return
 */
private static String initMacAddress(Context context) {
    // INIT macAddress
    String macAddress;

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    if (wifiManager.isWifiEnabled()) {
        // WIFI ALREADY ENABLED. GRAB THE MAC ADDRESS HERE
        WifiInfo info = wifiManager.getConnectionInfo();
        macAddress = info.getMacAddress();

    } else {
        // ENABLE THE WIFI FIRST
        wifiManager.setWifiEnabled(true);

        // WIFI IS NOW ENABLED. GRAB THE MAC ADDRESS HERE
        WifiInfo info = wifiManager.getConnectionInfo();
        macAddress = info.getMacAddress();

        // NOW DISABLE IT AGAIN
        wifiManager.setWifiEnabled(false);
    }

    if (macAddress == null) {
        macAddress = android.os.Build.ID;
    }

    return macAddress;
}

From source file:org.basdroid.common.NetworkUtils.java

/**
 * enable wifi device//from  w w w .j a v a2  s .  co  m
 * @param context
 */
public static void enableWifiNetwork(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (wifiManager.isWifiEnabled()) {
        // WIFI ALREADY ENABLED. GRAB THE MAC ADDRESS HERE
        Log.d(TAG, "WIFI ALREADY ENABLED.");
    } else {
        // ENABLE THE WIFI FIRST
        Log.d(TAG, "WIFI DISABLED. ENABLE THE WIFI FIRST");
        wifiManager.setWifiEnabled(true);
    }

    return;
}

From source file:com.google.example.gcmnetworkmanagerquickstart.MainActivity.java

public void startWifiTask() {
    Log.d(TAG, "startWiFiTask");

    // Disable WiFi so the task does not start immediately
    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifi.setWifiEnabled(false);

    // [START start_one_off_task]
    OneoffTask task = new OneoffTask.Builder().setService(MyTaskService.class).setTag(TASK_TAG_WIFI)
            .setExecutionWindow(0L, 3600L).setRequiredNetwork(Task.NETWORK_STATE_UNMETERED).build();

    mGcmNetworkManager.schedule(task);//from   www .  j  a v  a  2s .  c o  m
    // [END start_one_off_task]
}

From source file:com.google.example.gcmnetworkmanagerquickstart.MainActivity.java

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button_start_wifi_task:
        startWifiTask();/*from www.  j a va 2s  .c o m*/
        break;
    case R.id.button_start_charging_task:
        startChargingTask();
        break;
    case R.id.button_turn_on_wifi:
        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifi.setWifiEnabled(true);
        break;
    case R.id.button_start_periodic_task:
        startPeriodicTask();
        break;
    case R.id.button_stop_periodic_task:
        stopPeriodicTask();
        break;
    }
}

From source file:test.microsoft.com.wifipairing.WifiBase.java

public boolean setWifiEnabled(boolean enabled) {
    WifiManager wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
    if (wifiManager != null) {
        return wifiManager.setWifiEnabled(enabled);
    } else {//from w w w  . j  a v a2s .  c  o m
        return false;
    }
}

From source file:com.example.qrpoll.MainActivity.java

@Override
/**//  ww  w .  ja v a2  s. co m
 * tworzenie startowego widoku aplikacji
 */
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    scanBtn = (Button) findViewById(R.id.scan_button);
    button1 = (Button) findViewById(R.id.button1);
    formatTxt = (TextView) findViewById(R.id.scan_format);
    contentTxt = (TextView) findViewById(R.id.scan_content);

    scanBtn.setOnClickListener(this);
    SqlHandler sql = new SqlHandler(getApplication());
    sql.open();
    sql.close();
    if (!checkNetwork()) {
        if (!checkWifi()) {
            AlertDialog.Builder adb = new AlertDialog.Builder(this);
            adb.setTitle("Brak polaczenia");
            adb.setMessage("Nacisnij ok aby wlaczyc WIFI").setCancelable(false).setNeutralButton("OK",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                            wifi.setWifiEnabled(true);
                        }
                    });
            AlertDialog ad = adb.create();
            ad.show();
        }
    }
}

From source file:test.microsoft.com.wifipairing.WifiBase.java

public boolean Start() {

    boolean ret = false;
    //just making sure that when we start we have the Wifi on,
    // after this point we'll respect if it is turned off
    WifiManager wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    }/*from w ww  .jav  a  2s  . co m*/

    p2p = (WifiP2pManager) this.context.getSystemService(Context.WIFI_P2P_SERVICE);

    if (p2p == null) {
        Log.d("WifiBase", "This device does not support Wi-Fi Direct");
    } else {
        ret = true;
        channel = p2p.initialize(this.context, this.context.getMainLooper(), this);
    }

    mBRReceiver = new MainBCReceiver();
    filter = new IntentFilter();
    filter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    LocalBroadcastManager.getInstance(this.context).registerReceiver((mBRReceiver), filter);

    return ret;
}

From source file:com.hari.autotasx.GeofenceTransitionsIntentService.java

@Override
protected void onHandleIntent(Intent intent) {

    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = GeofenceErrorMessages.getErrorString(this, geofencingEvent.getErrorCode());
        // Log.e(TAG, errorMessage);
        return;/*from w  w w  .  ja v a  2s  .  c  o  m*/
    }

    int geofenceTransition = geofencingEvent.getGeofenceTransition();
    int smsDb = 0;
    int wifiDb = 0;
    int silDb = 0;
    int RemDb = 0;
    int blueDb = 0;
    int carDb = 0;
    String remMsgDb = null;
    int RemDb_carPark = 0;
    String remMsgDb_carPark = null;
    //Creating database object to fetch values
    ManageDB autodb = new ManageDB(this);
    autodb.open();

    if (!CarParkService.car_flag_static) {
        Cursor cursor = autodb.getEntry(ActionFragment.geoFence);
        cursor.moveToFirst();

        //System.out.println("cursor count"+cursor.getCount());

        //Log.i("logcat",cursor.getString(0)+", "+cursor.getString(1)+" , "+cursor.getString(2));
        smsDb = cursor.getInt(5);
        wifiDb = cursor.getInt(6);
        silDb = cursor.getInt(7);
        RemDb = cursor.getInt(8);
        remMsgDb = cursor.getString(9);
        blueDb = cursor.getInt(10);
        carDb = cursor.getInt(11);

        cursor.close();

    } else {
        Cursor cursor_carpark = autodb.getEntryCarPark(CarParkService.geofence_carpark);
        cursor_carpark.moveToFirst();

        carDb = cursor_carpark.getInt(11);
        remMsgDb_carPark = cursor_carpark.getString(9);

        cursor_carpark.close();
    }
    autodb.close();

    //Checking Enter/Exit transitions
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
        String geofenceTransitionDetails = getGeofenceTransitionDetails(this, geofenceTransition,
                triggeringGeofences);
        System.out.println("smsdb" + smsDb);
        if (RemDb == 1) {
            sendNotification(geofenceTransitionDetails, remMsgDb);
        }
        if (carDb == 1) {
            sendNotification(geofenceTransitionDetails, remMsgDb_carPark);
        }
        //Perform actions if set on Actions set
        if (silDb == 1) {
            Toast.makeText(this, "silent", Toast.LENGTH_SHORT).show();
            AudioManager am = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
            am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

        }

        if (wifiDb == 1) {
            WifiManager wifiManager = (WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(true);
        }

        if (blueDb == 1) {
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (!mBluetoothAdapter.isEnabled()) {
                mBluetoothAdapter.enable();
            }
        }
        if (smsDb == 1) {
            Toast.makeText(this, "inside sendsms()", Toast.LENGTH_SHORT).show();
            SmsManager sm = SmsManager.getDefault();
            System.out.printf("sm", sm);
            sm.sendTextMessage(ActionFragment.smsNo, null,
                    "Hi, I am in " + ActionFragment.geoFence.getNameLoc() + " now!!!!", null, null);
        }
        if (carDb == 1) {
            Intent mapIntent = new Intent(this, CarParkMap.class);
            mapIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

    } else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
        String geofenceTransitionDetails = getGeofenceTransitionDetails(this, geofenceTransition,
                triggeringGeofences);
        if (silDb == 1) {
            Toast.makeText(this, "general", Toast.LENGTH_SHORT).show();
            AudioManager am1 = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
            am1.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }

        if (wifiDb == 1) {
            WifiManager wifiManager1 = (WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE);
            wifiManager1.setWifiEnabled(false);
        }
        if (blueDb == 1) {
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (mBluetoothAdapter.isEnabled()) {
                mBluetoothAdapter.disable();
            }
        }
        if (smsDb == 1) {
            Toast.makeText(this, "inside sendsms()", Toast.LENGTH_SHORT).show();
            SmsManager sm = SmsManager.getDefault();
            System.out.printf("smsNo :" + ActionFragment.smsNo);
            //fetch
            sm.sendTextMessage(ActionFragment.smsNo, null,
                    "Hi, I am outside " + ActionFragment.geoFence.getNameLoc() + " now !!!", null, null);
        }
        if (RemDb == 1) {
            sendNotification(geofenceTransitionDetails, remMsgDb);
        }

    }

}

From source file:com.hijacker.InstallFirmwareDialog.java

void install(String firm_location, String util_location) {
    boolean start_airodump = false;
    if (getPIDs(PROCESS_AIRODUMP).size() > 0) {
        start_airodump = true;//from  w ww  . j  ava 2 s.c  o m
        stop(PROCESS_AIRODUMP);
    }
    WifiManager wifiManager = (WifiManager) getActivity().getApplicationContext()
            .getSystemService(Context.WIFI_SERVICE);
    wifiManager.setWifiEnabled(false);
    if (debug)
        Log.d("HIJACKER/InstFirmware", "Backing up firmware from " + firm_location);
    if (backup_cb.isChecked()) {
        if (new File(firm_backup_file).exists()) {
            Toast.makeText(getActivity(), R.string.backup_exists, Toast.LENGTH_SHORT).show();
        } else {
            shell.run("cp -n " + firm_location + "fw_bcmdhd.bin " + firm_backup_file);
            Toast.makeText(getActivity(), R.string.backup_created, Toast.LENGTH_SHORT).show();
        }
    }
    shell.done(); //clear any existing output
    shell = Shell.getFreeShell();

    if (debug) {
        Log.d("HIJACKER/InstFirmware", "Installing firmware in " + firm_location);
        Log.d("HIJACKER/InstFirmware", "Installing utility in " + util_location);
    }
    shell.run(busybox + " mount -o rw,remount,rw /system");
    extract("fw_bcmdhd.bin", firm_location);
    extract("nexutil", util_location);
    shell.run(busybox + " mount -o ro,remount,ro /system");

    Toast.makeText(getActivity(), R.string.installed_firm_util, Toast.LENGTH_SHORT).show();
    wifiManager.setWifiEnabled(true);

    if (start_airodump)
        Airodump.startClean();
}