Question

Below shows the code that I have created to output the signal Strength of my home access point (1 access point in total). I have a button initialized that calls the initializeWiFiListener() but my application has a few issues, the line of code :

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

has an error on it and I don't know why and is there any other reasons why this code won't output the strength of my home access point. Thank You

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));
}
Was it helpful?

Solution

the line of code :

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

has an error on it and I don't know why

You need to define the log tag. For example:

private static final String TAG = MainActivity.class.getSimpleName();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top