Java Code Examples for android.net.wifi.WifiInfo#getSupplicantState()

The following examples show how to use android.net.wifi.WifiInfo#getSupplicantState() . 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 File: OBConnectionManager.java    From GLEXP-Team-onebillion with Apache License 2.0 6 votes vote down vote up
public String getCurrentWifiSSID()
{
    WifiManager wifiManager = (WifiManager) MainActivity.mainActivity.getApplicationContext().getSystemService(MainActivity.WIFI_SERVICE);
    WifiInfo info = wifiManager.getConnectionInfo ();
    String ssid = info.getSSID();
    if (info.getSupplicantState() != SupplicantState.COMPLETED)
    {
        MainActivity.log("OBConnectionManager:getCurrentWifiSSID. not connected to current wifi. returning null");
        return null;
    }
    if (ssid.charAt(0) == '"' && ssid.charAt(ssid.length() - 1) == '"')
    {
        return ssid.substring(1, ssid.length() - 1);
    }
    else
    {
        return ssid;
    }
}
 
Example 2
Source File: WifiConnector.java    From Android with Apache License 2.0 6 votes vote down vote up
@Override
   public void onReceive(Context context, Intent intent) {
	
if (!WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(intent.getAction())) {
           return;
    }				
	
       mLock.lock();
	
       WifiInfo info = mWifiManager.getConnectionInfo();
       if ( info.getNetworkId()==mNetworkID && info.getSupplicantState() == SupplicantState.COMPLETED ) {
           mIsConnnected = true;
           mCondition.signalAll();			
}
	
       mLock.unlock();	
   }
 
Example 3
Source File: ESPDevice.java    From esp-idf-provisioning-android with Apache License 2.0 5 votes vote down vote up
@RequiresPermission(allOf = {Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.ACCESS_WIFI_STATE})
private String fetchWiFiSSID() {

    String ssid = null;
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {

        ssid = wifiInfo.getSSID();
        ssid = ssid.replace("\"", "");
    }
    Log.e(TAG, "Returning ssid : " + ssid);
    return ssid;
}
 
Example 4
Source File: AddDeviceActivity.java    From esp-idf-provisioning-android with Apache License 2.0 5 votes vote down vote up
private String getWifiSsid() {

        String ssid = null;
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {

            ssid = wifiInfo.getSSID();
            ssid = ssid.replace("\"", "");
        }
        return ssid;
    }
 
Example 5
Source File: DeviceUtils.java    From PrivacyStreams with Apache License 2.0 5 votes vote down vote up
/**
 * Check whether WiFi is connected
 * @param context a Context instance
 * @return true if Wifi is connected
 */
// @RequiresPermission(value = Manifest.permission.ACCESS_WIFI_STATE)
public static boolean isWifiConnected(Context context) {
    WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (wifiManager == null || !wifiManager.isWifiEnabled()) return false;
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo == null || wifiInfo.getNetworkId() == -1) return false;
    return wifiInfo.getSupplicantState() == SupplicantState.ASSOCIATED;
}
 
Example 6
Source File: NetworkChangeReceiver.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
@Nullable
public static EventNetworkChange grabNetworkStatus(final Context context) {
    EventNetworkChange event = new EventNetworkChange();

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) return null;
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

    if (activeNetwork != null) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI && activeNetwork.isConnected()) {
            event.setWifiConnected(true);
            WifiManager wifiManager = (WifiManager) MainApp.instance().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            if (wifiManager != null) {
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
                    event.setSsid(wifiInfo.getSSID());
                }
                if (L.isEnabled(L.CORE))
                    log.debug("NETCHANGE: Wifi connected. SSID: " + event.connectedSsid());
            }
        }

        if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
            event.setMobileConnected(true);
            event.setRoaming(activeNetwork.isRoaming());
            if (L.isEnabled(L.CORE))
                log.debug("NETCHANGE: Mobile connected. Roaming: " + event.getRoaming());
        }
    } else {
        if (L.isEnabled(L.CORE))
            log.debug("NETCHANGE: Disconnected.");
    }

    lastEvent = event;
    return event;
}
 
Example 7
Source File: WifiWizard.java    From WifiWizard with Apache License 2.0 5 votes vote down vote up
/**
 *    This method connects a network.
 *
 *    @param    callbackContext        A Cordova callback context
 *    @param    data                JSON Array, with [0] being SSID to connect
 *    @return    true if network connected, false if failed
 */
