Java 类android.net.wifi.SupplicantState 实例源码

项目:PasswordSafe-AndroidClient    文件:AccountListViewController.java   
@Override
public void onReceive(Context context, Intent intent) {
    _logger.Debug("_wifiStateReceiver onReceive");

    String action = intent.getAction();
    if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
        SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
        if ((SupplicantState.isValidState(state) && state == SupplicantState.COMPLETED)) {
            _logger.Debug("Successfully connected WIFI completed!");

            if (_sendDataController.GetSelectedServerIp() == null) {
                SearchForServer();
            }
        }
    } else {
        _logger.Warn(String.format("Action is %s", action));
    }
}
项目:TPlayer    文件:WifiManagerStub.java   
private static WifiInfo createWifiInfo() throws Exception {
    WifiInfo info = mirror.android.net.wifi.WifiInfo.ctor.newInstance();
    IPInfo ip = getIPInfo();
    InetAddress address = (ip != null ? ip.addr : null);
    mirror.android.net.wifi.WifiInfo.mNetworkId.set(info, 1);
    mirror.android.net.wifi.WifiInfo.mSupplicantState.set(info, SupplicantState.COMPLETED);
    mirror.android.net.wifi.WifiInfo.mBSSID.set(info, VASettings.Wifi.BSSID);
    mirror.android.net.wifi.WifiInfo.mMacAddress.set(info, VASettings.Wifi.MAC);
    mirror.android.net.wifi.WifiInfo.mIpAddress.set(info, address);
    mirror.android.net.wifi.WifiInfo.mLinkSpeed.set(info, 65);
    if (Build.VERSION.SDK_INT >= 21) {
        mirror.android.net.wifi.WifiInfo.mFrequency.set(info, 5000); // MHz
    }
    mirror.android.net.wifi.WifiInfo.mRssi.set(info, 200); // MAX_RSSI
    if (mirror.android.net.wifi.WifiInfo.mWifiSsid != null) {
        mirror.android.net.wifi.WifiInfo.mWifiSsid.set(info, WifiSsid.createFromAsciiEncoded.call(VASettings.Wifi.SSID));
    } else {
        mirror.android.net.wifi.WifiInfo.mSSID.set(info, VASettings.Wifi.SSID);
    }
    return info;
}
项目:susurrus-android-app    文件:WifiDirectService.java   
/**
 * Checks if the user is already connected to a wifi access point.
 * @return True, if connected.
 */
