Search code examples
androidandroid-wifi

Wifi scanning Application keeping crashing without showing the main activity


I'm writing an application to scan for wireless network available but , when I run my application it crashes before even displaying the main activity. also I was looking for how to use the scan result but I'm not sure if the way I'm using it is correct . I read about the broadcast receiver but I didn't find any examples of how to use it . here is my code , Any help is really appreciated

package com.example.wizer2;

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.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Wizer extends Activity {

WifiManager Wifi;
BroadcastReceiver receiver;
List<ScanResult> results;


Button bt1; //Current status
TextView t1; //for current status result


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

    //GUI

    bt1=(Button)findViewById(R.id.b1);
    t1=(TextView)findViewById(R.id.t1);

     // to enable wifi service
    Wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    bt1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {

            WifiInfo info = Wifi.getConnectionInfo();
            if (info.getBSSID()==null)
            {
                t1.setText("You are currently not connected to any wireless network.\n");
            }
            else
            {
                t1.append("Current Status:\n");
                t1.append("Network Name :"+info.getSSID().toString()+"\n");
                t1.append("RSSI :"+info.getRssi()+"\n");

            }


        }
    });
    Wifi.startScan();
    // Register Broadcast Receiver
    if (receiver == null)
        receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                results =Wifi.getScanResults();
//              Intent I1 =new Intent();


            }
        };
        registerReceiver(receiver, new IntentFilter( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}

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

}


Solution

  • I used your code only without changing anything except added permissions in manifest file. Its working for me Check the code

    public class MainActivity extends Activity {  WifiManager Wifi;
    BroadcastReceiver receiver;
    List<ScanResult> results; @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //GUI
        bt1=(Button)findViewById(R.id.btn1);
        t1=(TextView)findViewById(R.id.txt1);
         // to enable wifi service
        Wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        bt1.setOnClickListener(new View.OnClickListener() {   public void onClick(View arg0) {
                WifiInfo info = Wifi.getConnectionInfo();
                if (info.getBSSID()==null)
                {
                    t1.setText("You are currently not connected to any wireless network.\n");
                }
                else
                {
                    t1.append("Current Status:\n");
                    t1.append("Network Name :"+info.getSSID().toString()+"\n");
                    t1.append("RSSI :"+info.getRssi()+"\n");
    
                }
            }
        });  Wifi.startScan();
        // Register Broadcast Receiver
        if (receiver == null)
            receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    results =Wifi.getScanResults();
        //                Intent I1 =new Intent();
                }
            };
            registerReceiver(receiver, new IntentFilter( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    }
    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_wizer, menu);
        return true;
    }*/
    public void onStop() {
        unregisterReceiver(receiver);
    } }
    

    Manifest.xml

    <?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.textoverlay"
    android:versionCode="1"
    android:versionName="1.0" >  <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >  <activity
            android:name="com.example.textoverlay.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>  </application>