From here you can easily detect the Wifi-Networks as well as there info surrounding your device. This can be achieved by using the "WifiManager" in a following way :-
package com.example.wifidetector;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
mport android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
StringBuilder sb = new StringBuilder();
WifiManager mainWifi;
List<ScanResult> wifiList;
WifiReceiver receiverWifi;
Button mainText;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainText = (Button) findViewById(R.id.main_text);
textView = (TextView) findViewById(R.id.text_info);
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mainText.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mainWifi.startScan();
}
});
}
protected void onPause() {
unregisterReceiver(receiverWifi);
super.onPause();
}
protected void onResume() {
registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
@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;
}
class WifiReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
sb = new StringBuilder();
wifiList = mainWifi.getScanResults();
for(int i = 0; i < wifiList.size(); i++){
sb.append(new Integer(i+1).toString() + ".");
sb.append((wifiList.get(i)).toString());
sb.append(" ");
}
textView.setText(sb);
}
}
}
The layout of this particular activity is simple only contains a button and a text view below it.
By doing all this with just a click of a button , the given text view will be set with the information of the available network settings around you.
Don't forget to define the following permission in your Manifest :-
android:name="android.permission.ACCESS_WIFI_STATE"
android:name="android.permission.CHANGE_WIFI_STATE"
Hope this will help you .... :)
0 Comment(s)