private boolean connectNetwork(CallbackContext callbackContext, JSONArray data) {
    Log.d(TAG, "WifiWizard: connectNetwork entered.");
    if(!validateData(data)) {
        callbackContext.error("WifiWizard: connectNetwork invalid data");
        Log.d(TAG, "WifiWizard: connectNetwork invalid data.");
        return false;
    }
    String ssidToConnect = "";

    try {
        ssidToConnect = data.getString(0);
    }
    catch (Exception e) {
        callbackContext.error(e.getMessage());
        Log.d(TAG, e.getMessage());
        return false;
    }

    int networkIdToConnect = ssidToNetworkId(ssidToConnect);

    if (networkIdToConnect >= 0) {
        // We disable the network before connecting, because if this was the last connection before
        // a disconnect(), this will not reconnect.
        wifiManager.disableNetwork(networkIdToConnect);
        wifiManager.enableNetwork(networkIdToConnect, true);

        SupplicantState supState;
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        supState = wifiInfo.getSupplicantState();
        callbackContext.success(supState.toString());
        return true;

    }else{
        callbackContext.error("WifiWizard: cannot connect to network");
        return false;
    }
}
 
Example 8
Source File: ServerService.java    From androidwebserver with GNU General Public License v3.0 5 votes vote down vote up
public void startServer(Handler handler, String documentRoot, int port) {
	try {
		isRunning = true;
		WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		
		String ipAddress = intToIp(wifiInfo.getIpAddress());

		if( wifiInfo.getSupplicantState() != SupplicantState.COMPLETED) {
			new AlertDialog.Builder(this).setTitle("Error").setMessage("Please connect to a WIFI-network for starting the webserver.").setPositiveButton("OK", null).show();
			throw new Exception("Please connect to a WIFI-network.");
		}
        
  server = new Server(handler, documentRoot, ipAddress, port, getApplicationContext());
  server.start();
  
     Intent i = new Intent(this, StartActivity.class);
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);

     updateNotifiction("Webserver is running on port " + ipAddress + ":" + port);
     
 	Message msg = new Message();
 	Bundle b = new Bundle();
 	b.putString("msg", "Webserver is running on port " + ipAddress + ":" + port);
 	msg.setData(b);
 	handler.sendMessage(msg);
 	
	} catch (Exception e) {
		isRunning = false;
		Log.e("Webserver", e.getMessage());
     updateNotifiction("Error: " + e.getMessage());
	}
}
 
Example 9
Source File: PickServerFragment.java    From android-vlc-remote with GNU General Public License v3.0 5 votes vote down vote up
private WifiInfo getConnectionInfo() {
    WifiManager manager = (WifiManager) getActivity().getSystemService(Activity.WIFI_SERVICE);
    WifiInfo info = manager.getConnectionInfo();
    if (info != null) {
        SupplicantState state = info.getSupplicantState();
        if (state.equals(SupplicantState.COMPLETED)) {
            return info;
        }
    }
    return null;
}
 
Example 10
Source File: PickServerActivity.java    From android-vlc-remote with GNU General Public License v3.0 5 votes vote down vote up
private WifiInfo getConnectionInfo() {
    Object service = getSystemService(WIFI_SERVICE);
    WifiManager manager = (WifiManager) service;
    WifiInfo info = manager.getConnectionInfo();
    if (info != null) {
        SupplicantState state = info.getSupplicantState();
        if (state.equals(SupplicantState.COMPLETED)) {
            return info;
        }
    }
    return null;
}
 
Example 11
Source File: WifiWizard2.java    From WifiWizard2 with Apache License 2.0 4 votes vote down vote up
/**
 * This method retrieves the WifiInformation for the (SSID or BSSID) currently connected network.
 *
 * @param callbackContext A Cordova callback context
 * @param basicIdentifier A flag to get BSSID if true or SSID if false.
 * @return true if SSID found, false if not.
 */
private boolean getWifiServiceInfo(CallbackContext callbackContext, boolean basicIdentifier) {    
  if (API_VERSION >= 23 && !cordova.hasPermission(ACCESS_FINE_LOCATION)) { //Android 9 (Pie) or newer
    requestLocationPermission(WIFI_SERVICE_INFO_CODE);
    bssidRequested = basicIdentifier;
    return true;
  } else {
    WifiInfo info = wifiManager.getConnectionInfo();

    if (info == null) {
      callbackContext.error("UNABLE_TO_READ_WIFI_INFO");
      return false;
    }

    // Only return SSID or BSSID when actually connected to a network
    SupplicantState state = info.getSupplicantState();
    if (!state.equals(SupplicantState.COMPLETED)) {
      callbackContext.error("CONNECTION_NOT_COMPLETED");
      return false;
    }

    String serviceInfo;
    if (basicIdentifier) {
      serviceInfo = info.getBSSID();
    } else {
      serviceInfo = info.getSSID();
    }

    if (serviceInfo == null || serviceInfo.isEmpty() || serviceInfo == "0x") {
      callbackContext.error("WIFI_INFORMATION_EMPTY");
      return false;
    }

    // http://developer.android.com/reference/android/net/wifi/WifiInfo.html#getSSID()
    if (serviceInfo.startsWith("\"") && serviceInfo.endsWith("\"")) {
      serviceInfo = serviceInfo.substring(1, serviceInfo.length() - 1);
    }

    callbackContext.success(serviceInfo);
    return true;
  }
}
 
