WIFIActivity:
public class WIFIActivity extends Activity { //定义WifiManager对象 private WifiManager mainWifi; //扫描出的网络连接列表 private List<ScanResult> wifiList; //扫描完毕接收器 private WifiReceiver receiverWifi; private Button btn_scan; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //设定实例,供其他类调用 ((MyApp) getApplication()).setWifiActivity(this); mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); receiverWifi = new WifiReceiver(); btn_scan = (Button) findViewById(R.id.btn_scan); btn_scan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { scanWifi(); } }); } /** * 扫描wifi,加载进度条 */ private void scanWifi() { openWifi(); mainWifi.startScan(); dialog = ProgressDialog.show(this, "", "正在扫描wifi热点,请稍候"); } /** * 打开wifi */ private void openWifi() { if (!mainWifi.isWifiEnabled()) { mainWifi.setWifiEnabled(true); } } /** * 关闭wifi */ private void closeWifi() { if (mainWifi.isWifiEnabled()) { mainWifi.setWifiEnabled(false); } } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 0, 0, "重新扫描"); return super.onCreateOptionsMenu(menu); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { scanWifi(); return super.onMenuItemSelected(featureId, item); } @Override protected void onPause() { super.onPause(); //注销广播 unregisterReceiver(receiverWifi); } @Override protected void onDestroy() { super.onDestroy(); //关闭wifi closeWifi(); } @Override protected void onResume() { super.onResume(); registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); } /** * wifi广播接收器 */ class WifiReceiver extends BroadcastReceiver { @Override 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_SHORT).show(); Intent in = new Intent(WIFIActivity.this, WifiListActivity.class); startActivity(in); } } } public List<ScanResult> getWifiList() { return wifiList; } public void setWifiList(List<ScanResult> wifiList) { this.wifiList = wifiList; } public WifiManager getMainWifi() { return mainWifi; } public void setMainWifi(WifiManager mainWifi) { this.mainWifi = mainWifi; } }
WifiListActivity :
/** * 列出扫描到的wifi热点 */ public class WifiListActivity extends ListActivity implements AdapterView.OnItemClickListener { private WIFIActivity wifiActivity; private WifiConfiguration wc; private ListView listView; private ScanResult scanResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyApp app = (MyApp) getApplication(); wifiActivity = app.getWifiActivity(); wc = new WifiConfiguration(); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getString(wifiActivity.getWifiList())); setListAdapter(adapter); listView = getListView(); listView.setAdapter(adapter); listView.setOnItemClickListener(this); } /** * 将集合转成数组 * * @param wifiList * @return */ public String[] getString(List<ScanResult> wifiList) { ArrayList<String> listStr = new ArrayList<>(); for (int i = 0; i < wifiList.size(); i++) { ScanResult scanResult = wifiList.get(i); listStr.add(scanResult.SSID); } return listStr.toArray(new String[0]); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { scanResult = wifiActivity.getWifiList().get(position); wc.SSID = "\"" + scanResult.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 = wifiActivity.getMainWifi().addNetwork(wc); //Log.d("Wif iPreference", "1111111add Network returned " + res); boolean b = wifiActivity.getMainWifi().enableNetwork(res, true); //Log.d("WifiPreference", "2222222222enableNetwork returned " + b); Toast.makeText(WifiListActivity.this, res + "" + b, Toast.LENGTH_SHORT).show(); } }
MyApp :
public class MyApp extends Application { private WIFIActivity wifiActivity = null; public WIFIActivity getWifiActivity() { return wifiActivity; } public void setWifiActivity(WIFIActivity wifiActivity) { this.wifiActivity = wifiActivity; } }
运行示例图:
需要的权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
项目开发工具:Android Stuido,Gradle版本:2.8
源码下载地址:http://download.csdn.net/detail/lingwu7/9348727