Android WiFi Management Sample

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

时间: 2024-09-25 10:04:17

Android WiFi Management Sample的相关文章

Android Wifi的管理和操作

因为需要一直在弄网络的问题,今天看了一下Wifi的操作,经过整理,做出来了一个类,可能不全,但是个人感觉已经完全能够满足需要了,当然,里面的方法也有可能是错误的或者是不全的,这个类我没有进行完整的测试,只测试了其中的一些方法. 其实操作Wifi也是很简单的,主要使用以下几个对象或变量: private WifiManager wifiManager;// 声明管理对象OpenWifi private WifiInfo wifiInfo;// Wifi信息 private List<ScanRes

关于android wifi传输文件的问题

问题描述 关于android wifi传输文件的问题 我想问一下这个是怎么实现的,就是安卓客户端打开wifi传输功能,给出一个地址,然后用户在浏览器 输入这个地址,然后通过网页上传文件然后到安卓端 解决方案 做了个毕设, 是在Wifi环境下,Android手机和PC的文件共享,也就是在PC通过WIFI读写手机中的文件.然后,毕设过后,本来想发出来给大家使用. 发布到应用宝,但是需要提供国家著作权的证明. 发布到小米应用市场,嫌APP功能太少.所以都没有发布成功.所以只能贴出来这里给大家参考了.

android wifi 如何在路由器关上的时候马上检测到wifi连接状态变化

问题描述 android wifi 如何在路由器关上的时候马上检测到wifi连接状态变化 在应用中监听了WifiManager.NETWORK_STATE_CHANGED_ACTION这个广播,希望在wifi状态变化的时候重启应用.可是每次路由器关上后,应用都需要等七八秒才检测到wifi断开.怎么才能在路由器一关上就检测到手机wifi连接状态变化呢?

android wifi direct group owner negotiation

问题描述 android wifi direct group owner negotiation 有人知道android wifi direct 中group owner negotiation 中的哪个device作为GO哪个作为Client以及 采用Pin码还是PBC模式是在哪里确定的吗?

Android wifi无线调试App新玩法ADB WIFI

Wifi 调试App已经不是什么新鲜的事情了,之前也看过不少,不是使用麻烦就是需要root权限,今个我给大家介绍一款好用的android studio 插件--ADB WIFI. 安装 settings->plugins->Browse repositories 搜索框输入ADB WIFI找到插件,安装,重启android Studio后可以看到 导航菜单下Tools->Android->ADB WIFI就算安装成功了.   使用 第一步:首先还是需要用数据线连接电脑,如下图:  

Android Wifi的forget()操作实例详解

Android  Wifi的forget()操作实例详解 我们在处理某个Wifi连接时,有时会需要忘掉当前连接的密码信息.执行这项操作,我们需要调用WifiManager::forget()函数: /** * Delete the network in the supplicant config. * * This function is used instead of a sequence of removeNetwork() * and saveConfiguration(). * * @p

Android WIFI模块解析

Android中的AP扫描结果,通过WifiNative的JNI调用,实际调用wpa_supplicant的相应命令,并返回字符串给JNI方法,下面是对该字符串的解析函数:  Java代码   private static final Pattern scanResultPattern = Pattern.compile("\t+");   /**       * Parse the scan result line passed to us by wpa_supplicant (he

Android WiFi ADB

作者:snowdreamEmail:yanghui1986527#gmail.comGithub: https://github.com/snowdreamQQ 群: 529327615原文地址:https://snowdream.github.io/blog/2016/10/27/wifi-adb-without-usb/ 简介 大家在开发调试Android应用的时候,都需要使用USB连接电脑和测试手机.那么如何通过WIFI来连接电脑和测试手机呢? 通常的做法是这样: 安装idea插件Andr

Eclipse开发环境导入android sdk的sample中的源码_Android

    初学Android编程,Android SDK中提供的Sample代码自然是最好的学习材料.     Sample代码在SDK安装目录(X:\Android\android-sdk\samples)下.但我在用Eclipse 开发环境导入的时候遇到了一些麻烦.网上看到有人是通过"File -> Import..."或"File -> New->Project -> Create Project From Existing Code"解