Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 掃描並取得周圍 Wi-Fi 熱點的實作

編輯:關於 Android 程式設計

很多 App 都需要知道使用者周遭有哪些無線網路,例如網路診斷工具、訊號監測或自動連線類應用。實作起來比想像中簡單,核心就是 WifiManager 加上一個 ScanResult 清單。

整體思路

  1. 透過 getSystemService(WIFI_SERVICE) 取得 WifiManager。
  2. 呼叫 getScanResults() 拿到掃描結果,每筆都是一個 ScanResult。
  3. 依 level 欄位(訊號強度)由強到弱排序。
  4. 把結果呈現在畫面上。

MainActivity.java

 public class MainActivity extends AppCompatActivity {

  // 存放周圍 wifi 熱點的清單
  ArrayList<ScanResult> list;
  WifiManager wifiManager;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    // 取得系統 wifi 服務
    list = (ArrayList<ScanResult>) wifiManager.getScanResults();

    sortByLevel(list);
    init();
  }

  private void init() {
    TextView tv1 = (TextView) findViewById(R.id.tv1);
    TextView tv2 = (TextView) findViewById(R.id.tv2);
    TextView tv3 = (TextView) findViewById(R.id.tv3);

    if (list.get(0).SSID != null && list.get(1).SSID != null) {
      tv1.setText("訊號最強為 " + list.get(0).SSID);
      tv2.setText("訊號第二位:" + list.get(1).SSID);
      tv3.setText("共有 " + list.size() + " 個 wifi");
    }
  }

  /** 依訊號強度由強到弱排序 */
  private void sortByLevel(ArrayList<ScanResult> list) {
    for (int i = 0; i < list.size(); i++) {
      for (int j = 1; j < list.size(); j++) {
        if (list.get(i).level < list.get(j).level) {
          // level 即為強度,數值越大越強
          ScanResult temp = list.get(i);
          list.set(i, list.get(j));
          list.set(j, temp);
        }
      }
    }
  }
} 

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <TextView android:id="@+id/tv1"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Hello World!" />

  <TextView android:id="@+id/tv2"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Hello World!" />

  <TextView android:id="@+id/tv3"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Hello World!" />
</LinearLayout> 

幾個實務上會遇到的問題

  • 權限。至少需要 ACCESS_WIFI_STATE;Android 6.0 以上還要 ACCESS_FINE_LOCATION 並在執行期動態申請,否則 getScanResults() 會回傳空清單。這是系統基於隱私的設計,不是 API 壞了。
  • 清單可能為空。首次開啟 Wi-Fi、或才剛打開定位服務時,掃描結果往往還沒準備好。上例直接用 list.get(0) 會拋出 IndexOutOfBounds,正式環境請先判空。
  • 需要一點時間。掃描是 asynchronously 進行的,較嚴謹的做法是註冊 BroadcastReceiver 監聽 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION,在收到廣播後才取值。
  • 排序可以更省事。本例用手寫氣泡排序教學用,實務上直接 Collections.sort(list, ...) 搭配自訂 Comparator 即可。
  • Android 9 以後的限制。系統對 Wi-Fi 掃描結果做了頻率與內容上的收斂,能拿到的資訊比早期版本精簡,設計功能時要先確認目標版本的行為。

整體來說,取得周圍熱點這件事程式碼量極小,真正的成本在權限引導與空值的防禦。把這兩塊處理好,功能就穩了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved