Android WiFi Management Sample

简介: <div class="markdown_views"><p>Method <em>onCreate(Bundle savedInstanceState)</em> is called when the activity(<em>WiFiManageActivity.java</em>) is starting.</p><p>These intent broadcasts

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);
    }
}

Full Codes

相关文章
|
6月前
|
Android开发
Android 状态栏WiFi图标的显示逻辑
Android 状态栏WiFi图标的显示逻辑
166 0
|
6月前
|
Android开发
Android获取当前连接的wifi名称
Android获取当前连接的wifi名称
337 6
|
6月前
|
Android开发
android连接指定wifi
android连接指定wifi
107 0
|
6月前
|
Java Android开发
Android 9在连接以太网情况下 还能连接WiFi
Android 9在连接以太网情况下 还能连接WiFi
65 0
|
6月前
|
Android开发
Android12 ethernet和wifi共存
Android12 ethernet和wifi共存
377 0
|
6月前
|
Java Shell Android开发
Android11 有线网和wifi优先级设置
Android11 有线网和wifi优先级设置
516 0
|
6月前
|
Java Android开发 开发者
rk3399 android以太网和wifi共存
rk3399 android以太网和wifi共存
209 0
|
6月前
|
缓存 Java Android开发
Android 9.0 WiFi 扫描结果上报和获取流程
Android 9.0 WiFi 扫描结果上报和获取流程
292 0
|
6月前
|
Android开发
Android 获取Wifi开关状态、控制Wifi开关
Android 获取Wifi开关状态、控制Wifi开关
202 0
|
6月前
|
Android开发
android 获取wifi的协议标准
android 获取wifi的协议标准
97 0