输出RSSI wifi强度

时间:2014-02-06 11:47:09

标签: android eclipse broadcastreceiver wifimanager

下面显示了我创建的用于输出家庭接入点信号强度的代码(总共1个接入点)。我有一个初始化的按钮,调用initializeWiFiListener()但我的应用程序有一些问题,代码行:

  

Log.i(TAG,“执行initializeWiFiListener”);

有错误,我不知道为什么,还有其他原因导致此代码无法输出我的家庭接入点的强度。谢谢

package com.example.newandroidapplication;

import java.util.List;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private void initializeWiFiListener(){

    Log.i(TAG, "executing initializeWiFiListener");
    String connectivity_context = Context.WIFI_SERVICE;
    final WifiManager wifi = (WifiManager)getSystemService(connectivity_context);
    if(!wifi.isWifiEnabled()){
        if(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING){
            wifi.setWifiEnabled(true);
        }

    }
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            WifiInfo info = wifi.getConnectionInfo();
            //TODO: implement methods for action handling
            int value = info.getRssi();
            System.out.println(value);
        }
    }, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
}

1 个答案:

答案 0 :(得分:0)

  

代码行:

Log.i(TAG, "executing initializeWiFiListener");
     

有错误,我不知道为什么

您需要定义日志标记。例如:

private static final String TAG = MainActivity.class.getSimpleName();