public boolean isAlreadyConnected() {
    // get an instance of the wifi-manager and information of the current access point
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    SupplicantState supplicantState = wifiInfo.getSupplicantState();

    // does the supplicant wifi-cli have a completed-state?
    if(SupplicantState.COMPLETED.equals(supplicantState)) {
        // completed state, connected to access point
        Log.d(LOG_TAG, "Already connected to an access point");

        return true;
    }
    else {
        return false;
    }
}
项目:AndroidSettingDemoAP    文件:WifiEnabler.java   
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
        handleWifiStateChanged(intent.getIntExtra(
                WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN));
    } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
        if (!mConnected.get()) {
            handleStateChanged(WifiInfo.getDetailedStateOf((SupplicantState)
                    intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE)));
        }
    } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
        NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(
                WifiManager.EXTRA_NETWORK_INFO);
        mConnected.set(info.isConnected());
        handleStateChanged(info.getDetailedState());
    }
}
项目:rx-receivers    文件:RxWifiManagerTest.java   
@SuppressWarnings("ResourceType") @Test //
public void supplicantStateChanges() throws IllegalAccessException, InstantiationException {
  Application application = RuntimeEnvironment.application;

  TestSubscriber<SupplicantStateChangedEvent> o = new TestSubscriber<>();
  RxWifiManager.supplicantStateChanges(application).subscribe(o);
  o.assertValues();

  Intent intent1 = new Intent(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION) //
      .putExtra(EXTRA_NEW_STATE, (Parcelable) SupplicantState.INACTIVE)
      .putExtra(EXTRA_SUPPLICANT_ERROR, ERROR_AUTHENTICATING);
  application.sendBroadcast(intent1);
  SupplicantStateChangedEvent event1 =
      SupplicantStateChangedEvent.create(SupplicantState.INACTIVE, ERROR_AUTHENTICATING);
  o.assertValues(event1);

  Intent intent2 = new Intent(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION) //
      .putExtra(EXTRA_NEW_STATE, (Parcelable) SupplicantState.ASSOCIATING)
      .putExtra(EXTRA_SUPPLICANT_ERROR, -1);
  application.sendBroadcast(intent2);
  SupplicantStateChangedEvent event2 =
      SupplicantStateChangedEvent.create(SupplicantState.ASSOCIATING, -1);
  o.assertValues(event1, event2);
}
项目:ghetto-unlock    文件:GhettoTrustAgentSettings.java   
private void updateTrust(String trustedSsid) {
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo == null) {
        return;
    }

    if (wifiInfo.getSSID() == null) {
        return;
    }

    if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
        if (trustedSsid.equals(wifiInfo.getSSID())) {
            GhettoTrustAgent.sendGrantTrust(this, "GhettoTrustAgent", TRUST_DURATION_30SECS,
                    false);
        }
        else {
            Log.d(TAG, "Found insecure SSID: " + trustedSsid);
            GhettoTrustAgent.sendRevokeTrust(this);
        }
    } else {
        Log.d(TAG, "Disconnected from Wifi: " + trustedSsid);
        GhettoTrustAgent.sendRevokeTrust(this);
    }
}
项目:androidwisprclient    文件:NetworkScanReceiver.java   
private boolean isAlreadyConnected(WifiManager wm) {
    boolean alreadyConnected = false;
    WifiInfo connectionInfo = wm.getConnectionInfo();
    if (connectionInfo != null) {
        SupplicantState supplicantState = connectionInfo.getSupplicantState();
        if (supplicantState != null) {
            alreadyConnected = supplicantState.equals(SupplicantState.ASSOCIATING)
                    || supplicantState.equals(SupplicantState.ASSOCIATED)
                    || supplicantState.equals(SupplicantState.COMPLETED)
                    || supplicantState.equals(SupplicantState.FOUR_WAY_HANDSHAKE)
                    || supplicantState.equals(SupplicantState.GROUP_HANDSHAKE);
        }
    }

    return alreadyConnected;
}
项目:persontracker    文件:UpdaterInstallCommand.java   
@Override
public void execute(Context service) 
{
       delegate.setInstallingState(IndicatorState.ACTIVE, 0, service.getString(R.string.if_ardrone_led_green_reset_wifi_connection));

       boolean connected = false;
       WifiManager mgr = (WifiManager) service.getSystemService(Context.WIFI_SERVICE);

       while (!connected) {
           WifiInfo info = mgr.getConnectionInfo();

           SupplicantState state = info.getSupplicantState();

           if (state == SupplicantState.COMPLETED ||
               state == SupplicantState.DORMANT) {
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                  break;
               }
               break;
           }
       }

       delegate.setInstallingState(IndicatorState.ACTIVE, 0, service.getString(R.string.if_ardrone_led_green_reset_wifi_connection));
}
项目:android-wear-gopro-remote    文件:WifiIntentReceiver.java   
@Override
public void onReceive(Context context, Intent intent) {

    final String action = intent.getAction();
    if(BuildConfig.DEBUG) Logger.debug(TAG, "onReceive:" + action);

    if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION .equals(action)) {
        SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
        if(state == SupplicantState.COMPLETED) {
            final WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
            if(wifiManager != null) {
                WifiInfo info = wifiManager.getConnectionInfo();
                if (info != null) {
                    mContext = context;
                    onWiFiConnected(info.getSSID());
                }
            }
        }
    }
}
项目:PrivacyStreams    文件:DeviceUtils.java   
/**
 * 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;
}
项目:LucaHome-AndroidApplication    文件:WIFIReceiver.java   
@Override
public void onReceive(final Context context, Intent intent) {
    String action = intent.getAction();
    if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
        SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
        if (!(SupplicantState.isValidState(state) && state == SupplicantState.COMPLETED)) {
            Logger.getInstance().Warning(TAG, "Not yet a valid connection!");
            return;
        }
    }

    new Handler().postDelayed(() -> {
        BroadcastController broadcastController = new BroadcastController(context);
        if (new NetworkController(context).IsHomeNetwork(SettingsController.getInstance().GetHomeSsid())) {
            if (!new AndroidSystemController(context).IsServiceRunning(MainService.class)) {
                Intent startMainServiceIntent = new Intent(context, MainService.class);
                startMainServiceIntent.putExtra(MainService.MainServiceOnStartCommandBundle, true);
                context.startService(startMainServiceIntent);
            } else {
                broadcastController.SendSimpleBroadcast(MainService.MainServiceStartDownloadAllBroadcast);
            }
            broadcastController.SendSimpleBroadcast(NetworkController.WIFIReceiverInHomeNetworkBroadcast);
        } else {
            TemperatureService.getInstance().CloseNotification();
            WirelessSocketService.getInstance().CloseNotification();
            WirelessSwitchService.getInstance().CloseNotification();
            broadcastController.SendSimpleBroadcast(NetworkController.WIFIReceiverNoHomeNetworkBroadcast);
        }
    }, 10 * 1000);
}
项目:mobly-bundled-snippets    文件:JsonSerializer.java   
private JSONObject serializeWifiInfo(WifiInfo data) throws JSONException {
    JSONObject result = new JSONObject(mGson.toJson(data));
    result.put("SSID", trimQuotationMarks(data.getSSID()));
    for (SupplicantState state : SupplicantState.values()) {
        if (data.getSupplicantState().equals(state)) {
            result.put("SupplicantState", state.name());
        }
    }
    return result;
}
项目:localcloud_fe    文件:WifiWizard.java   
/**
 *    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;
    }
}
项目:localcloud_fe    文件:WifiWizard.java   
/**
 *    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;
    }
}
项目:localcloud_fe    文件:WifiWizard.java   
/**
 *    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;
    }
}
项目:JinsMemeBRIDGE-Android    文件:RemoConfigFragment.java   
private void startScan() {
  SupplicantState wifiState = remoController.getWifiState();
  Log.d(TAG, "startScan: " + wifiState);

  if (wifiState != SupplicantState.COMPLETED) {
    changeState(State.WIFI_ERROR);
    showWifiErrorDialog();
    return;
  } else {
    changeState(State.SCAN);
    remoController.startDiscovery();
    deviceMap = new HashMap<String, String>();
    showScanDialog();
  }
}
项目:Sense-Hub-Android-Things    文件:WifiSta.java   
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION .equals(action)) {
        SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
        if (SupplicantState.isValidState(state)
                && state == SupplicantState.COMPLETED) {

            mIsConnected = checkConnectedToDesiredWifi();
            if(mCallback != null){
                if(mIsConnected){
                    mCallback.onSuccess();
                }
                else{
                    mCallback.onFailed();
                }
            }

        }
        else if(SupplicantState.isValidState(state)
                && state == SupplicantState.DISCONNECTED){
            Log.d(mTAG, "Wifi STA is disconnected");
            WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
            wifiManager.reconnect();
        }
    }
}
项目:RxAndroidTBP    文件:NetInfo.java   
public SupplicantState getSupplicantState() {
    return info.getSupplicantState();
}
项目:LucaHome-AndroidApplication    文件:WIFIReceiver.java   
@Override
public void onReceive(final Context context, Intent intent) {
    String action = intent.getAction();
    if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
        SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
        if (!(SupplicantState.isValidState(state) && state == SupplicantState.COMPLETED)) {
            Logger.getInstance().Warning(TAG, "Not yet a valid connection!");
            return;
        }
    }

    new Handler().postDelayed(() -> {
        BroadcastController broadcastController = new BroadcastController(context);
        if (new NetworkController(context).IsHomeNetwork(SettingsController.getInstance().GetHomeSsid())) {
            if (!new AndroidSystemController(context).IsServiceRunning(MainService.class)) {
                Intent startMainServiceIntent = new Intent(context, MainService.class);
                startMainServiceIntent.putExtra(MainService.MainServiceOnStartCommandBundle, true);
                context.startService(startMainServiceIntent);
            } else {
                broadcastController.SendSimpleBroadcast(MainService.MainServiceStartDownloadAllBroadcast);
            }
            broadcastController.SendSimpleBroadcast(NetworkController.WIFIReceiverInHomeNetworkBroadcast);
        } else {
            TemperatureService.getInstance().CloseNotification();
            WirelessSocketService.getInstance().CloseNotification();
            WirelessSwitchService.getInstance().CloseNotification();
            broadcastController.SendSimpleBroadcast(NetworkController.WIFIReceiverNoHomeNetworkBroadcast);
        }
    }, 10 * 1000);
}
项目:Cyber-Connect-Android    文件:LoginController.java   
public static boolean isConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    SupplicantState supplicantState = wifiInfo.getSupplicantState();
    if (supplicantState == SupplicantState.COMPLETED) {
        if (netInfo != null && netInfo.isConnected())
            return true;

    }
    return false;
}
项目:ip-widget    文件:MyWidget.java   
private  String getWifiIp() {
    WifiManager mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();
    Log.e("IP in Mask Integer", mWifiInfo.getIpAddress() + "");
    Log.e("IP Address", intToIP(mWifiInfo.getIpAddress()) + "");
    Log.e("ip-widget", "Supplicant state" + mWifiInfo.getSupplicantState());

    if(mWifiManager.isWifiEnabled() && (mWifiInfo.getSupplicantState() == SupplicantState.ASSOCIATED || mWifiInfo.getSupplicantState() == SupplicantState.COMPLETED)) {
        return mWifiInfo.getSSID() + " - " + intToIP(mWifiInfo.getIpAddress());
    }

    return "Disconnected";
}
项目:CordovaWifiPlugin    文件:WifiWizard.java   
/**
 *    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.setWifiEnabled(true);
        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;
    }
}
项目:PMCADemo    文件:WifiActivity.java   
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wifi);

    textView = (TextView) findViewById(R.id.logView);
    webView = (WebView) findViewById(R.id.webView);

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

    wifiStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            wifiStateChanged(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN));
        }
    };

    supplicantStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            networkStateChanged(WifiInfo.getDetailedStateOf((SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE)));
        }
    };

    networkStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            networkStateChanged(((NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)).getDetailedState());
        }
    };

    httpServer = new HttpServer();
}
项目:connectordb-android    文件:DatapointCache.java   
/**
 * The ssid of the currently connected network.
 */
