找传奇、传世资源到传世资源站!

自动搜索连接wifi_

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

自动搜索连接wifi_ Android平台开发-第1张

package com.example.testforwifilinkdemo;

import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class LinkWifiActivity extends Activity {
	private TextView showWifiStateTextView;
	private Button linkBtn;
	private WifiAdmin wifiAdmin;
	private List<WifiConfiguration> configuratedList;
	private List<ScanResult> scanResultList;
	//当前连接的wifi
	private WifiInfo currentWifiInfo;

	//WifiConfigutate的SSID的格式为:"\""  wifi名称  "\""
	 private final static String CONFIGURATESSID = "\""  "lol"  "\"";
	 //ScanResult的SSID的格式为:"wifi名称"
	private final static String SCANSSID = "lol";

//	private final static String CONFIGURATESSID = "\""  "aWiFi"  "\"";
	// 表示是否在可连接范围内
	private boolean canConnectable = false;
	// 表示wifi是否已经配置过了
	private boolean isConfigurated = false;
	//判断是否连接上制定的wifi
	private boolean isConnected = false;
	private int networkId;
	private BroadcastReceiver wifiConnectReceiver;

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

		wifiAdmin = new WifiAdmin(LinkWifiActivity.this);

		wifiConnectReceiver = new WifiConnectReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		LinkWifiActivity.this.registerReceiver(wifiConnectReceiver, filter);

		showWifiStateTextView = (TextView) findViewById(R.id.showWifiStateTextView);
		linkBtn = (Button) findViewById(R.id.linkWifiBtn);

		linkBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// configuratedList = wifiAdmin.getConfiguratedList();
				configuratedList = wifiAdmin.getConfiguratedList();
				scanResultList = wifiAdmin.getScanResultList();
				if (scanResultList != null) {
					for (int i = 0; i < scanResultList.size(); i ) {
						System.out.println("scanResultList"  i  "----->"
								 scanResultList.get(i).SSID);
						if (scanResultList.get(i).SSID.equals(SCANSSID)) {
							canConnectable = true;
							break;
						}
					}
					if (canConnectable) {
						System.out.println("%%%%%在可连接范围内%%%%%%%%");

						if (configuratedList != null) {
							System.out.println("*********************");
							for (int j = 0; j < configuratedList.size(); j ) {
								System.out.println("configuratedList"  j
										 "------->"
										 configuratedList.get(j).SSID);
								if (configuratedList.get(j).SSID
										.equals(CONFIGURATESSID)) {
									isConfigurated = true;
									networkId = configuratedList.get(j).networkId;
									System.out
											.println("#########################"
													 j);
									break;
								}
								// isConfigurated = false;
							}
							System.out.println("%%%%%%%%%%%%%%%%%%%%%%"
									 isConfigurated);
							if (isConfigurated) {
								System.out.println("^^^^^^^连接前");
								wifiAdmin.connectWifi(networkId);
								System.out.println("#########连接后");
								System.out.println("isConnected--------->"  isConnected);
								while (!isConnected) {
									System.out
											.println("*************连接上指定wifi*************");
									break;
								}
								System.out.println("*****************************");
							}
						}
					} else {
						System.out.println("$$$$$$$$$$不在可连接范围内$$$$$$$$$$$$$");
					}
				}
			}
		});
	}

	class WifiConnectReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
				ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
				NetworkInfo notewokInfo = manager.getActiveNetworkInfo();
				if (notewokInfo != null) {
					System.out.println("notewokInfo.getExtraInfo()------->"
							 notewokInfo.getExtraInfo());
					currentWifiInfo = wifiAdmin.getCurrentWifiInfo();
					System.out.println("currentWifiInfo.getSSID()----->"
							 currentWifiInfo.getSSID());
					if (currentWifiInfo.getSSID().equals(CONFIGURATESSID)) {
						isConnected = true;
					}
				} else {
					System.out.println("notewokInfo is null");
				}

			}
		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		LinkWifiActivity.this.unregisterReceiver(wifiConnectReceiver);
	}

}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复