xmlns:android=\
3. package=\
android:versionName=\4. 5.
6.
7.
8.
9. 10. 11. 12. 13.
14. 15. 16. 17. 18. 19. 20. 21.
复制代码
至此这个Demo就完成了,看效果图。
```````````````````````````````````````````````
本文主要在此基础上,进一步完善程序功能,主要功能有打开无线网卡、关闭无线网卡、检查网卡状态、扫描网络、扫描结果、连接Wifi、断开Wifi、Wifi连接状态。 运行效果图如下:
其中连接Wifi是比较让我头疼的。需要在程序中进行连接,这会有两种情况。第一,Wifi没有密码,可以直接连接;第二,Wifi有密码,在程序中给出密码,然后连接。第一种我查阅相关资料好像确实是可以实现的,第二种我没找到,我自己也不知道该怎么去搞。最后想出一个办法,直接让程序跳到系统设置的Wifi的页面,然后让人手动去设置。观察了一下Android版的QQ,也确实是这么做的。不信你瞧:
点击设置网络后跳转到系统设置页面:
所以此处设置网络我也可以这么做,不过还是有点不爽,可能自己对Wifi这块还不是很熟悉,我会继续查阅相关API文档,到时候再跟大家分享,好了,下面贴一下源代码: 主页面:
1. package org.sunchao; 2.
3. import android.app.Activity; 4. import android.content.Intent; 5. import android.os.Bundle; 6. import android.view.View;
7. import android.view.View.OnClickListener; 8. import android.widget.Button; 9. import android.widget.ScrollView; 10. import android.widget.TextView; 11.
12. public class Main extends Activity implements OnClickListener { 13. // 右侧滚动条按钮
14. private ScrollView sView; 15. private Button openNetCard; 16. private Button closeNetCard; 17. private Button checkNetCardState; 18. private Button scan;
19. private Button getScanResult; 20. private Button connect; 21. private Button disconnect;
22. private Button checkNetWorkState; 23. private TextView scanResult; 24.
25. private String mScanResult; 26. private WifiAdmin mWifiAdmin; 27.
28. /** Called when the activity is first created. */ 29. @Override
30. public void onCreate(Bundle savedInstanceState) { 31. super.onCreate(savedInstanceState); 32. setContentView(R.layout.main);
33. mWifiAdmin = new WifiAdmin(Main.this); 34. init(); 35. } 36.
37. /**
38. * 按钮等控件的初始化 39. */
40. public void init() {
41. sView = (ScrollView) findViewById(R.id.mScrollView); 42. openNetCard = (Button)
findViewById(R.id.openNetCard);
43. closeNetCard = (Button)
findViewById(R.id.closeNetCard);
44. checkNetCardState = (Button)
findViewById(R.id.checkNetCardState);
45. scan = (Button) findViewById(R.id.scan); 46. getScanResult = (Button)
findViewById(R.id.getScanResult); 47. scanResult = (TextView)
findViewById(R.id.scanResult);
48. connect = (Button) findViewById(R.id.connect); 49. disconnect = (Button) findViewById(R.id.disconnect); 50. checkNetWorkState = (Button)
findViewById(R.id.checkNetWorkState); 51.
52. openNetCard.setOnClickListener(Main.this); 53. closeNetCard.setOnClickListener(Main.this); 54. checkNetCardState.setOnClickListener(Main.this); 55. scan.setOnClickListener(Main.this);
56. getScanResult.setOnClickListener(Main.this); 57. connect.setOnClickListener(Main.this); 58. disconnect.setOnClickListener(Main.this);
59. checkNetWorkState.setOnClickListener(Main.this); 60. } 61.
62. /**
63. * WIFI_STATE_DISABLING 0 WIFI_STATE_DISABLED 1
WIFI_STATE_ENABLING 2
64. * WIFI_STATE_ENABLED 3 65. */
66. public void openNetCard() { 67. mWifiAdmin.openNetCard(); 68. } 69.
70. public void closeNetCard() { 71. mWifiAdmin.closeNetCard(); 72. } 73.
74. public void checkNetCardState() { 75. mWifiAdmin.checkNetCardState(); 76. } 77.
78. public void scan() { 79. mWifiAdmin.scan(); 80. } 81.
82. public void getScanResult() {
83. mScanResult = mWifiAdmin.getScanResult(); 84. scanResult.setText(mScanResult); 85. } 86.
87. public void connect() { 88. mWifiAdmin.connect();
89. // startActivityForResult(new Intent(
90. // android.provider.Settings.ACTION_
WIFI_SETTINGS), 0);
91. startActivity(new
Intent(android.provider.Settings.ACTION_WIFI_SETTINGS)); 92. } 93.
94. public void disconnect() {
95. mWifiAdmin.disconnectWifi(); 96. } 97.
98. public void checkNetWorkState() { 99. mWifiAdmin.checkNetWorkState(); 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119.
}
@Override
public void onClick(View v) { switch (v.getId()) { case R.id.openNetCard: openNetCard(); break;
case R.id.closeNetCard: closeNetCard(); break;
case R.id.checkNetCardState: checkNetCardState(); break; case R.id.scan: scan(); break;
case R.id.getScanResult: getScanResult(); break;
120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134.
复制代码
case R.id.connect: connect(); break;
case R.id.disconnect: disconnect(); break;
case R.id.checkNetWorkState: checkNetWorkState(); break; default: break; } } }
Wifi工具类:
1. package org.sunchao; 2.
3. import java.util.List; 4.
5. import android.content.Context; 6. import android.net.wifi.ScanResult;
7. import android.net.wifi.WifiConfiguration; 8. import android.net.wifi.WifiInfo; 9. import android.net.wifi.WifiManager;
10. import android.net.wifi.WifiManager.WifiLock; 11. import android.util.Log; 12.
13. public class WifiAdmin {
14. private final static String TAG = \
15. private StringBuffer mStringBuffer = new StringBuffer(); 16. private List listResult;
17. private ScanResult mScanResult; 18. // 定义WifiManager对象
19. private WifiManager mWifiManager; 20. // 定义WifiInfo对象
21. private WifiInfo mWifiInfo; 22. // 网络连接列表
23. private List mWifiConfiguration; 24. // 定义一个WifiLock 25. WifiLock mWifiLock; 26.
27. /** 28. * 构造方法 29. */
30. public WifiAdmin(Context context) {
31. mWifiManager = (WifiManager) context
32. .getSystemService(Context.WIFI_SERV
ICE);
33. mWifiInfo = mWifiManager.getConnectionInfo(); 34. } 35.
36. /**
37. * 打开Wifi网卡 38. */
39. public void openNetCard() {
40. if (!mWifiManager.isWifiEnabled()) { 41. mWifiManager.setWifiEnabled(true); 42. } 43. } 44.
45. /**
46. * 关闭Wifi网卡 47. */
48. public void closeNetCard() {
49. if (mWifiManager.isWifiEnabled()) {
50. mWifiManager.setWifiEnabled(false); 51. } 52. } 53.
54. /**
55. * 检查当前Wifi网卡状态 56. */
57. public void checkNetCardState() {
58. if (mWifiManager.getWifiState() == 0) { 59. Log.i(TAG, \网卡正在关闭\
60. } else if (mWifiManager.getWifiState() == 1) { 61. Log.i(TAG, \网卡已经关闭\
62. } else if (mWifiManager.getWifiState() == 2) { 63. Log.i(TAG, \网卡正在打开\
64. } else if (mWifiManager.getWifiState() == 3) { 65. Log.i(TAG, \网卡已经打开\66. } else {
67. Log.i(TAG, \晕......没有获取到状态
---_---\68. } 69. } 70.
71. /**
72. * 扫描周边网络 73. */
74. public void scan() {
75. mWifiManager.startScan();
76. listResult = mWifiManager.getScanResults();
77. if (listResult != null) {
78. Log.i(TAG, \当前区域存在无线网络,请查看扫描结果
\
79. } else {
80. Log.i(TAG, \当前区域没有无线网络\81. } 82. } 83.
84. /**
85. * 得到扫描结果 86. */
87. public String getScanResult() {
88. // 每次点击扫描之前清空上一次的扫描结果 89. if (mStringBuffer != null) {
90. mStringBuffer = new StringBuffer(); 91. }
92. // 开始扫描网络 93. scan();
94. listResult = mWifiManager.getScanResults(); 95. if (listResult != null) {
96. for (int i = 0; i < listResult.size(); i++)
{
97. mScanResult = listResult.get(i); 98. mStringBuffer =
mStringBuffer.append(\
99. .append(\
(mScanResult.SSID).append(\100.
.append(mScanRe
sult.BSSID).append(\101.
.append(mScanRe
sult.capabilities).append(\
102. .append(mScanRe
sult.frequency).append(\103.
.append(mScanRe
sult.level).append(\104.
.append(mScanRe
sult.describeContents()).append(\105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130.
} }
Log.i(TAG, mStringBuffer.toString()); return mStringBuffer.toString(); }
/**
* 连接指定网络 */
public void connect() {
mWifiInfo = mWifiManager.getConnectionInfo(); }
/**
* 断开当前连接的网络 */
public void disconnectWifi() { int netId = getNetworkId();
mWifiManager.disableNetwork(netId); mWifiManager.disconnect(); mWifiInfo = null; }
/**
* 检查当前网络状态
131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146.
*
* @return String */
public void checkNetWorkState() { if (mWifiInfo != null) {
Log.i(TAG, \网络正常工作\ } else {
Log.i(TAG, \网络已断开\ } }
/**
* 得到连接的ID */
public int getNetworkId() {
return (mWifiInfo == null) ? 0 :
mWifiInfo.getNetworkId(); 147. 148. 149. 150. 151. 152. 153.
}
/**
* 得到IP地址 */
public int getIPAddress() {
return (mWifiInfo == null) ? 0 :
mWifiInfo.getIpAddress(); 154. 155. 156. 157. 158. 159. 160.
}
// 锁定WifiLock
public void acquireWifiLock() { mWifiLock.acquire(); }
161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171.
// 解锁WifiLock
public void releaseWifiLock() { // 判断时候锁定
if (mWifiLock.isHeld()) { mWifiLock.acquire(); } }
// 创建一个WifiLock
public void creatWifiLock() { mWifiLock =
mWifiManager.createWifiLock(\172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186.
}
// 得到配置好的网络
public List getConfiguration() { return mWifiConfiguration; }
// 指定配置好的网络进行连接
public void connectConfiguration(int index) { // 索引大于配置好的网络索引返回
if (index >= mWifiConfiguration.size()) { return; }
// 连接配置好的指定ID的网络
mWifiManager.enableNetwork(mWifiConfiguration.
get(index).networkId, 187. 188. 189. 190.
true); }
// 得到MAC地址
191. 192.
public String getMacAddress() {
return (mWifiInfo == null) ? \
mWifiInfo.getMacAddress(); 193. 194. 195. 196. 197.
}
// 得到接入点的BSSID
public String getBSSID() {
return (mWifiInfo == null) ? \
mWifiInfo.getBSSID(); 198. 199. 200. 201. 202.
}
// 得到WifiInfo的所有信息包 public String getWifiInfo() {
return (mWifiInfo == null) ? \
mWifiInfo.toString(); 203. 204. 205. 206. 207.
}
// 添加一个网络并连接
public int addNetwork(WifiConfiguration wcg) { int wcgID =
mWifiManager.addNetwork(mWifiConfiguration.get(3)); 208. 209. 210. 211. 212.
复制代码
mWifiManager.enableNetwork(wcgID, true); return wcgID; } }
AndroidManifest.xml:(注意需要添加的权限)
1.
xmlns:android=\
3. package=\
android:versionName=\
4. 6.
android:label=\
7.
android:label=\8. 9. android:name=\10. android:name=\11.
12. 13.
14.
15. 16. android:name=\mission>
17. android:name=\sion>
18. android:name=\mission>
19. android:name=\sion> 20.
21.