How to scan for WiFi networks
Android allows applications to access to view the access the state of the wirless connections at very low level. Application can access almost all the information of a wifi connection.
The information that an application can access includes connected network's link speed,IP address, negotiation state, other networks information. Applications can also scan, add, save, terminate and initiate Wi-Fi connections.
Android provides WifiManager API to manage all aspects of WIFI connectivity. We can instantiate this class by callinggetSystemService method. Its syntax is given below:
WifiManager mainWifiObj; mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
In order to scan a list of wireless networks, you also need to register your BroadcastReceiver. It can be registered using registerReceiver method with argument of your reciever class object. Its sytanx is given below:
class WifiScanReceiver extends BroadcastReceiver { public void onReceive(Context c, Intent intent) { } } WifiScanReceiver wifiReciever = new WifiScanReceiver(); registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
The wifi scan can be start by calling the startScan method of the WifiManager class. This method returns a list of ScanResult objects. You can access any object by calling the get method of list. Its syntax is given below:
List<ScanResult> wifiScanList = mainWifiObj.getScanResults(); String data = wifiScanList.get(0).toString();
Apart from just Scanning , you can have more control over your WIFI by using the methods defined in WifiManager class. They are listed as follows:
Sr.No | Method & Description |
---|---|
1 | addNetwork(WifiConfiguration config) This method add a new network description to the set of configured networks. |
2 | createWifiLock(String tag) This method creates a new WifiLock. |
3 | disconnect() This method disassociate from the currently active access point. |
4 | enableNetwork(int netId, boolean disableOthers) This method allow a previously configured network to be associated with. |
5 | getWifiState() This method gets the Wi-Fi enabled state |
6 | isWifiEnabled() This method return whether Wi-Fi is enabled or disabled. |
7 | setWifiEnabled(boolean enabled) This method enable or disable Wi-Fi. |
8 | updateNetwork(WifiConfiguration config) This method update the network description of an existing configured network. |
Here is an example demonstrating the use of WIFI. It creates a basic application that scans a list of wirless networks and populate them in a list view.
Here is an example demonstrating the use of WIFI. It creates a basic application that scans a list of wirless networks and populate them in a list view.
package
com.example.wifiscandemo;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
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.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.ListView;
import
android.widget.SimpleAdapter;
import
android.widget.TextView;
import
android.widget.Toast;
public
class
MainActivity extends
Activity implements
OnClickListener
{
WifiManager wifi;
ListView lv;
TextView textStatus;
Button buttonScan;
int
size = 0;
List<ScanResult> results;
String ITEM_KEY
= "key";
ArrayList<HashMap<String,
String>> arraylist
= new
ArrayList<HashMap<String, String>>();
SimpleAdapter adapter;
/*
Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textStatus
= (TextView) findViewById(R.id.textStatus);
buttonScan
= (Button) findViewById(R.id.buttonScan);
buttonScan.setOnClickListener(this);
lv
= (ListView)findViewById(R.id.list);
wifi
= (WifiManager) getSystemService(Context.WIFI_SERVICE);
if
(wifi.isWifiEnabled()
== false)
{
Toast.makeText(getApplicationContext(),
"wifi is disabled..making it
enabled",
Toast.LENGTH_LONG).show();
wifi.setWifiEnabled(true);
}
this.adapter
= new
SimpleAdapter(MainActivity.this,
arraylist,
R.layout.row,
new
String[] { ITEM_KEY
}, new
int[]
{ R.id.list_value
});
lv.setAdapter(this.adapter);
registerReceiver(new
BroadcastReceiver()
{
@Override
public
void
onReceive(Context c, Intent intent)
{
results
= wifi.getScanResults();
size
= results.size();
}
}, new
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
public
void
onClick(View view)
{
arraylist.clear();
wifi.startScan();
Toast.makeText(this,
"Scanning...."
+ size,
Toast.LENGTH_SHORT).show();
try
{
size
= size - 1;
while
(size >=
0)
{
HashMap<String,
String> item = new
HashMap<String, String>();
item.put(ITEM_KEY,"SSID:
"+ results.get(size).SSID
+ "\n"+
"BSSID
: "+ results.get(size).BSSID
+ "\n"+
"capabilities:
"+
results.get(size).capabilities+"\n"+
"frequency
: "+ results.get(size).frequency
+ "\n"+
"level
: "+ results.get(size).level
+ "\n"
);
arraylist.add(item);
size--;
adapter.notifyDataSetChanged();
}
}
catch
(Exception e)
{ }
}
}
in this example
BSSID The address of the access point.
SSID The network name.
capabilities Describes the authentication, key management, and encryption schemes supported by the access point.
frequency The frequency in MHz of the channel over which the client is communicating with the access point.
level The detected signal level in dBm.
timestamp Time Synchronization Function (tsf) timestamp in microseconds when this result was last seen.
and dont forget to include permissions in manifest
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"
/>
<uses-permission
android:name="android.permission.CHANGE_WIFI_STATE"
/>
Happppppy Codddding :)
No comments:
Post a Comment