Question

I'm trying to display the scan results of WiFi with getScanResults(). However, crash occurs when the info of getScanResults is refered. More detail, when the two lines of code added crash just happens:

if(results.size() > 0)
 Toast.makeText(this, "Networks available!", Toast.LENGTH_LONG).show(); 

and the code segment is as fellows:

public void displayWifiNetworks(WifiManager wifi, ListView listView) {
    List<ScanResult> results = wifi.getScanResults();
    if(results.size() > 0)
        Toast.makeText(this, "Networks available!", Toast.LENGTH_LONG).show();

Whether someone could help me, THX!

I have add the <receiver> tag in which the action tags are included in the mainFest.xml. But however, it crashes stil when running, with the warning "appname has stopped", the java file and mainFest.xml are as follows: `

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.connectivitymanager"
android:versionCode="1"
android:versionName="1.0" >
    <uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="17" />

<!-- add the permission to access and change the network state -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

<!-- add the permission to access and change the WiFi state -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_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.connectivitymanager.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>

    <!-- in order to access the WifiManage.getScanResults()  -->
    <receiver 
        android:name="com.example.connectivitymanager.MainActivity$WifiReceiver">
        <intent-filter>
            <action android:name="android.net.wifi.STATE_CHANGE"></action>
            <action android:name="android.net.wifi.SCAN_RESULTS"></action>
        </intent-filter>
    </receiver>
</application>

and the java file

com.example.connectivitymanager;
public class MainActivity extends Activity {
private TextView viewText;
private Switch switchWiFiState;
WifiManager wifi;//this is used in WifiReceiver
WifiReceiver receiverWifi;
List<ScanResult> wifiList;  

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

    //display the initial state of the WiFi
    String service = Context.CONNECTIVITY_SERVICE;
    ConnectivityManager connectivity = (ConnectivityManager)getSystemService(service);
    switchWiFiState = (Switch)findViewById(R.id.switch1);       
    if(connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
        switchWiFiState.setChecked(true);
    }
    else
        switchWiFiState.setChecked(false);
    //change the state when thumb the switch back and force 
    switchWiFiState.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //update the WiFi state by the switch
            String service = Context.WIFI_SERVICE;
            wifi = (WifiManager)getSystemService(service);
            receiverWifi = new WifiReceiver();
            registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            wifi.startScan();   //startScan 
            if(isChecked) {
                wifi.setWifiEnabled(true);
                //when the WiFi is enabled, the available networks should be listed in ListView listAvailableNetworks
                ListView listView = (ListView) findViewById(R.id.listAvailableNetworks);                    
                displayWifiNetworks(wifi, listView);
            }
            else 
                wifi.setWifiEnabled(false);
        }
    });




}

public void displayWifiNetworks(WifiManager wifi, ListView listView) {
    //Toast.makeText(this, "displaying WiFi information...", Toast.LENGTH_LONG).show(); 
    //if(null != wifi.getScanResults().get(1).SSID)
    List<ScanResult> results = wifi.getScanResults();
    if(results.size() > 0)
        Toast.makeText(this, "Networks available!", Toast.LENGTH_LONG).show();


}

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

//when click the displayConnectState button, the state is displayed in the textView
public void displayConnectState(View view) {
    switch (view.getId()) {
    case R.id.button1:
        Toast.makeText(this, "Loading the connect info...", Toast.LENGTH_LONG).show();
        String service = Context.CONNECTIVITY_SERVICE;
        ConnectivityManager connectivity = (ConnectivityManager)getSystemService(service);
        viewText = (TextView) findViewById(R.id.textView2);
        viewText.setText(String.valueOf(connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getDetailedState()));
        break;
    }
}

class WifiReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(MainActivity.this, "in onReceive ...", Toast.LENGTH_LONG).show();
        StringBuilder sb = new StringBuilder();
        wifiList = wifi.getScanResults();
        for(int i = 0; i< wifiList.size(); i++) {
            System.out.println(wifiList.get(i).toString());
        }

    }   
}
}
Was it helpful?

Solution

getScanResults() can return null, most of the time because you have not the permission

<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.SCAN_RESULTS"/>

from grepcode

/**
 * Return the results of the latest access point scan.
 * @return the list of access points found in the most recent scan.
 */
public List<ScanResult> getScanResults() {
    try {
        return mService.getScanResults();
    } catch (RemoteException e) {
        return null;
    }
}

their bad, the service is throwing a RemoteException and they are simply ignoring it. I will suggest you to retry ever 3seconds to retrieve the results:

Declare the WifiManager as class variable and instead of call displayWifiNetworks, call handler.post(wifiRunnable);. If you leave the Activity, inside the onPause() you have to call handler.removeCallbacks(null);

Handler handler = new Handler();

Runnable wifiRunnable = new Runnable() {
    @Override
    public void run() {
        List<ScanResult> results = wifi.getScanResults();
        if (results == null) {

            handler.postDelayed(this, 3000);
            return;
        }

        handler.removeCallbacks(this);

        if(results.size() > 0)
            Toast.makeText(this, "Networks available!", Toast.LENGTH_LONG).show();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top