橘子味的心
标题:8.12 Android WiFi开发实例演示

Android SDK 提供了 WIFI 开发的相关 API,被保存在 android.net.wifi 和 android.net.wifi.p2p 包下。借助于 Android SDK 提供的相关开发类,可以方便地在 Android 系统的手机上开发基于 WIFI 的应用程序。

实例 WIFIDemo 演示了使用 WIFI 进行连接设备搜索并获取相应信息的过程,运行效果如图 1 所示。

WIFIDemo 运行结果
图 1  WIFIDemo 运行结果

实例 WIFIDemo 中所使用的布局文件 main.xml 的内容如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/mScrollView"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:scrollbars="vertical">
  7.  
  8. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. android:orientation="vertical">
  12.  
  13. <LinearLayout
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:orientation="horizontal">
  17.  
  18. <Button
  19. android:id="@+id/open_bt"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="打开wifi" />
  23.  
  24. <Button
  25. android:id="@+id/close_bt"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="关闭wifi" />
  29.  
  30. <Button
  31. android:id="@+id/check_bt"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:text="检查wifi" />
  35.  
  36. <Button
  37. android:id="@+id/search_bt"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:text="扫描wifi" />
  41. </LinearLayout>
  42.  
  43. <TextView
  44. android:id="@+id/text"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:text="null" />
  48. </LinearLayout>
  49. </ScrollView>
实例 WIFIDemo 中 AndroidManifest.xml 文件的代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="introduction.android.wifidemo">
  4.  
  5. <uses-sdk android:minSdkVersion="10" />
  6.  
  7. <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
  8. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  9. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  10. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  11. <application
  12. android:allowBackup="true"
  13. android:icon="@mipmap/ic_launcher"
  14. android:label="@string/app_name"
  15. android:roundIcon="@mipmap/ic_launcher_round"
  16. android:supportsRtl="true"
  17. android:theme="@style/AppTheme">
  18. <activity android:name=".MainActivity">
  19. <intent-filter>
  20. <action android:name="android.intent.action.MAIN" />
  21. <category android:name="android.intent.category.LAUNCHER" />
  22. </intent-filter>
  23. </activity>
  24. </application>
  25.  
  26. </manifest>