Example 12
Source File: WifiHelper.java    From android-wear-gopro-remote with Apache License 2.0 4 votes vote down vote up
public static boolean connectToWifi(Context context, final String ssid, String password) {

        int networkId = -1;
        int c;

        final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wifiManager == null) {
            Logger.error(TAG, "No WiFi manager");
            return false;
        }

        List<WifiConfiguration> list;

        if (wifiManager.isWifiEnabled()) {
            list = wifiManager.getConfiguredNetworks();
        } else {
            if (!wifiManager.setWifiEnabled(true)) {
                Logger.error(TAG, "Enable WiFi failed");
                return false;
            }
            c = 0;
            do {
                Utils.sleep(500);
                list = wifiManager.getConfiguredNetworks();
            } while (list == null && ++c < 10);
        }

        if (list == null) {
            Logger.error(TAG, "Could not get WiFi network list");
            return false;
        }

        for (WifiConfiguration i : list) {
            if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
                networkId = i.networkId;
                break;
            }
        }

        WifiInfo info;
        if (networkId < 0) {
            WifiConfiguration conf = new WifiConfiguration();
            conf.SSID = "\"" + ssid + "\"";
            conf.preSharedKey = "\"" + password + "\"";
            networkId = wifiManager.addNetwork(conf);
            if (networkId < 0) {
                Logger.error(TAG, "New WiFi config failed");
                return false;
            }
        } else {
            info = wifiManager.getConnectionInfo();
            if (info != null) {
                if (info.getNetworkId() == networkId) {
                    if(Logger.DEBUG) Logger.debug(TAG, "Already connected to " + ssid);
                    return true;
                }
            }
        }

        if (!wifiManager.disconnect()) {
            Logger.error(TAG, "WiFi disconnect failed");
            return false;
        }

        if (!wifiManager.enableNetwork(networkId, true)) {
            Logger.error(TAG, "Could not enable WiFi.");
            return false;
        }

        if (!wifiManager.reconnect()) {
            Logger.error(TAG, "WiFi reconnect failed");
            return false;
        }

        c = 0;
        do {
            info = wifiManager.getConnectionInfo();
            if (info != null && info.getNetworkId() == networkId &&
                    info.getSupplicantState() == SupplicantState. COMPLETED &&  info.getIpAddress() != 0) {
                if(Logger.DEBUG) Logger.debug(TAG, "Successfully connected to %s %d", ssid, info.getIpAddress());
                return true;
            }
            Utils.sleep(500);
        } while (++c < 30);

        Logger.error(TAG, "Failed to connect to " + ssid);
        return false;
    }
 
Example 13
Source File: InformationCollector.java    From open-rmbt with Apache License 2.0 4 votes vote down vote up
private void getWiFiInfo()
    {
        initNetwork();
        if (wifiManager != null)
        {
            final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            fullInfo.setProperty("WIFI_SSID",
            String.valueOf(Helperfunctions.removeQuotationsInCurrentSSIDForJellyBean(wifiInfo.getSSID())));
            		
            /*
             * fullInfo.setProperty("WIFI_LINKSPEED",
             * String.valueOf(wifiInfo.getLinkSpeed()));
             */
            fullInfo.setProperty("WIFI_BSSID", String.valueOf(wifiInfo.getBSSID()));
            fullInfo.setProperty("WIFI_NETWORK_ID", String.valueOf(wifiInfo.getNetworkId()));
            /*
             * fullInfo.setProperty("WIFI_RSSI",
             * String.valueOf(wifiInfo.getRssi()));
             */
            final SupplicantState wifiState = wifiInfo.getSupplicantState();
            fullInfo.setProperty("WIFI_SUPPLICANT_STATE", String.valueOf(wifiState.name()));
            final DetailedState wifiDetail = WifiInfo.getDetailedStateOf(wifiState);
            fullInfo.setProperty("WIFI_SUPPLICANT_STATE_DETAIL", String.valueOf(wifiDetail.name()));
            
            if (getNetwork() == NETWORK_WIFI)
            {
                
                final int rssi = wifiInfo.getRssi();
                if (rssi != -1 && rssi >= ACCEPT_WIFI_RSSI_MIN)
                {
                    int linkSpeed = wifiInfo.getLinkSpeed();
                    if (linkSpeed < 0) {
                        linkSpeed = 0;
                    }
                    
                    final SignalItem signalItem = SignalItem.getWifiSignalItem(linkSpeed, rssi);
                    if (this.collectInformation) {
                        signals.add(signalItem);	
                    }
                    lastSignalItem.set(signalItem);
                    signal.set(rssi);
                    signalType.set(SINGAL_TYPE_WLAN);
//                    Log.i(DEBUG_TAG, "Signals1: " + signals.toString());
                }
            }
        }
    }