Method onCreate(Bundle savedInstanceState) is called when the activity(WiFiManageActivity.java) is starting.
These intent broadcasts (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION and WifiManager.RSSI_CHANGED_ACTION) will to be received by BroadcastReceiver (WiFiBroadcastReceiver.java).
Make text about the best signal one connection when received intent action WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.
Make text about all active connections when received intent action WifiManager.RSSI_CHANGED_ACTION.
Activity Class:
package com.paad.wifi;
import android.app.Activity;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
public class WiFiManageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
setWifiEnabled(wifi);
// Register a broadcast receiver that listens for actions.
registerReceiver(new WiFiBroadcastReceiver(this, wifi), new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
registerReceiver(new WiFiBroadcastReceiver(this, wifi), new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
wifi.startScan();
}
private void setWifiEnabled(WifiManager wifi) {
if (!wifi.isWifiEnabled() && wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {
wifi.setWifiEnabled(true);
}
}
}
BroadcastReceiver Class:
package com.paad.wifi;
import java.util.List;
import com.paad.util.NetworkUtil;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;
public class WiFiBroadcastReceiver extends BroadcastReceiver {
private Context context = null;
private WifiManager wifi = null;
public WiFiBroadcastReceiver(Context context, WifiManager wifi) {
this.context = context;
this.wifi = wifi;
}
@Override
public void onReceive(Context context, Intent intent) {
if (WifiManager.RSSI_CHANGED_ACTION.equals(intent.getAction())) {
findActiveConnection();
}
if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())) {
findBestSignalConnection();
}
}
private void findBestSignalConnection() {
List<ScanResult> results = wifi.getScanResults();
ScanResult bestSignal = null;
for (ScanResult result : results) {
if (bestSignal == null || WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
bestSignal = result;
}
String connSummary = String.format("%s networks found. %s is " + "the strongest.", results.size(), bestSignal.SSID);
Toast.makeText(context, connSummary, Toast.LENGTH_LONG).show();
Log.d(NetworkUtil.TAG, connSummary);
}
private void findActiveConnection() {
WifiInfo info = wifi.getConnectionInfo();
if (info.getBSSID() != null) {
String ssid = info.getSSID();
String units = WifiInfo.LINK_SPEED_UNITS;
int speed = info.getLinkSpeed();
int strength = WifiManager.calculateSignalLevel(info.getRssi(), 5);
String ipAddress = NetworkUtil.intToIp(info.getIpAddress());
String connSummary = String.format("Connected to %s at %s%s. " + "Strength %s/5. " + "IP %s", ssid, speed, units, strength, ipAddress);
Toast.makeText(context, connSummary, Toast.LENGTH_LONG).show();
Log.d(NetworkUtil.TAG, connSummary);
}
}
}
Util Class:
package com.paad.util;
public class NetworkUtil {
public final static String TAG = "WiFi";
public static String intToIp(int i) {
return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 24) & 0xFF);
}
}
时间: 2024-09-25 10:04:17