• android代码设置、打开WLAN wifi热点及热点的连接


    其实创建热点很简单,先获取到wifi的服务,再配置热点名称、密码等等,然后再通过反射打开它就OK了。

      下面我们看看创建热点的代码实现:

    这一段是开启WLAN热点,并可以指定好它的热点名和密码

    支行后,这一段程序可以开启WLAN和同时自动关闭WIFI。

    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热点

    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文件的生成。

    还有配置权限。

    <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>  
    

      

  • 相关阅读:
    .NET 回归
    Smart ORM v0.4.1开发计划
    计算4的1万次方的结果
    GMS Modem 短信收发组件
    计划任务的编成实现
    告别2007,展望2008
    Smart ORM v0.3发布(完全面向对象的轻量级ORM工具)
    Java中委托事件模型 (转)
    刚刚开通了博客,写点东西
    ASP.NET弹出对话框(转)
  • 原文地址:https://www.cnblogs.com/killiancheung/p/4758199.html
Copyright © 2020-2023  润新知