?
Wifi2Activity.java
?
?
package wifi2.com;import java.util.ArrayList;import java.util.List;import wifi2.com.WifiConnect.WifiCipherType;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.net.wifi.WifiConfiguration;import android.net.wifi.WifiManager;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ScrollView;import android.widget.TextView;import android.widget.Toast;public class Wifi2Activity extends Activity{ // 右侧滚动条按钮 private Button connect; private TextView Tsid; private TextView Tpwd; private String sid; private String pwd; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); connect = (Button) findViewById(R.id.connect); Tsid = (TextView) findViewById(R.id.sid); Tpwd = (TextView) findViewById(R.id.pwd); connect.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub sid = Tsid.getText().toString(); pwd = Tpwd.getText().toString(); //WifiAdmin wifiAdmin = new WifiAdmin(Wifi2Activity.this); WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiConfiguration wc = new WifiConfiguration(); wc.SSID = "\""+sid+"\""; //Milliwave Tech //判断并开启 wifi if (!wifi.isWifiEnabled()) { wifi.setWifiEnabled(true); } //\\ //-- 是否是开放式的不用密码------------------ //如果有密码 //wc.preSharedKey = "\""+pwd+"\""; // //wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); //如果是开发式的没有密码的 wc.wepKeys[0] = ""; wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); wc.wepTxKeyIndex = 0; //-- ============================== wc.hiddenSSID = true; wc.status = WifiConfiguration.Status.ENABLED; wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // TYPE = WIFICIPHER_WEP,WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID //wifi.saveConfiguration(); int res = wifi.addNetwork(wc); Toast.makeText(Wifi2Activity.this, "add Network returned " + res, Toast.LENGTH_LONG).show(); boolean b = wifi.enableNetwork(res, true); Toast.makeText(Wifi2Activity.this, "enableNetwork returned " + b, Toast.LENGTH_LONG).show(); //wifiAdmin.AddNetwork(); } }); } }?
?
?
main.xml 布局文件
?
?
?
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="SID" /> <EditText android:id="@+id/sid" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="PassWord" /> <EditText android:id="@+id/pwd" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/connect" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="连接Wifi" /></LinearLayout>?
?
判断 wifi 连接是否可用
?
?
?
public boolean isWifiConnect() { ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); return mWifi.isConnected(); }?
获取 android 的 ip
?
?
public String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { } return null; }?
?
enableNetwork ?后要做循环 处理 待 配置好 所有WIFI 信息
?
for (int i = 0; i < 6; i++) { if(isWifiConnect() == false || getLocalIpAddress() == null || getLocalIpAddress() == ""){ sleep(2000); }}
或者判断当前 WIFI 的 SSIWifiInfo info = wifi.getConnectionInfo();?
String ssid = info.getSSID();if(!ssid.contains("BroadlinkProv")){ cNewstatus = 0;}
?D
?
?
?
如果网络类型 只是 WPA
?
?
?
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);?
?
?
获取IP,MASK,DNS等 信息
?
package t3.com;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.widget.TextView;import android.net.*;import android.net.wifi.WifiManager;import android.text.format.Formatter;public class T3Activity extends Activity { public String s_dns1 ; public String s_dns2; public String s_gateway; public String s_ipAddress; public String s_leaseDuration; public String s_netmask; public String s_serverAddress; TextView info; DhcpInfo d; WifiManager wifii; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); wifii= (WifiManager) getSystemService(Context.WIFI_SERVICE); d=wifii.getDhcpInfo(); s_dns1="DNS 1: "+String.valueOf(d.dns1); s_dns2="DNS 2: "+String.valueOf(d.dns2); s_gateway="Default Gateway: "+String.valueOf(d.gateway); // Formatter.formatIpAddress(d.ipAddress) 方法是将 返回的INT 型 转为 IP 格式 s_ipAddress="IP Address: "+String.valueOf(Formatter.formatIpAddress(d.ipAddress)); //本地IP s_leaseDuration="Lease Time: "+String.valueOf(d.leaseDuration); s_netmask="Subnet Mask: "+String.valueOf(Formatter.formatIpAddress(d.netmask)); //本地mask s_serverAddress="Server IP: "+String.valueOf(d.serverAddress); //dispaly them info= (TextView) findViewById(R.id.tv); info.setText("Network Info\n"+s_dns1+"\n"+s_dns2+"\n"+s_gateway+"\n"+s_ipAddress+"\n"+s_leaseDuration+"\n"+s_netmask+"\n"+s_serverAddress); }}?
获取MAC 地址
?
public String getLocalMacAddress() { WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); return info.getMacAddress(); }?
?
其中 wifiinfo , getConnectionInfo()
wifiinfo.getBSSID();
wifiinfo.getSSID();
wifiinfo.getIpAddress();获取IP地址。
wifiinfo.getMacAddress();获取MAC地址。
wifiinfo.getNetworkId();获取网络ID。
wifiinfo.getLinkSpeed();获取连接速度,可以让用户获知这一信息。
? ? wifiinfo.getRssi();获取RSSI,RSSI就是接受信号强度指示。在这可以直 接和华为提供的Wi-Fi信号阈值进行比较来提供给用户,让用户对网络或地理位置做出调整来获得最好的连接效果。
? ? 这里得到信号强度就靠wifiinfo.getRssi();这个方法。
?
?
?
?
?