Java Code Examples for com.thanosfisherman.wifiutils.WifiUtils

The following examples show how to use com.thanosfisherman.wifiutils.WifiUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: react-native-wifi-reborn   Source File: RNWifiModule.java    License: ISC License 5 votes vote down vote up
/**
 * Use this to connect with a wifi network.
 * Example:  wifi.findAndConnect(ssid, password, false);
 * The promise will resolve with the message 'connected' when the user is connected on Android.
 *
 * @param SSID     name of the network to connect with
 * @param password password of the network to connect with
 * @param isWep    only for iOS
 * @param promise  to send success/error feedback
 */
@ReactMethod
public void connectToProtectedSSID(@NonNull final String SSID, @NonNull final String password, final boolean isWep, final Promise promise) {
    final boolean locationPermissionGranted = PermissionUtils.isLocationPermissionGranted(context);
    if (!locationPermissionGranted) {
        promise.reject("location permission missing", "Location permission (ACCESS_FINE_LOCATION) is not granted");
        return;
    }

    final boolean isLocationOn = LocationUtils.isLocationOn(context);
    if (!isLocationOn) {
        promise.reject("location off", "Location service is turned off");
        return;
    }

    WifiUtils.withContext(context).connectWith(SSID, password).onConnectionResult(new ConnectionSuccessListener() {
        @Override
        public void success() {
            promise.resolve("connected");
        }

        @Override
        public void failed(@NonNull ConnectionErrorCode errorCode) {
            promise.reject("failed", "Could not connect to network");
        }
    }).start();
}
 
Example 2
Source Project: WifiUtils   Source File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 555);
    WifiUtils.enableLog(true);
    //TODO: CHECK IF LOCATION SERVICES ARE ON

    final Button buttonConnect = findViewById(R.id.button_connect);
    buttonConnect.setOnClickListener(v -> connectWithWpa());

    final Button buttonDisconnect = findViewById(R.id.button_disconnect);
    buttonDisconnect.setOnClickListener(v -> disconnect(v.getContext()));
}
 
Example 3
Source Project: WifiUtils   Source File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
private void connectWithWpa() {
    WifiUtils.withContext(getApplicationContext())
            .connectWith(SSID, PASSWORD)
            .setTimeout(40000)
            .onConnectionResult(successListener)
            .start();
}
 
Example 4
Source Project: WifiUtils   Source File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
private void disconnect(final Context context) {
    WifiUtils.withContext(context)
            .disconnect(new DisconnectionSuccessListener() {
                @Override
                public void success() {
                    Toast.makeText(MainActivity.this, "Disconnect success!", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void failed(@NonNull DisconnectionErrorCode errorCode) {
                    Toast.makeText(MainActivity.this, "Failed to disconnect: " + errorCode.toString(), Toast.LENGTH_SHORT).show();
                }
            });
}
 
Example 5
Source Project: DeAutherDroid   Source File: MainActivity.java    License: Apache License 2.0 4 votes vote down vote up
private void showScanResults() {

        View scanView = getLayoutInflater().inflate(R.layout.view_list, null);

        listView = scanView.findViewById(R.id.list);

        WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        if (manager != null) {
            manager.startScan();
        }

        Handler handlerX = new Handler();
        handlerX.post(new Runnable() {
            @Override
            public void run() {
                WifiUtils.withContext(getApplicationContext()).scanWifi(results -> {
                    List<String> arrayWIFIList = new ArrayList<>();
                    ScanResult result;
                    for (int i = 0; i < results.size(); i++) {
                        result = results.get(i);

                        Log.v(TAG, result.toString());
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            arrayWIFIList.add(i, result.SSID + "[" + (double) result.frequency / 1000 + "GHz" + "/" + result.level + "dBm" + "]"
                                    + result.capabilities + getChannelWidth(result.channelWidth));
                        } else {
                            arrayWIFIList.add(i, result.SSID + "[" + (double) result.frequency / 1000 + "GHz" + "/" + result.level + "dBm" + "]"
                                    + result.capabilities);
                        }
                    }

                    String[] stringArray = arrayWIFIList.toArray(new String[0]);

                    ArrayAdapter<?> adapter = new ArrayAdapter<Object>(MainActivity.this, R.layout.list_text, R.id.textList, stringArray);

                    adapter.notifyDataSetChanged();
                    listView.setAdapter(adapter);

                }).start();
                handlerX.postDelayed(this, TimeUnit.SECONDS.toMillis(10));
            }
        });

        if (Prefs.getInstance(getBaseContext()).isWIFIRand()) {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (listView.getChildCount() > 0) {
                        for (int i = 0; i < listView.getChildCount(); i++) {
                            SecureRandom random = new SecureRandom(); /*  Just some beautifying */
                            int color = Color.argb(255, random.nextInt(200), random.nextInt(200), random.nextInt(255));
                            ((TextView) listView.getChildAt(i)).setTextColor(color);
                        }
                    }
                    handler.postDelayed(this, 250);
                }
            }, 100);
        }


        if (!MainActivity.this.isFinishing()) {
            new AlertDialog.Builder(MainActivity.this)
                    .setView(scanView)
                    .setTitle("WiFi Scan-results (Local)")
                    .setCancelable(false)
                    .setOnDismissListener(dialog -> {

                    })
                    .setNeutralButton("Help", (dialog, which) -> new AlertDialog.Builder(MainActivity.this)
                            .setMessage(R.string.help_scan)
                            .setCancelable(false)
                            .setPositiveButton("Okay", null)
                            .show())
                    .setPositiveButton("Close / Stop Scan", null).show();
        }
    }
 
Example 6
Source Project: WifiUtils   Source File: MainActivity.java    License: Apache License 2.0 4 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void connectWithWps() {
    WifiUtils.withContext(getApplicationContext()).connectWithWps("d8:74:95:e6:f5:f8", "51362485").onConnectionWpsResult(this::checkResult).start();
}
 
Example 7
Source Project: WifiUtils   Source File: MainActivity.java    License: Apache License 2.0 4 votes vote down vote up
private void enableWifi() {
    WifiUtils.withContext(getApplicationContext()).enableWifi(this::checkResult);
    //or without the callback
    //WifiUtils.withContext(getApplicationContext()).enableWifi();
}