实例 WIFIDemo 中主 Activity 文件 MainActivity.java 的代码如下:
  1. package introduction.android.wifidemo;
  2.  
  3. import java.util.List;
  4.  
  5. import android.R.string;
  6. import android.app.Activity;
  7. import android.content.Context;
  8. import android.net.wifi.ScanResult;
  9. import android.net.wifi.WifiInfo;
  10. import android.net.wifi.WifiManager;
  11. import android.os.Bundle;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;
  15. import android.widget.ScrollView;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18.  
  19. public class MainActivity extends Activity {
  20. private Button open_bt, close_bt, check_bt, search_bt;
  21. private TextView textView;
  22. private WifiManager wifiManager;
  23. private WifiInfo wifiInfo;
  24. private ScrollView scrollView;
  25. private List WifiConfiguration;
  26. private ScanResult scanResult;
  27. private List<ScanResult> WifiList;
  28. private StringBuffer stringBuffer = new StringBuffer();
  29.  
  30. /**
  31. * Called when the activity is first created.
  32. */
  33. public void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.activity_main);
  36.  
  37. scrollView = (ScrollView) findViewById(R.id.mScrollView);
  38. open_bt = (Button) findViewById(R.id.open_bt);
  39. close_bt = (Button) findViewById(R.id.close_bt);
  40. check_bt = (Button) findViewById(R.id.check_bt);
  41. search_bt = (Button) findViewById(R.id.search_bt);
  42. textView = (TextView) findViewById(R.id.text);
  43.  
  44. open_bt.setOnClickListener(new open_btListener());
  45. close_bt.setOnClickListener(new close_btListener());
  46. check_bt.setOnClickListener(new check_btListener());
  47. search_bt.setOnClickListener(new search_btListener());
  48.  
  49. }
  50.  
  51. class search_btListener implements OnClickListener {
  52. public void onClick(View v) {
  53. // TODO Auto-generated method stub
  54. wifiManager.startScan();
  55. WifiList = wifiManager.getScanResults();
  56. wifiInfo = wifiManager.getConnectionInfo();
  57. if (stringBuffer != null) {
  58. stringBuffer = new StringBuffer();
  59. }
  60. stringBuffer = stringBuffer.append("Wifi名").append(" ").append("Wifi地址").append(" ")
  61. .append("Wifi频率").append("").append("Wifi信号")
  62. .append("\n");
  63. if (WifiList != null) {
  64. for (int i = 0; i < WifiList.size(); i++) {
  65. scanResult = WifiList.get(i);
  66. stringBuffer = stringBuffer.append(scanResult.SSID).append(" ")
  67. .append(scanResult.BSSID).append(" ")
  68. .append(scanResult.frequency).append(" ")
  69. .append(scanResult.level).append("\n");
  70. textView.setText(stringBuffer.toString());
  71. }
  72.  
  73. stringBuffer = stringBuffer.append
  74. ("-----------------------------------------------").append("\n");
  75. textView.setText(stringBuffer.toString());
  76. stringBuffer = stringBuffer.append("当前Wifi—BSSID").append(":").append(wifiInfo.getBSSID()).append("\n")
  77. .append("当前wifi—HiddenSSID").append(": ").append(wifiInfo.getHiddenSSID()).append("\n")
  78. .append("当前Wifi—IpAddress").append(": ").append(wifiInfo.getIpAddress()).append("\n")
  79. .append("当前Wifi—LinkSpeed").append(": ").append(wifiInfo.getLinkSpeed()).append("\n")
  80. .append("当前Wifi—MacAddress").append(": ").append(wifiInfo.getMacAddress()).append("\n")
  81. .append("当前Wifi—Network ID").append(": ").append(wifiInfo.getNetworkId()).append("\n")
  82. .append("当前Wifi—RSSI").append(": ").append(wifiInfo.getRssi()).append("\n")
  83. .append("当前Wifi—SSID").append(": ").append(wifiInfo.getSSID()).append("\n")
  84. .append("-----------------------------------------------").append("\n")
  85. .append("全部打印出关于本机Wifi信息").append(": ").append
  86. (wifiInfo.toString());
  87. textView.setText(stringBuffer.toString());
  88. }
  89. //stringBuffer=stringBuffer.append("-----------------------------------------------").append("\n");
  90. //textView.setText()
  91. }
  92. }
  93.  
  94. class check_btListener implements OnClickListener {
  95. public void onClick(View v) {
  96. // TODO Auto-generated method stub
  97. wifiManager = (WifiManager) MainActivity.this
  98. .getSystemService(Context.WIFI_SERVICE);
  99. System.out.println(wifiManager.getWifiState());
  100. Toast.makeText(MainActivity.this,
  101. "当前网卡状态为:" + change(), Toast.LENGTH_SHORT)
  102. .show();
  103. }
  104. }
  105.  
  106.  
  107. class close_btListener implements OnClickListener {
  108. public void onClick(View v) {
  109. // TODO Auto-generated method stub
  110. wifiManager = (WifiManager) MainActivity.this
  111. .getSystemService(Context.WIFI_SERVICE);
  112. wifiManager.setWifiEnabled(false);
  113. System.out.println(wifiManager.getWifiState());
  114. Toast.makeText(MainActivity.this,
  115. "当前网卡状态为" + change(), Toast.LENGTH_SHORT)
  116. .show();
  117. }
  118. }
  119.  
  120. class open_btListener implements OnClickListener {
  121. public void onClick(View v) {
  122. // TODO Auto-generated method stub
  123. wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
  124. wifiManager.setWifiEnabled(true);
  125. System.out.println(wifiManager.getWifiState());
  126. Toast.makeText(MainActivity.this,
  127. "当前网卡状态为" + change(), Toast.LENGTH_SHORT)
  128. .show();
  129. }
  130. }
  131.  
  132. public String change() {
  133. String temp = null;
  134. switch (wifiManager.getWifiState()) {
  135. case 0:
  136. temp = "wifi正在关闭ING";
  137. break;
  138. case 1:
  139. temp = "wifi已经关闭";
  140. break;
  141. case 2:
  142. temp = "wifi正在打开ING";
  143. break;
  144. case 3:
  145. temp = "wifi已经打开";
  146. break;
  147. default:
  148. break;
  149. }
  150. return temp;
  151. }
  152. }