如何在android 5 api 22中使用wifi配置和brodcastreciver连接到特定的热点(wifi)

时间:2018-10-28 11:46:11

标签: java android

我试图通过连接到特定的wifi和idid来理解android中的wifi部分,我的设备已连接到我成功配置的wifi,但是iam面临一个小问题,即我希望在连接失败和连接成功时显示一条消息在连接到已配置的wifi时可以为例如做出决定打开失败的第三个活动时打开第二个活动....我该怎么做

WIFICONFIGURATION.java类

public class WiFiConfiguration extends Activity {

Button btnnext;


public String networkSSID = null;
public String networkPass = null;

public Button ConnectButton;
private BroadcastReceiver broadcastReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_wifi_configuration);

    btnnext = (Button) findViewById(R.id.btnnext);
    ConnectButton = (Button) findViewById(R.id.ApartmentButton);




    ConnectButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            networkSSID = "helloworld";
            networkPass = "mypass";


            WifiConfiguration conf = new WifiConfiguration();
            conf.SSID = "\"" + networkSSID + "\"";   // Please note the quotes. String should contain ssid in quotes

            /* for wep*/
            //conf.wepKeys[0] = "\"" + networkPass + "\"";
            //conf.wepTxKeyIndex = 0;
            //conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            //conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

            /* for wpa*/
            conf.preSharedKey = "\"" + networkPass + "\"";

            /* for open network*/
            //conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

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


            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(wifiManager.NETWORK_STATE_CHANGED_ACTION);
            registerReceiver(broadcastReceiver, intentFilter);


            wifiManager.setWifiEnabled(true);

            wifiManager.addNetwork(conf);

            List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
            for (WifiConfiguration i : list) {
                if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                    Log.v("rht", "WifiConfiguration SSID " + i.SSID);

                    boolean isDisconnected = wifiManager.disconnect();
                    Log.v("rht", "isDisconnected : " + isDisconnected);

                    boolean isEnabled = wifiManager.enableNetwork(i.networkId, true);
                    Log.v("rht", "isEnabled : " + isEnabled);

                    boolean isReconnected = wifiManager.reconnect();
                    Log.v("rht", "isReconnected : " + isReconnected);

                    Toast.makeText(context, "Connection started", 400).show();

                    /*if (wifiManager.getConnectionInfo()==){
                        Toast.makeText(context, "Connection success", 400).show();



                }
                else {

                    Toast.makeText(context, "Connection fail", 400).show();*/
                }
                break;
            }


        }



    });


}


@Override
protected void onStart() {
    super.onStart();
    IntentFilter intentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
    this.registerReceiver(mWifiStateChangeReceiver, intentFilter);
}

@Override
protected void onStop() {
    super.onStop();
    this.unregisterReceiver(mWifiStateChangeReceiver);
}

private BroadcastReceiver mWifiStateChangeReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            String action = intent.getAction();
            if (!TextUtils.isEmpty(action)) {
                if (action.equalsIgnoreCase(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                    if (connectivityManager != null) {
                        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                        if (networkInfo != null) {
                            // just to be more sure, you can also check whether the connected APN as same as the required one
                            // by comparing APN names
                            if (networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
                                Toast.makeText(context, "Connection success", 400).show();

                                /*Intent i = new Intent(context, secondActivity.class);
                                startActivity(i);*/
                            }

                            else
                            {
                                Toast.makeText(context, "Connection fali", 400).show();

                            }
                        }
                    }
                }
            }
        }
    }

};

} }

0 个答案:

没有答案