android的wifi操作及连接未配置过的wifi

PL361 发布于 2015/06/07 22:54
阅读 1K+
收藏 2
import java.util.BitSet;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class WifiActivity extends Activity {
    public static final BitSet NONE = null;
	public static final BitSet WPA = null;
	public static final Context context = null;
	public static final String TAG = null;
	/** Called when the activity is first created. */
	private Button startButton = null;
	private Button stopButton = null;
	private Button checkButton = null;
	private Button resultButton=null;
	private Button deletButton=null;
	private Button newButton=null;
	private WifiInfo mWifiInfo;
	private static WifiManager wifiManager = null;
	private DhcpInfo dhcpInfo;
	
	public Object list;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startButton = (Button)findViewById(R.id.startWifi);
        stopButton = (Button)findViewById(R.id.stopWifi);
        checkButton = (Button)findViewById(R.id.checkWifi);
        resultButton = (Button)findViewById(R.id.resultWifi);
        deletButton=(Button)findViewById(R.id.deletnetwork);
        newButton=(Button)findViewById(R.id.newnetwork);
        startButton.setOnClickListener(new StartWifiListener());
        stopButton.setOnClickListener(new StopWifiListener());
        checkButton.setOnClickListener(new CheckWifiListener());
        resultButton.setOnClickListener(new resultButtonListener());
        deletButton.setOnClickListener(new DeletNetworkListener());
        newButton.setOnClickListener(new NewButtonListener());
    }
    class StartWifiListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
			wifiManager.setWifiEnabled(true);
			
			System.out.println("wifi state 开--->" + wifiManager.getWifiState());
			Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
		}
    }
    class StopWifiListener implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
			wifiManager.setWifiEnabled(false);
			System.out.println("wifi state 关--->" + wifiManager.getWifiState());
			Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
		}
    
    }
    
    class NewButtonListener extends Thread implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			List<WifiConfiguration> mWifiConfiguration = null;  
			WifiConfiguration mScanResult;  
			WifiConfiguration isadd;
			wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
			List<WifiConfiguration> netlist = wifiManager.getConfiguredNetworks();
			//for(int j=0;j<netlist.size();j++)
			
				
				System.out.println("大小为:"+netlist.size() );
				System.out.println("第一个:"+netlist.get(0).SSID);
				System.out.println("第二个:"+netlist.get(1).SSID);
				isadd = CreateWifiInfo("PL", "panlei361", 3);
				 int wcgID = wifiManager.addNetwork(isadd);
			        boolean b = wifiManager.enableNetwork(wcgID, true);
			        System.out.println("addNetwork--" + wcgID);
			        System.out.println("enableNetwork--" + b);
			        try {
						sleep(10000);
						 getWifiGateWay();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			       
		           // pingWifiGateway(gatew);
			
////		
		}

    }
    public DhcpInfo getDhcpInfo() {
        return dhcpInfo = wifiManager.getDhcpInfo();
    }
    public static String getWifiGateWay() {
    	
    	 
        DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
        Log.v(" wifi ipAddress", Formatter.formatIpAddress(dhcpInfo.ipAddress) + "");
        Log.v("wifi gateway", Formatter.formatIpAddress(dhcpInfo.gateway) + "");
        Log.v("wifi dns1", Formatter.formatIpAddress(dhcpInfo.dns1) + "");
        Log.v("wifi dns2", Formatter.formatIpAddress(dhcpInfo.dns2) + "");
        String wifi_gateway = Formatter.formatIpAddress(dhcpInfo.gateway);
        return wifi_gateway;
    }
    public WifiConfiguration CreateWifiInfo(String SSID, String Password, int Type) {
        Log.i(TAG, "SSID:" + SSID + ",password:" + Password);
        WifiConfiguration config = new WifiConfiguration();
        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();
        config.SSID = "\"" + SSID + "\"";
 
      //  WifiConfiguration tempConfig = this.IsExsits(SSID);
 
   
 
        if (Type == 1) // WIFICIPHER_NOPASS
        {
            Log.i(TAG, "Type =1.");
            config.wepKeys[0] = "";
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            config.wepTxKeyIndex = 0;
        }
        if (Type == 2) // WIFICIPHER_WEP
        {
            Log.i(TAG, "Type =2.");
            config.hiddenSSID = true;
            config.wepKeys[0] = "\"" + Password + "\"";
            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            config.wepTxKeyIndex = 0;
        }
        if (Type == 3) // WIFICIPHER_WPA
        {
 
            Log.i(TAG, "Type =3.");
            config.preSharedKey = "\"" + Password + "\"";
 
            config.hiddenSSID = true;
            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            // config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            config.status = WifiConfiguration.Status.ENABLED;
        }
        return config;
    }
    class CheckWifiListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
			System.out.println("wifi state --->" + wifiManager.getWifiState());
			Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
		}
    	
    }
    class resultButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
			String wificontens= wifiManager.getScanResults().toString();
		
			//System.out.println("列表:"+wificontens);
			System.out.println("长度:"+wificontens.length());
			int i=0;
			int j=0;
			int number=0;
			String subwifiname;
			while(wificontens.indexOf(" SSID", i)>0)
			{
				 j = wificontens.indexOf(" SSID", i);
				 System.out.println("位置:"+j);
				 subwifiname = wificontens.substring(i, j);
				 number++;
				 System.out.println("第"+number+"个:"+subwifiname);
				 i = j+3;
			}
			
		
			
			//System.out.println("位置:"+wificontens.indexOf(" SSID", 0));
			//String subwifiname=wificontens.substring(0, 116);
			//System.out.println("第一个:"+subwifiname);
			
			//Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
		}
    }
    class DeletNetworkListener implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
			wifiManager.removeNetwork(2);
		}
    	
    }
    
}




加载中
返回顶部
顶部