為了某個所有人都說做不到的業務需求,老闆不曲不撓地在網路上Google了段程式碼開始進行無差別攻擊,最後沒人要理他的結果是丟到我頭上,讓人不知道該感激碰上一個會自己找solution的老闆,還是該怨嘆碰上了一個會把自己都看不懂的tutorial code當solution亂丟的老闆 orz,但既然都代為實作了,簡單記錄一下心得好了
在AndroidManifest.xml 中加上相關的存取許可
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
在程式中建立 WiFiManager
WiFiManager wifimgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
接下來可以透過wifimgr做一些事
看手機的WiFi是否有被啟用
wifimgr.isWifiEnabled()
取得 WiFi 連線資訊
//如果 WiFi 是disable的狀態,下面會抓到null
// 可以用wifimgr.setWifiEnabled(true) 啟用
WifiInfo info = wifimgr.getConnectionInfo();
把 info 倒出來會長得類似像這樣
SSID: <none>, BSSID: <none>, MAC: 11:22:33:44:55:66, Supplicant state: ASSOCIATING, RSSI: -200, Link Speed: 10, Net ID: -1
你也可以取得你手機記錄到的 wifi 資訊
List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
for (WifiConfiguration config : configs) {
Log.d("DUMP", config.toString());
}
單然,你也可以抓到你手機認到的WiFi AP的相關資訊,這部份不難,只是要多打一點字
先產生一個吃資料的Receiver,並註冊之
receiver = new WiFiScanReceiver(this);
registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
這樣,在手機收到WiFi AP 訊號異動的事件時,你就可以叫receiver做相對應的處理,下面的Code中也只是把 AP 資訊印出來
package com.cht.dc.tt.ywhuang;
import java.util.List;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;
public class WiFiScanReceiver extends BroadcastReceiver {
private static final String TAG = "WiFi";
main mainAct;
public WiFiScanReceiver(main mainAct){
super();
this.mainAct = mainAct;
}
@Override
public void onReceive(Context arg0, Intent arg1) {
List<scanresult> results = mainAct.wifimgr.getScanResults();
ScanResult bestSignal = null;
for (ScanResult result : results) {
mainAct.textStatus.append("ScanResult : \n");
mainAct.textStatus.append("SSID: " + result.SSID + "\n");
mainAct.textStatus.append("BSSID: " + result.BSSID + "\n");
mainAct.textStatus.append("Level: " + result.level + "\n");
mainAct.textStatus.append("Frequency: " + result.frequency + "\n");
mainAct.textStatus.append("Capabilities: " + result.capabilities + "\n");
mainAct.textStatus.append("END_ScanResult\n");
mainAct.textStatus.append("\n");
if (bestSignal == null ||
WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0) {
bestSignal = result;
}
}
String message = String.format("%s networks found. %s is the strongest.",
results.size(), bestSignal.SSID);
mainAct.textStatus.append(message + "\n");
mainAct.textStatus.append("--------------------------\n");
//Toast.makeText(mainAct, message, Toast.LENGTH_LONG).show();
Log.d(TAG, "onReceive() message: " + message);
}
}
丟出來的訊息會長得類似像這樣
ScanResult:
SSID: ABCDEF
BSSID: 11:22:33:44:55:66
Level: -80
Frequency: 2412
Capabilities: [WPA-EAP-TKIP][WPA2-EAP-CCMP]
END_ScanResult
...
9 networks found. ABCDEF is the strongest
這樣問題解決了嗎 ? NONONONONONONON~~~Not even close T_T
但為什麼老闆開心地像是已經打敗Google了呢 ? orz
good experience haha!!
回覆刪除