获取连接的网络的WiFi信号强度 [英] Get WiFi Signal Strength of Connected Network

查看:792
本文介绍了获取连接的网络的WiFi信号强度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示在我的Andr​​oid应用程序在连接WiFi网络的信号强度。我已经尝试了以下内容:

I'm trying to display the signal strength of the connected WiFi network in my Android app. I've tried the following:

//Receiver for information on the network info
private BroadcastReceiver mNetworkReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Log.d("INFO", "onReceive()");
        List<ScanResult> scanResult = mWifi.getScanResults();
        for(ScanResult scan : scanResult)
        {
            Log.d("INFO","Network strength: " + String.valueOf(scan.level) + " dBm " + scan.SSID);
        }
    }   
};

然后,我做我的接收器的注册/注销的onResume()/的onPause():

And then I do the registering/unregistering of my receiver on the onResume()/onPause():

@Override
public void onResume()
{
    super.onResume();
    mActivity.registerReceiver(mNetworkReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); //network info
}

@Override
public void onPause()
{
    super.onPause();
    mActivity.unregisterReceiver(mNetworkReceiver);
}

不过,这似乎并没有工作。我也有 ACCESS_WIFI_STATE 权限启用。我的接收器只调用如果我叫&LT; WiFiManager&GT; .startScan()手动,但我试图把它与我的广播接收器工作,让我总能表示信号强度只要稍有改变。我注意到的onReceive()提供了一个列表,它是不必要的,如果我只是想连接的网络的统计。更改过什么办法?

But this doesn't seem to work. I also have the ACCESS_WIFI_STATE permission enabled. My receiver is only called if I call <WiFiManager>.startScan() manually, but I'm trying to get it to work with my broadcast receiver so that I can always show the signal strength whenever it changes. I noticed that the onReceive() provides a list which is unnecessary if I just want the connected network's stats. Any way to change that too?

编辑:另外,我认识到,有这方面的一些相关问题,但那些我读过并没有太大的帮助。 EDIT2:澄清我的问题有点。主要的问题是,我的接收器不会被调用,除非我称之为 startScan()

edit: Also, I realize that there are some related questions on this, but the ones I've read weren't too helpful. edit2: Clarified my question a bit. The main problem is that my receiver is never called unless I call startScan().

推荐答案

好这个code是工作的罚款与我通知WiFi信号强度时,它改变使用吐司,所以

well this code is working fine with me to inform WiFi signal strength when it changes using toast, so

我的接收器是这样

@Override
  public void onReceive(Context context, Intent intent) {
    List<ScanResult> results = wifiDemo.wifi.getScanResults();
    ScanResult bestSignal = null;
    for (ScanResult result : results) {
      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);
    Toast.makeText(context, message, Toast.LENGTH_LONG).show();

    Log.d("Debug", "onReceive() message: " + message);
  }


这是我的启动活动以注册接收器,让无线信息上的TextView


this is what in my start-up activity to register Receiver and get Wifi Info on TextView

public class WiFiDemo extends Activity {

    WifiManager wifi;
    BroadcastReceiver receiver;

    TextView textStatus;    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Setup UI
        textStatus = (TextView) findViewById(R.id.textStatus);      

        // Setup WiFi
        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        // Get WiFi status
        WifiInfo info = wifi.getConnectionInfo();
        textStatus.append("\n\nWiFi Status: " + info.toString());

        // List available networks
        List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
        for (WifiConfiguration config : configs) {
            textStatus.append("\n\n" + config.toString());
        }

        // Register Broadcast Receiver
        if (receiver == null)
            receiver = new WiFiScanReceiver(this);

        registerReceiver(receiver, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        Log.d(TAG, "onCreate()");
    }

    @Override
    public void onStop() {
        unregisterReceiver(receiver);
            super.onStop();
    }
    }


我把滚动视图TextView的在 XML布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textStatus"
            android:text="WiFi Connections :" />
    </ScrollView>

</LinearLayout>


终于我的清单是这个样子


and finally my manifest is look like this

    <uses-sdk android:minSdkVersion="10" />
      <uses-permission android:name="android.permission.ACCESS_WIFI_STATE">
</uses-permission>
      <uses-permission android:name="android.permission.CHANGE_WIFI_STATE">
</uses-permission>

    <activity android:name=".WiFiDemo" android:label="@string/app_name">
          <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
        </activity>

        <receiver android:name=".WiFiScanReceiver">
          <intent-filter>
            <action android:name="com.rdc" />
          </intent-filter>
        </receiver>

还有任何问题,让我知道!

still have any trouble let me know!!

这篇关于获取连接的网络的WiFi信号强度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