这是从网上收集到的一段扫描WIFI热点的代码,如下:
另: 关于WIFI的开启关闭与连接热点
1、需要申请的权限
复制代码
2、相关操作的代码
复制代码
3、注意
如果遇到force-close, 选wait即可, 因为启动wifi需要几秒钟, UI如果5妙钟还没反映的话, 系统会给你这个force close exception
- package com.android.wifitester;
-
- 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.WifiManager;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
-
- public class WifiTester extends Activity {
- TextView mainText;
- WifiManager mainWifi;
- WifiReceiver receiverWifi;
- List<ScanResult> wifiList;
- StringBuilder sb = new StringBuilder();
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mainText = (TextView) findViewById(R.id.mainText);
- mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
-
- receiverWifi = new WifiReceiver();
- registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
- mainWifi.startScan();
- mainText.setText("\n开始扫描...\n");
- }
-
- public boolean onCreateOptionsMenu(Menu menu) {
- menu.add(0, 0, 0, "刷新");
- return super.onCreateOptionsMenu(menu);
- }
-
- public boolean onMenuItemSelected(int featureId, MenuItem item) {
- mainWifi.startScan();
- mainText.setText("开始扫描");
- return super.onMenuItemSelected(featureId, item);
- }
-
- protected void onPause() {
- unregisterReceiver(receiverWifi);
- super.onPause();
- }
-
- protected void onResume() {
- registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
- super.onResume();
- }
-
- class WifiReceiver extends BroadcastReceiver {
- public void onReceive(Context c, Intent intent) {
- sb = new StringBuilder();
- wifiList = mainWifi.getScanResults();
- for(int i = 0; i < wifiList.size(); i++){
- sb.append(new Integer(i+1).toString() + ".");
- sb.append((wifiList.get(i)).toString());
- sb.append("\n");
- }
- mainText.setText(sb);
- }
- }
-
- }
你同时需要申请如下权限:
-
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
- <uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES"></uses-permission>
- <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
- <uses-permission android:name="android.permission.INTERNET"></uses-permission>
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
- <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission>
-
另: 关于WIFI的开启关闭与连接热点
1、需要申请的权限
- android.permission.ACCESS_WIFI_STATE
- android.permission.CHANGE_WIFI_STATE
- android.permission.WAKE_LOCK
- WifiManager wifi = (WifiManager) context
- .getSystemService(Context.WIFI_SERVICE);
- if (!wifi.isWifiEnabled()) { //如果wifi没有开启,则开启。
- wifi.setWifiEnabled(true);
- }
-
- WifiConfiguration wc = new WifiConfiguration();
- wc.SSID = "\"SSID\""; //配置wifi的SSID,即该热点的名称,如:TP-link_xxx
- wc.preSharedKey = "\"password\""; //该热点的密码
- 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 = wifi.addNetwork(wc);
- Log.d("WifiPreference", "add Network returned " + res);
- boolean b = wifi.enableNetwork(res, true);
- Log.d("WifiPreference", "enableNetwork returned " + b);
如果遇到force-close, 选wait即可, 因为启动wifi需要几秒钟, UI如果5妙钟还没反映的话, 系统会给你这个force close exception