public String getSSID() {
    WifiManager wifiManager = (WifiManager) context.getSystemService (Context.WIFI_SERVICE);
    WifiInfo info = wifiManager.getConnectionInfo ();
    if (info.getSupplicantState() == SupplicantState.COMPLETED) {
        String ssid = info.getSSID();
        // https://code.google.com/p/android/issues/detail?id=43336
        if (ssid.equals("0x") || ssid.equals("<unknown ssid>")) {
            return "";
        }
        return ssid;
    }
    return "";
}
项目:qksms    文件:DisconnectWifi.java   
@Override
public void onReceive(Context c, Intent intent) {
    // if wifi tries to connect while we are sending an MMS, disable it until that message is done sending
    WifiManager wifi = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
    if (!intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE).toString().equals(SupplicantState.SCANNING))
        wifi.disconnect();
}
项目:csploit    文件:WifiScannerActivity.java   
@Override
public void onReceive(Context context, Intent intent){
  SupplicantState state = intent.getParcelableExtra(EXTRA_NEW_STATE);

  if(state != null){
    if(state.equals(SupplicantState.COMPLETED)){
      onSuccessfulConnection();
    } else if(state.equals(SupplicantState.DISCONNECTED)){
      onFailedConnection();
    }
  }
}
项目:AndroidSettingDemoAP    文件:WifiSettings.java   
private void handleEvent(Context context, Intent intent) {
    String action = intent.getAction();
    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
        updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                WifiManager.WIFI_STATE_UNKNOWN));
    } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action) ||
            WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION.equals(action) ||
            WifiManager.LINK_CONFIGURATION_CHANGED_ACTION.equals(action))
    {
        updateAccessPoints();
    } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
        // Ignore supplicant state changes when network is connected
        // TODO: we should deprecate SUPPLICANT_STATE_CHANGED_ACTION and
        // introduce a broadcast that combines the supplicant and network
        // network state change events so the apps dont have to worry about
        // ignoring supplicant state change when network is connected
        // to get more fine grained information.
        SupplicantState state = (SupplicantState) intent.getParcelableExtra(
                WifiManager.EXTRA_NEW_STATE);
        if (!mConnected.get() && SupplicantState.isHandshakeState(state)) {
            // && SupplicantState.isHandshakeState(state)) {
            updateConnectionState(WifiInfo.getDetailedStateOf(state));
        }
    } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
        NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(
                WifiManager.EXTRA_NETWORK_INFO);
        mConnected.set(info.isConnected());
        // changeNextButtonState(info.isConnected());
        updateAccessPoints();
        updateConnectionState(info.getDetailedState());

    } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) {
        updateConnectionState(null);
    }
}
项目:rx-receivers    文件:RxWifiManager.java   
/** TODO: docs. */
@CheckResult @NonNull //
public static Observable<SupplicantStateChangedEvent> //
supplicantStateChanges(@NonNull final Context context) {
  checkNotNull(context, "context == null");
  IntentFilter filter = new IntentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
  return RxBroadcastReceiver.create(context, filter)
      .map(new Func1<Intent, SupplicantStateChangedEvent>() {
        @Override public SupplicantStateChangedEvent call(Intent intent) {
          SupplicantState newState = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
          int error = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 0);
          return SupplicantStateChangedEvent.create(newState, error);
        }
      });
}
项目:CordovaWifiPlugin    文件:WifiWizard.java   
/**
 *    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.setWifiEnabled(true);
        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;
    }
}
项目:timetracker    文件:AutoRecorder.java   
@Override
public void onReceive(Context context, Intent intent) {
    //abort if user disabled notifications in preferences
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    boolean notifyPref = sharedPref.getBoolean(SettingsFragment.KEY_PREF_NOTIFY, false);
    if (!notifyPref)
        return;

    // abort if intent signifies only an intermediate step while connecting
    if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
        SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
        switch (state) {
            case COMPLETED:
            case DISCONNECTED:
                // continue
                break;
            default:
                // don't act on any other events
                return;
        }
    }


    if (taskSuggestor == null)
        taskSuggestor = new MainTaskSuggestor();

    if (mBound) {
        action();
    } else {
        // need to connect TaskRecorderService first
        bindTaskRecorderService(context);
    }
}
项目:WifiWizard    文件:WifiWizard.java   
/**
 *    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;
    }
}
项目:mpd-control    文件:WifiConnectionSettingsFragment.java   
protected void initWifi(final Preference pref, Context context)
{
    receiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(final Context context, Intent intent) 
        {
            String action = intent.getAction();

            if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) 
            {
                int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
                int oldstate = intent.getIntExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
                if (oldstate != state)
                {
                    emptyList(pref);
                    if (state == WifiManager.WIFI_STATE_ENABLED)
                    {
                        fillList(pref, context);
                    }
                }
            } 
            else if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) 
            {
                 SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
                 if (state == SupplicantState.COMPLETED)
                 {
                     selectActive(pref, context);
                 }
            }
        }
    };      

    context.registerReceiver(receiver, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
    context.registerReceiver(receiver, new IntentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION));
}
项目:androidwebserver    文件:ServerService.java   
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());
    }
}
项目:android-vlc-remote    文件:PickServerActivity.java   
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;
}
项目:wifikeyguard    文件:WifiStateReceiver.java   
@Override
public void onReceive(final Context context, final Intent intent) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    final WifiInfo info = wifiManager.getConnectionInfo();

    if (intent.getAction().equals(ACTION_NETWORK_TOGGLED)) {
        final String extra_ssid = intent.getStringExtra(EXTRA_SSID);
        if (!extra_ssid.equals(WifiPreferenceUtil.normalizeSSID(info.getSSID()))) {
            Log.d(TAG, "not connected to network [ " + extra_ssid + " ]");
            return;
        }
    }

    if (info.getSupplicantState() != SupplicantState.COMPLETED) {
        Log.d(TAG, "Wifi is not connected.");
        lockKeyguard(context);
        return;
    }

    Log.d(TAG, "Wifi is connected.");
    final WifiPreferenceUtil util = new WifiPreferenceUtil(context);
    final String ssid = info.getSSID();
    if (util.isKeyguardDisabledForNetwork(ssid)) {
        Log.d(TAG, "Unlocking keyguard for SSID: [ " + ssid + " ].");
        unlockKeyguard(context);
    } else {
        Log.d(TAG, "Locking keyguard for SSID: [ " + ssid + " ].");
        lockKeyguard(context);
    }
}
项目:BibSearch    文件:WifiReceiver.java   
@Override
public void onReceive(Context context, Intent intent) {
  if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
    handleChange(
        (SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE),
        intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR));
  } else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
    handleNetworkStateChanged((NetworkInfo) intent.getParcelableExtra(
        WifiManager.EXTRA_NETWORK_INFO));
  } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
    ConnectivityManager con = (ConnectivityManager) parent.getSystemService(
        Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] s = con.getAllNetworkInfo();
    for (NetworkInfo i : s){
      if (i.getTypeName().contentEquals("WIFI")){
        NetworkInfo.State state = i.getState();
        String ssid = mWifiManager.getConnectionInfo().getSSID();

        if (state == NetworkInfo.State.CONNECTED && ssid != null){
          mWifiManager.saveConfiguration();
          String label = parent.getString(R.string.wifi_connected);
          statusView.setText(label + '\n' + ssid);
          Runnable delayKill = new Killer(parent);
          delayKill.run();
        }
        if (state == NetworkInfo.State.DISCONNECTED){
          Log.d(TAG, "Got state Disconnected for ssid: " + ssid);
          parent.gotError();
        }
      }
    }
  }
}
项目:WifiUtils    文件:WifiConnectionReceiver.java   
@Override
public void onReceive(Context context, @NonNull Intent intent)
{
    final String action = intent.getAction();
    if (Objects.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION, action))
    {
        final SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
        final int supl_error = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);

        if (state == null)
        {
            handler.removeCallbacks(handlerCallback);
            mWifiConnectionCallback.errorConnect();
            return;
        }

        wifiLog("Connection Broadcast action: " + state);

        switch (state)
        {
            case COMPLETED:
            case FOUR_WAY_HANDSHAKE:
                if (isAlreadyConnected(mWifiManager, mScanResult != null ? mScanResult.BSSID : null))
                {
                    handler.removeCallbacks(handlerCallback);
                    mWifiConnectionCallback.successfulConnect();
                }
                break;
            case DISCONNECTED:
                if (supl_error == WifiManager.ERROR_AUTHENTICATING)
                {
                    wifiLog("Authentication error...");
                    handler.removeCallbacks(handlerCallback);
                    mWifiConnectionCallback.errorConnect();
                }
                else
                {
                    wifiLog("Disconnected. Re-attempting to connect...");
                    reEnableNetworkIfPossible(mWifiManager, mScanResult);
                }
        }
    }
}
项目:open-rmbt    文件:InformationCollector.java   
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);

                    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                        CellInformationWrapper cellInformationWrapper = new CellInformationWrapper(wifiInfo);
                        cellInformationWrapper.setActive(true);
                        this.lastCellInfos.add(cellInformationWrapper);
                        this.registeredCells.clear();
                        this.registeredCells.add(cellInformationWrapper);
                        this.cellInfos.add(cellInformationWrapper);
                    }
//                    Log.i(DEBUG_TAG, "Signals1: " + signals.toString());
                }
            }
        }
    }
项目:JinsMemeBRIDGE-Android    文件:RemoController.java   
public SupplicantState getWifiState() {
  WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  WifiInfo wifiInfo = wifiManager.getConnectionInfo();
  SupplicantState state = wifiInfo.getSupplicantState();
  return state;
}
项目:mosmetro-android    文件:NetworkReceiver.java   
public void onReceive(Context context, Intent intent) {
    this.context = context;
    this.intent = intent;

    // Stop if Intent is empty
    if (intent == null || intent.getAction() == null)
        return;

    // Stop if automatic connection is disabled in settings
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    if (!settings.getBoolean("pref_autoconnect", true))
        return;

    // If Wi-Fi is disabled, stop ConnectionService immediately
    WifiUtils wifi = new WifiUtils(context);
    if (!wifi.isEnabled()) {
        Logger.log(this, "Wi-Fi not enabled");
        stopService();
        return;
    }

    switch (intent.getAction()) {
        /**
         * Listen to all Wi-Fi state changes and start ConnectionService if Wi-Fi is connected
         * Also check SupplicantState for better results
         */
        case WifiManager.NETWORK_STATE_CHANGED_ACTION:
            SupplicantState state = wifi.getWifiInfo(intent).getSupplicantState();
            if (state == null) break;

            Logger.log(this, String.format(Locale.ENGLISH, "Intent: %s (%s)",
                    intent.getAction(), state.name()
            ));

            switch (state) {
                case COMPLETED:
                case ASSOCIATED: // This appears randomly between multiple CONNECTED states
                    startService();
                    break;
                case SCANNING: // Some devices do not report DISCONNECTED state so...
                case DISCONNECTED:
                    stopService();
                    break;
            }

            break;

        default:
            Logger.log(this, "Unknown Intent: " + intent.getAction());
    }
}