The Wi-Fi sensor can read network status and determine nearby wireless access points.
The Android SDK provides a set of APIs for retrieving information about the Wi-Fi networks
available to the device and Wi-Fi network connection details.These APIs are separate
from the SensorManager APIs.This information can be used for tracking signal
strength, finding access points of interest, or performing actions when connected to specific
access points.This section describes how to get Wi-Fi information. However, if you
are looking for information on networking, that is more thoroughly discussed as part of
Chapter 12,"Using Android Networking APIs."
The following samples require two explicit permissions in the AndroidManifest.xml
file.The CHANGE_WIFI_STATE permission is needed when an application is accessing information
about Wi-Fi networks that can turn on the Wi-Fi radio, thus changing its state.
The ACCESS_WIFI_STATE permission is needed, as well, to request any information from
the Wi-Fi device.You can add these to the AndroidManifest.xml file as follows:
<uses-permission
android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE" />
The next thing the application needs is an instance of the WifiManager object. It is a system
service, so the getSystemService() method works.
WifiManager wifi =
(WifiManager) getSystemService(Context.WIFI_SERVICE);
Now that the WifiManager object is available, the application can do something interesting
or useful with it. First, the application performs a Wi-Fi scan to see what access points
are available in the local area.You need to complete a few steps to perform a scan:
1. Start the scan with the startScan() method of the WifiManager object.
2. Register a BroadcastReceiver for the SCAN_RESULTS_AVAILABLE Intent.
3. Call getScanResults() to get a list of ScanResult objects.
4. Iterate over the results and do something with them.
You can perform the first two steps with the following code:
wifi.startScan();
registerReceiver(rcvWifiScan,
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
The sample BroadcastReceiver object, shown here, performs the last two steps. It is
called regularly until the stopScan() method is called on the WifiManager object.
rcvWifiScan = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
List<ScanResult> resultList = wifi.getScanResults();
int foundCount = resultList.size();
Toast.makeText(WiFi.this,
"Scan done, " + foundCount + " found",
Toast.LENGTH_SHORT).show();
ListIterator<ScanResult> results = resultList.listIterator();
String fullInfo = "Scan Results : \n";
while (results.hasNext()) {
ScanResult info = results.next();
String wifiInfo = "Name: " + info.SSID +
"; capabilities = " + info.capabilities +
"; sig str = " + info.level + "dBm";
Log.v("WiFi", wifiInfo);
fullInfo += wifiInfo + "\n";
}
status.setText(fullInfo);
}
};
The ScanResult object contains a few more fields than demonstrated here. However, the
SSID, or name, property is probably the most recognizable to users.The capabilities
property lists such things as what security model can be used (such as "WEP").The signal
strength (level), as given, isn't all that descriptive for most users.
However, the WifiManager object provides a couple of helper methods for dealing
with signal levels.The first is the calculateSignalLevel() that effectively turns the
number into a particular number of "bars" of strength.You can use the second,
compareSignalLevel(), to compare the relative signal strengths of two results.
You can use the WifiManager object to list known access points.These are typically access
points that the user has configured or connected to in the past.The following code
demonstrates the use of the getConfiguredNetworks() method:
ListIterator<WifiConfiguration> configs =
wifi.getConfiguredNetworks().listIterator();
String allConfigs = "Configs: \n";
while (configs.hasNext()) {
WifiConfiguration config = configs.next();
String configInfo = "Name: " + config.SSID +
"; priority = " + config.priority;
Log.v("WiFi", configInfo);
allConfigs += configInfo + "\n";
}
status.setText(allConfigs);
The returned WifiConfiguration object does not include all the fields that it could. For
instance, it does not fill any network key fields. It does, however, fill in similar fields to
those found in the ScanResults object.This could be used, for instance, to notify the
users when they are in range of known Wi-Fi networks if their devices are set to not
automatically connect.
You can use the WifiManager object to configure Wi-Fi networks, get the state of the
Wi-Fi radio, and more. See the android.net.wifi package for more information.
0 件のコメント:
コメントを投稿