Search in sources :

Example 91 with WifiManager

use of android.net.wifi.WifiManager in project RoMote by wseemann.

the class ConfigureDeviceFragment method getWirelessNetworkName.

private String getWirelessNetworkName(Context context) {
    String networkName = "";
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
        return networkName;
    }
    final NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
    if (isConnected && isWiFi) {
        final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
        networkName = connectionInfo.getSSID();
    }
    return networkName;
}
Also used : WifiManager(android.net.wifi.WifiManager) NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) WifiInfo(android.net.wifi.WifiInfo)

Example 92 with WifiManager

use of android.net.wifi.WifiManager in project AnExplorer by 1hakr.

the class ConnectionUtils method getLocalInetAddress.

public static InetAddress getLocalInetAddress(Context context) {
    if (!isConnectedToLocalNetwork(context)) {
        Log.e(TAG, "getLocalInetAddress called and no connection");
        return null;
    }
    if (isConnectedToWifi(context)) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        int ipAddress = wm.getConnectionInfo().getIpAddress();
        if (ipAddress == 0)
            return null;
        return intToInet(ipAddress);
    }
    try {
        Enumeration<NetworkInterface> netinterfaces = NetworkInterface.getNetworkInterfaces();
        while (netinterfaces.hasMoreElements()) {
            NetworkInterface netinterface = netinterfaces.nextElement();
            Enumeration<InetAddress> adresses = netinterface.getInetAddresses();
            while (adresses.hasMoreElements()) {
                InetAddress address = adresses.nextElement();
                // this is the condition that sometimes gives problems
                if (!address.isLoopbackAddress() && !address.isLinkLocalAddress())
                    return address;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : WifiManager(android.net.wifi.WifiManager) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException)

Example 93 with WifiManager

use of android.net.wifi.WifiManager in project AnExplorer by 1hakr.

the class ConnectionUtils method isConnectedToLocalNetwork.

public static boolean isConnectedToLocalNetwork(Context context) {
    boolean connected = false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    connected = ni != null && ni.isConnected() && (ni.getType() & (ConnectivityManager.TYPE_WIFI | ConnectivityManager.TYPE_ETHERNET)) != 0;
    if (!connected) {
        Log.d(TAG, "isConnectedToLocalNetwork: see if it is an WIFI AP");
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        try {
            Method method = wm.getClass().getDeclaredMethod("isWifiApEnabled");
            connected = (Boolean) method.invoke(wm);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (!connected) {
        Log.d(TAG, "isConnectedToLocalNetwork: see if it is an USB AP");
        try {
            for (NetworkInterface netInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
                if (netInterface.getDisplayName().startsWith("rndis")) {
                    connected = true;
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
    return connected;
}
Also used : WifiManager(android.net.wifi.WifiManager) SocketException(java.net.SocketException) NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) NetworkInterface(java.net.NetworkInterface) Method(java.lang.reflect.Method) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException)

Example 94 with WifiManager

use of android.net.wifi.WifiManager in project underlx by underlx.

the class MainService method onCreate.

@Override
public void onCreate() {
    creationDate = new Date();
    stateTickHandler = new Handler();
    PreferenceManager.setDefaultValues(this.getApplicationContext(), R.xml.notif_settings, false);
    api = API.getInstance();
    wfc = new WiFiChecker(this, (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE));
    wfc.setScanInterval(10000);
    if (networks.size() == 0) {
        loadNetworks();
    }
    SharedPreferences sharedPref = getSharedPreferences("settings", MODE_PRIVATE);
    sharedPref.registerOnSharedPreferenceChangeListener(generalPrefsListener);
    if (new Date().getTime() - sharedPref.getLong(PreferenceNames.LastAutoTopologyUpdateCheck, 0) > TimeUnit.HOURS.toMillis(2)) {
        SharedPreferences.Editor e = sharedPref.edit();
        e.putLong(PreferenceNames.LastAutoTopologyUpdateCheck, new Date().getTime());
        e.apply();
        checkForTopologyUpdates();
    }
    realmForListeners = Application.getDefaultRealmInstance(this);
    tripRealmResults = realmForListeners.where(Trip.class).findAll();
    tripRealmResults.addChangeListener(tripRealmChangeListener);
    feedbackRealmResults = realmForListeners.where(Feedback.class).findAll();
    feedbackRealmResults.addChangeListener(feedbackRealmChangeListener);
    pairManager = new PairManager(getApplicationContext());
    // pairManager.unpair();
    API.getInstance().setPairManager(pairManager);
    if (!pairManager.isPaired()) {
        pairManager.pairAsync();
    }
    synchronizer = new Synchronizer(getApplicationContext());
}
Also used : WifiManager(android.net.wifi.WifiManager) SharedPreferences(android.content.SharedPreferences) Handler(android.os.Handler) Date(java.util.Date)

Example 95 with WifiManager

use of android.net.wifi.WifiManager in project WifiUtils by ThanosFisherman.

the class ConnectorUtils method connectWps.

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
static void connectWps(@NonNull final WifiManager wifiManager, @NonNull final ScanResult scanResult, @NonNull String pin, long timeOutMillis, @NonNull final ConnectionWpsListener connectionWpsListener) {
    final WeakHandler handler = new WeakHandler();
    final WpsInfo wpsInfo = new WpsInfo();
    final Runnable handlerTimeoutRunnable = new Runnable() {

        @Override
        public void run() {
            wifiManager.cancelWps(null);
            wifiLog("Connection with WPS has timed out");
            cleanPreviousConfiguration(wifiManager, scanResult);
            connectionWpsListener.isSuccessful(false);
            handler.removeCallbacks(this);
        }
    };
    final WifiManager.WpsCallback wpsCallback = new WifiManager.WpsCallback() {

        @Override
        public void onStarted(String pin) {
        }

        @Override
        public void onSucceeded() {
            handler.removeCallbacks(handlerTimeoutRunnable);
            wifiLog("CONNECTED With WPS successfully");
            connectionWpsListener.isSuccessful(true);
        }

        @Override
        public void onFailed(int reason) {
            handler.removeCallbacks(handlerTimeoutRunnable);
            final String reasonStr;
            switch(reason) {
                case 3:
                    reasonStr = "WPS_OVERLAP_ERROR";
                    break;
                case 4:
                    reasonStr = "WPS_WEP_PROHIBITED";
                    break;
                case 5:
                    reasonStr = "WPS_TKIP_ONLY_PROHIBITED";
                    break;
                case 6:
                    reasonStr = "WPS_AUTH_FAILURE";
                    break;
                case 7:
                    reasonStr = "WPS_TIMED_OUT";
                    break;
                default:
                    reasonStr = String.valueOf(reason);
            }
            wifiLog("FAILED to connect with WPS. Reason: " + reasonStr);
            cleanPreviousConfiguration(wifiManager, scanResult);
            reenableAllHotspots(wifiManager);
            connectionWpsListener.isSuccessful(false);
        }
    };
    wifiLog("Connecting with WPS...");
    wpsInfo.setup = WpsInfo.KEYPAD;
    wpsInfo.BSSID = scanResult.BSSID;
    wpsInfo.pin = pin;
    wifiManager.cancelWps(null);
    if (!cleanPreviousConfiguration(wifiManager, scanResult))
        disableAllButOne(wifiManager, scanResult);
    handler.postDelayed(handlerTimeoutRunnable, timeOutMillis);
    wifiManager.startWps(wpsInfo, wpsCallback);
}
Also used : WifiManager(android.net.wifi.WifiManager) WpsInfo(android.net.wifi.WpsInfo) RequiresApi(android.support.annotation.RequiresApi)

Aggregations

WifiManager (android.net.wifi.WifiManager)329 WifiInfo (android.net.wifi.WifiInfo)80 WifiConfiguration (android.net.wifi.WifiConfiguration)76 IOException (java.io.IOException)38 Intent (android.content.Intent)27 SuppressLint (android.annotation.SuppressLint)26 ConnectivityManager (android.net.ConnectivityManager)23 Context (android.content.Context)22 NetworkInfo (android.net.NetworkInfo)21 Test (org.junit.Test)20 AccessPoint (com.android.settingslib.wifi.AccessPoint)17 IntentFilter (android.content.IntentFilter)16 Bundle (android.os.Bundle)16 ArrayList (java.util.ArrayList)16 NetworkPolicyEditor (com.android.settingslib.NetworkPolicyEditor)12 PowerManager (android.os.PowerManager)11 WifiEnterpriseConfig (android.net.wifi.WifiEnterpriseConfig)10 SocketException (java.net.SocketException)10 SharedPreferences (android.content.SharedPreferences)9 Method (java.lang.reflect.Method)9