Android 控制WIFI相關操做做者:徐福安 發表於 2011-5-29 22:36:25 評論(0) 閱讀(1156)
Android 控制WIFI相關操做
WIFI的全稱是Wireless Fidelity,又稱802.11b標準,是一種高速的無線通訊協議,傳輸速度能夠達到11Mb/s。實際上,對WIFI並不須要過多的控制(當成功鏈接WIFI後,就能夠直接經過IP在WIFI設備之間進行通訊了),通常只須要控制打開或關閉WIFI以及得到一些與WIFI相關的信息(例如,MAC地址、IP等)。若是讀者的Android手機有WIFI功能,能夠在手機上測試本節的例子。要注意的是,WIFI功能不能在Android模擬器上測試,就算在有WIFI功能的真機上也須要先經過WIFI和計算機或其餘WIFI設備鏈接後,才能得到與WIFI相關的信息。
本節的例子能夠關閉和開始WIFI,並得到各類與WIFI相關的信息。首先確認手機經過WIFI與其餘WIFI設備成功鏈接,而後運行本節的例子,會看到如圖所示的輸出信息。
本例的完整實現代碼以下:
Java代碼
package eoe.demo.wifi;
import java.net.Inet4Address;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class Main extends Activity implements OnCheckedChangeListener {
private WifiManager wifiManager;
private WifiInfo wifiInfo;
private CheckBox chkOpenCloseWifiBox;
private List<WifiConfiguration> wifiConfigurations;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 得到WifiManager對象
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// 得到鏈接信息對象
wifiInfo = wifiManager.getConnectionInfo();
chkOpenCloseWifiBox = (CheckBox) findViewById(R.id.chkOpenCloseWifi);
TextView tvWifiConfigurations = (TextView) findViewById(R.id.tvWifiConfigurations);
TextView tvWifiInfo = (TextView) findViewById(R.id.tvWifiInfo); chkOpenCloseWifiBox.setOnCheckedChangeListener(this);
// 根據當前WIFI的狀態(是否被打開)設置複選框的選中狀態
if (wifiManager.isWifiEnabled()) {
chkOpenCloseWifiBox.setText("Wifi已開啓");
chkOpenCloseWifiBox.setChecked(true);
} else {
chkOpenCloseWifiBox.setText("Wifi已關閉");
chkOpenCloseWifiBox.setChecked(false);
}
// 得到WIFI信息
StringBuffer sb = new StringBuffer();
sb.append("Wifi信息\n");
sb.append("MAC地址:" + wifiInfo.getMacAddress() + "\n");
sb.append("接入點的BSSID:" + wifiInfo.getBSSID() + "\n");
sb.append("IP地址(int):" + wifiInfo.getIpAddress() + "\n");
sb.append("IP地址(Hex):" + Integer.toHexString(wifiInfo.getIpAddress()) + "\n"); sb.append("IP地址:" + ipIntToString(wifiInfo.getIpAddress()) + "\n");
sb.append("網絡ID:" + wifiInfo.getNetworkId() + "\n");
tvWifiInfo.setText(sb.toString());
// 獲得配置好的網絡
wifiConfigurations = wifiManager.getConfiguredNetworks();
tvWifiConfigurations.setText("已鏈接的無線網絡\n");
for (WifiConfiguration wifiConfiguration : wifiConfigurations) {
tvWifiConfigurations.setText(tvWifiConfigurations.getText() + wifiConfiguration.SSID + "\n");
}
}
// 將int類型的IP轉換成字符串形式的IP
private String ipIntToString(int ip) {
try {
byte[] bytes = new byte[4];
bytes[0] = (byte) (0xff & ip);
bytes[1] = (byte) ((0xff00 & ip) >> 8);
bytes[2] = (byte) ((0xff0000 & ip) >> 16);
bytes[3] = (byte) ((0xff000000 & ip) >> 24);
return Inet4Address.getByAddress(bytes).getHostAddress();
} catch (Exception e) {
return "";
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 當選中複選框時打開WIFI
if (isChecked) {
wifiManager.setWifiEnabled(true);
chkOpenCloseWifiBox.setText("Wifi已開啓");
}
// 當取消複選框選中狀態時關閉WIFI
else {
wifiManager.setWifiEnabled(false);
chkOpenCloseWifiBox.setText("Wifi已關閉");
}
}
} java
在AndroidManifest.xml文件中要使用以下的代碼打開相應的權限。android
Java代碼:
XML/HTML代碼
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE">
</uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK">
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE">
</uses-permission> 網絡