android代碼設置、打開WLAN wifi熱點及熱點的鏈接

其實建立熱點很簡單,先獲取到wifi的服務,再配置熱點名稱、密碼等等,而後再經過反射打開它就OK了。java

  下面咱們看看建立熱點的代碼實現:android

這一段是開啓WLAN熱點,並能夠指定好它的熱點名和密碼app

支行後,這一段程序能夠開啓WLAN和同時自動關閉WIFI。ide

package com.tel.lajoin.wifi.hotspot;

import java.lang.reflect.Method;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class HotspotActivity extends Activity {
	private WifiManager wifiManager;
	private Button open;
	private boolean flag=false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//獲取wifi管理服務
		wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		open=(Button)findViewById(R.id.open_hotspot);
		//經過按鈕事件設置熱點
		open.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//若是是打開狀態就關閉,若是是關閉就打開
				flag=!flag;
				setWifiApEnabled(flag);
			}
		});
	}

	// wifi熱點開關
	public boolean setWifiApEnabled(boolean enabled) {
		if (enabled) { // disable WiFi in any case
			//wifi和熱點不能同時打開,因此打開熱點的時候須要關閉wifi
			wifiManager.setWifiEnabled(false);
		}
		try {
			//熱點的配置類
			WifiConfiguration apConfig = new WifiConfiguration();
			//配置熱點的名稱(能夠在名字後面加點隨機數什麼的)
			apConfig.SSID = "YRCCONNECTION";
			//配置熱點的密碼
			apConfig.preSharedKey="12122112";
     			//經過反射調用設置熱點
			Method method = wifiManager.getClass().getMethod(
					"setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
			//返回熱點打開狀態
			return (Boolean) method.invoke(wifiManager, apConfig, enabled);
		} catch (Exception e) {
			return false;
		}
	}
}

 

下在一段是,去連一個WLAN熱點this

package com.tel.lajoin.wifiscan;

import java.util.ArrayList;
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.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;

public class MainActivity extends Activity {
	private List<ScanResult> wifiList;
	private WifiManager wifiManager;
	private List<String> passableHotsPot;
	private WifiReceiver wifiReceiver;
	private boolean isConnected=false;
	private Button connect;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}

	/* 初始化參數 */
	public void init() {
		setContentView(R.layout.main);
		connect=(Button)findViewById(R.id.connect);
		wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		wifiReceiver = new WifiReceiver();
		//經過按鈕事件搜索熱點
		connect.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				wifiManager.startScan();
			}
		});		
	}

	/* 監聽熱點變化 */
	private final class WifiReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			wifiList = wifiManager.getScanResults();
			if (wifiList == null || wifiList.size() == 0 || isConnected)
				return;
			onReceiveNewNetworks(wifiList);
		}
	}
	
	/*當搜索到新的wifi熱點時判斷該熱點是否符合規格*/
	public void onReceiveNewNetworks(List<ScanResult> wifiList){
		passableHotsPot=new ArrayList<String>();
		for(ScanResult result:wifiList){
			System.out.println(result.SSID);
			if((result.SSID).contains("YRCCONNECTION"))
				passableHotsPot.add(result.SSID);
		}
		synchronized (this) {
			connectToHotpot();
		}
	}
	
	/*鏈接到熱點*/
	public void connectToHotpot(){
		if(passableHotsPot==null || passableHotsPot.size()==0)
			return;
		WifiConfiguration wifiConfig=this.setWifiParams(passableHotsPot.get(0));
		int wcgID = wifiManager.addNetwork(wifiConfig);
	    boolean flag=wifiManager.enableNetwork(wcgID, true);
	    isConnected=flag;
		System.out.println("connect success? "+flag);
	}
	
	/*設置要鏈接的熱點的參數*/
	public WifiConfiguration setWifiParams(String ssid){
		WifiConfiguration apConfig=new WifiConfiguration();
		apConfig.SSID="\""+ssid+"\"";
		apConfig.preSharedKey="\"12122112\"";
		apConfig.hiddenSSID = true;
		apConfig.status = WifiConfiguration.Status.ENABLED;
		apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
		apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
		apConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
		apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
		apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
		apConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
		return apConfig;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		/*銷燬時註銷廣播*/
		unregisterReceiver(wifiReceiver);
	}
}

 

固然,若是要運行起來,還要在MANIFEST.XML中設定好定義的ACTIVITY類。以及相應的LAYOUT文件和R文件的生成。.net

還有配置權限。blog

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>"
	<!-- 設置容許改變配置信息的權�?-->  
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />  

     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>    
	 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>    
	 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>    
	 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>  
相關文章
相關標籤/搜索