代码如下: 1.主活动代码WifiTest.java
package src.hero.wifitest; import java.util.List; import android.app.Activity; import android.app.ProgressDialog; 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.WifiManager; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; /* *信号强度是负数 wifiinfo.getRssi()这个方法决定的 *得到的值是一个0到-100的区间值,是一个int型数据,其中0到-50表示信号最好, *-50到-70表示信号偏差,小于-70表示最差,有可能连接不上或者掉线。 */ public class WifiTest extends Activity { private Button btn_scan; /** 定义WifiManager对象 */ private WifiManager mainWifi; /** 扫描完毕接收器 */ private WifiReceiver receiverWifi; /** 扫描出的网络连接列表 */ private List wifiList; private ProgressDialog dialog; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wifi_test); ((WifiTesterApp) getApplication()).setWifiTester(WifiTest.this);// 设定实例,供其他类调用 mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); receiverWifi = new WifiReceiver(); btn_scan = (Button) this.findViewById(R.id.button_scran); btn_scan.setOnClickListener(new OnClickListener() { public void onClick(View v) { scanWifi(); } }); } /** * 扫描WIFI 加载进度条 */ void scanWifi() { OpenWifi(); mainWifi.startScan(); dialog = ProgressDialog.show(WifiTest.this, "", "正在扫描WIFI热点,请稍候"); } /** * 打开WIFI */ public void OpenWifi() { if (!mainWifi.isWifiEnabled()) { mainWifi.setWifiEnabled(true); } } /** * 关闭WIFI */ public void CloseWifi() { if (mainWifi.isWifiEnabled()) { mainWifi.setWifiEnabled(false); } } public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 0, 0, "重新扫描"); return super.onCreateOptionsMenu(menu); } public boolean onMenuItemSelected(int featureId, MenuItem item) { scanWifi(); return super.onMenuItemSelected(featureId, item); } protected void onPause() { super.onPause(); unregisterReceiver(receiverWifi);// 注销广播 } protected void onResume() { super.onResume(); registerReceiver(receiverWifi, new IntentFilter( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));// 注册广播 } class WifiReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { if (intent.getAction().equals( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { wifiList = mainWifi.getScanResults(); dialog.dismiss(); Toast.makeText(context, "扫描完毕", Toast.LENGTH_LONG).show(); Intent in = new Intent(); in.setClass(WifiTest.this, ListOk.class); WifiTest.this.startActivity(in); } } } public List getWifiList() { return wifiList; } public void setWifiList(List wifiList) { this.wifiList = wifiList; } public WifiManager getMainWifi() { return mainWifi; } public void setMainWifi(WifiManager mainWifi) { this.mainWifi = mainWifi; } } 2.ListOk.java package src.hero.wifitest; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener; public class ListOk extends ListActivity implements OnItemClickListener { private ListView lv; private WifiTest wifiTester; // -----------------------连接WIFI private ScanResult scanRet; private WifiConfiguration wc; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setTitle("当前可用的WIFI列表"); WifiTesterApp _TestActivityApp = (WifiTesterApp) this.getApplication(); wifiTester = (_TestActivityApp).getWifiTester(); wc = new WifiConfiguration(); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, getString(wifiTester.getWifiList())); setListAdapter(adapter); lv = getListView(); lv.setAdapter(adapter); lv.setOnItemClickListener(this); } public String[] getString(List wifiList) { ArrayList listStr = new ArrayList(); for (int i = 0; i < wifiList.size(); i++) { listStr.add(wifiList.get(i).toString()); } return listStr.toArray(new String[0]); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { scanRet = wifiTester.getWifiList().get(position); wc.SSID = "\"" + scanRet.SSID + "\""; // 配置wifi的SSID,即该热点的名称,如:TP-link_xxx wc.preSharedKey = "\"7675781777\""; // 该热点的密码 wc.hiddenSSID = true; wc.status = WifiConfiguration.Status.ENABLED; wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // int res = wifiTester.getMainWifi().addNetwork(wc); // Log.d("Wif iPreference", "1111111add Network returned " + res); // boolean b = wifiTester.getMainWifi().enableNetwork(res, true); // Log.d("WifiPreference", "2222222222enableNetwork returned " + b); } } 3.WifiTesterApp.java package src.hero.wifitest; import android.app.Application; public class WifiTesterApp extends Application { private WifiTest wifiTester = null; public WifiTest getWifiTester() { return wifiTester; } public void setWifiTester(WifiTest wifiTester) { this.wifiTester = wifiTester; } } 4.AndroidMainfest.xml
5.layout.xml