获取连接网络的WiFi信号强度

时间:2012-07-09 05:28:18

标签: android broadcastreceiver android-wifi

我正在尝试在我的Android应用中显示连接的WiFi网络的信号强度。我尝试了以下内容:

//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()上注册/取消注册我的接收器:

@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权限。我的接收器只有在我手动拨打<WiFiManager>.startScan()时才会被呼叫,但我正试图让它与我的广播接收器一起工作,以便我可以随时显示信号强度。我注意到onReceive()提供了一个列表,如果我只想要连接网络的统计数据,则不需要这个列表。有什么方法可以改变它吗?

编辑:另外,我意识到有一些相关的问题,但我读过的那些并没有太大帮助。 edit2:稍微澄清了我的问题。主要问题是除非我拨打startScan(),否则我的接收器永远不会被呼叫。

3 个答案:

答案 0 :(得分:5)

这段代码可以正常使用,以便在使用吐司更改时通知WiFi信号强度,所以

我的收件人就像这样

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

这是我的启动活动中注册Receiver并在TextView上获取Wifi信息

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

我在 XML布局文件

中的TextView上放置滚动视图
<?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>

最后我的清单看起来像这样

    <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>

仍然有任何问题让我知道!!

答案 1 :(得分:2)

在你的reciver中执行此操作 -

final List<ScanResult> results = _wifiManager.getScanResults();
//do it to get connected info..
    //  final WifiInfo wifiInfo = _wifiManager.getConnectionInfo();
      //           int connectedRssi= wifiInfo.getRssi();
        if (results != null) {
                for (final ScanResult result : results) {
                int rssi=result.level;
            }
              scanResult(rssi);
        }

为扫描结果创建一个界面,并在您想要显示它的活动中将其调用...

     public void scanResult(int rssi){
        _rssiText.setText(rssi);

      }

清单权限 -

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

希望这对你有帮助......

答案 2 :(得分:2)

如果您只想监控当前的WiFi信号强度,可以将IntentFilterWifiManager.RSSI_CHANGED_ACTION一起使用,而不是WifiManager.SCAN_RESULTS_AVAILABLE_ACTION