Hosdgfag2
Hosdgfag2

Reputation: 151

Scanning wifi once on android, but the program scans multiple times

I'm an Android beginner, and I try to write a program that scans all available AP's once, and then save them to a string.

The problem is that I can't get the program to scan just once, instead the output from the following run is:

Scanning:
Done
Connected to AP
Connected to AP
Connected to AP
Connected to AP
Connected to AP
...

"Scanning" is printed to the screen when the method ScanWifi() is called. It is only printed once, so that indicates that the method is only run once. After that "Done" is printed, indicating that the method has returned.

After that however, there is an infinite chain of "Connected to AP", indicating that the wifi somehow still is scanning. I tried to add a return statement at the end of the onReceive() function, but it didn't make any difference. Any ideas?

WifiScan activity:

package com.example.wifi_scanner;

import android.support.v7.app.ActionBarActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;

public class WifiScan extends ActionBarActivity {

    WifiManager wifi;
    BroadcastReceiver receiver;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      setContentView(R.layout.activity_wifi_scan);        
        textView = new TextView(this);
        textView.setTextSize(10);
        setContentView(textView);
    }

    @Override
    protected void onStart() {
        super.onStart();

        scanWifi();

        textView.append("\nDone");
        return;
    }

    void scanWifi() {
        final StringBuilder sb = new StringBuilder();

        textView.setText("Scanning:");                  

        // Setup receiver 
        registerReceiver(new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context c, Intent intent) 
            {
                textView.append("\nConnected to AP");
                sb.append(wifi.getScanResults().toString());
                sb.append("\n");
                return;
            }
        }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

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

        // Start scan
        wifi.startScan();
    }
}

Main activity:

package com.example.wifi_scanner;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends ActionBarActivity {

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

    public void startScan(View view) {
        Intent intent = new Intent(this, WifiScan.class);
        startActivity(intent);
    }
}

Main activity xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.wifi_scanner.MainActivity" >

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Scan Wifi" 
        android:onClick="startScan"  />

</RelativeLayout>

Upvotes: 0

Views: 308

Answers (1)

Grender
Grender

Reputation: 1619

A solution would be controlling the behaviour with booleans. You must know that the receiver will always be called whenever a new wifi is found.

Upvotes: 1

Related Questions