当前位置: 代码迷 >> Android >> Android 联接Wifi和创建Wifi热点
  详细解决方案

Android 联接Wifi和创建Wifi热点

热度:77   发布时间:2016-04-28 07:34:48.0
Android 连接Wifi和创建Wifi热点

上代码:

MainActivity.java

?

[java]?view plaincopy
?
  1. package?com.widget.hotspot;??
  2. ??
  3. import?android.os.Bundle;??
  4. import?android.app.Activity;??
  5. import?android.content.BroadcastReceiver;??
  6. import?android.content.Context;??
  7. import?android.content.Intent;??
  8. import?android.content.IntentFilter;??
  9. import?android.util.Log;??
  10. import?android.view.Menu;??
  11. import?android.view.View;??
  12. import?android.widget.Button;??
  13. ??
  14. public?class?MainActivity?extends?Activity?{??
  15. ??
  16. ????public?static?final?String?TAG?=?"MainActivity";??
  17. ??????
  18. ????private?Button?mBtn1,?mBtn2;??
  19. ??????
  20. ????private?WifiAdmin?mWifiAdmin;??
  21. ??????
  22. ????private?Context?mContext?=?null;??
  23. ????@Override??
  24. ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  25. ????????super.onCreate(savedInstanceState);??
  26. ??????????
  27. ????????mContext?=?this;??
  28. ??????????
  29. ????????setContentView(R.layout.activity_main);??
  30. ??????????
  31. ????????mBtn1?=?(Button)findViewById(R.id.button1);??
  32. ????????mBtn2?=?(Button)findViewById(R.id.button2);??
  33. ????????mBtn1.setText("点击连接Wifi");??
  34. ????????mBtn2.setText("点击创建Wifi热点");??
  35. ????????mBtn1.setOnClickListener(new?Button.OnClickListener()?{??
  36. ??????????????
  37. ????????????@Override??
  38. ????????????public?void?onClick(View?v)?{??
  39. ????????????????//?TODO?Auto-generated?method?stub??
  40. ??
  41. ????????????????mWifiAdmin?=?new?WifiAdmin(mContext)?{??
  42. ??????????????????????
  43. ????????????????????@Override??
  44. ????????????????????public?void?myUnregisterReceiver(BroadcastReceiver?receiver)?{??
  45. ????????????????????????//?TODO?Auto-generated?method?stub??
  46. ????????????????????????MainActivity.this.unregisterReceiver(receiver);??
  47. ????????????????????}??
  48. ??????????????????????
  49. ????????????????????@Override??
  50. ????????????????????public?Intent?myRegisterReceiver(BroadcastReceiver?receiver,??
  51. ????????????????????????????IntentFilter?filter)?{??
  52. ????????????????????????//?TODO?Auto-generated?method?stub??
  53. ????????????????????????MainActivity.this.registerReceiver(receiver,?filter);??
  54. ????????????????????????return?null;??
  55. ????????????????????}??
  56. ??????????????????????
  57. ????????????????????@Override??
  58. ????????????????????public?void?onNotifyWifiConnected()?{??
  59. ????????????????????????//?TODO?Auto-generated?method?stub??
  60. ????????????????????????Log.v(TAG,?"have?connected?success!");??
  61. ????????????????????????Log.v(TAG,?"###############################");??
  62. ????????????????????}??
  63. ??????????????????????
  64. ????????????????????@Override??
  65. ????????????????????public?void?onNotifyWifiConnectFailed()?{??
  66. ????????????????????????//?TODO?Auto-generated?method?stub??
  67. ????????????????????????Log.v(TAG,?"have?connected?failed!");??
  68. ????????????????????????Log.v(TAG,?"###############################");??
  69. ????????????????????}??
  70. ????????????????};??
  71. ????????????????mWifiAdmin.openWifi();??
  72. ????????????????mWifiAdmin.addNetwork(mWifiAdmin.createWifiInfo("YOU_WIFI",?"MM123456",?WifiAdmin.TYPE_WPA));??
  73. ??????????????????
  74. ????????????}??
  75. ????????});??
  76. ??????????
  77. ????????mBtn2.setOnClickListener(new?Button.OnClickListener()?{??
  78. ??????????????
  79. ????????????@Override??
  80. ????????????public?void?onClick(View?v)?{??
  81. ????????????????//?TODO?Auto-generated?method?stub??
  82. ??????????????????
  83. ????????????????WifiApAdmin?wifiAp?=?new?WifiApAdmin(mContext);??
  84. ????????????????wifiAp.startWifiAp("\"HotSpot\"",?"hhhhhh123");??
  85. ????????????}??
  86. ????????});??
  87. ??????????
  88. ????}??
  89. ??
  90. ????@Override??
  91. ????public?boolean?onCreateOptionsMenu(Menu?menu)?{??
  92. ????????//?Inflate?the?menu;?this?adds?items?to?the?action?bar?if?it?is?present.??
  93. ????????getMenuInflater().inflate(R.menu.activity_main,?menu);??
  94. ????????return?true;??
  95. ????}??
  96. ??
  97. ?????@Override??
  98. ????????public?void?onResume()?{??
  99. ????????????super.onResume();??
  100. ??????????????
  101. ????????????Log.d("Rssi",?"Registered");??
  102. ????????}??
  103. ??
  104. ????????@Override??
  105. ????????public?void?onPause()?{??
  106. ????????????super.onPause();??
  107. ??????????????
  108. ????????????Log.d("Rssi",?"Unregistered");??
  109. ????????}??
  110. ??????
  111. }??

?

?

WifiAdmin.java

参考了://http://blog.csdn.net/yuanbohx/article/details/8109042

?

[java]?view plaincopy
?
  1. package?com.widget.hotspot;??
  2. ??
  3. import?java.util.List;??
  4. import?java.util.Timer;??
  5. import?java.util.TimerTask;??
  6. import?android.content.BroadcastReceiver;??
  7. import?android.content.Context;??
  8. import?android.content.Intent;??
  9. import?android.content.IntentFilter;??
  10. import?android.net.ConnectivityManager;??
  11. import?android.net.NetworkInfo;??
  12. import?android.net.NetworkInfo.DetailedState;??
  13. import?android.net.wifi.ScanResult;??
  14. import?android.net.wifi.WifiConfiguration;??
  15. import?android.net.wifi.WifiInfo;??
  16. import?android.net.wifi.WifiManager;??
  17. import?android.net.wifi.WifiManager.WifiLock;??
  18. import?android.util.Log;??
  19. ??
  20. ??
  21. public?abstract?class?WifiAdmin?{??
  22. ??????
  23. ????private?static?final?String?TAG?=?"WifiAdmin";??
  24. ??????
  25. ????private?WifiManager?mWifiManager;??
  26. ????private?WifiInfo?mWifiInfo;??
  27. ????//?扫描出的网络连接列表??
  28. ????private?List<ScanResult>?mWifiList;??
  29. ????private?List<WifiConfiguration>?mWifiConfiguration;??
  30. ??
  31. ????private?WifiLock?mWifiLock;??
  32. ??????
  33. ????private?String?mPasswd?=?"";??
  34. ????private?String?mSSID?=?"";??
  35. ??????
  36. ????private?Context?mContext?=?null;??
  37. ??
  38. ????public?WifiAdmin(Context?context)?{??
  39. ??????????
  40. ????????mContext?=?context;??
  41. ??????????
  42. ????????//?取得WifiManager对象??
  43. ????????mWifiManager?=?(WifiManager)?context.getSystemService(Context.WIFI_SERVICE);??
  44. ????????//?取得WifiInfo对象??
  45. ????????mWifiInfo?=?mWifiManager.getConnectionInfo();??
  46. ??????????
  47. ????????Log.v(TAG,?"getIpAddress?=?"?+?mWifiInfo.getIpAddress());??
  48. ????}??
  49. ??
  50. ????//?打开WIFI??
  51. ????public?void?openWifi()?{??
  52. ????????if?(!mWifiManager.isWifiEnabled())?{??
  53. ????????????mWifiManager.setWifiEnabled(true);??
  54. ????????}??
  55. ????}??
  56. ??
  57. ????//?关闭WIFI??
  58. ????public?void?closeWifi()?{??
  59. ????????if?(mWifiManager.isWifiEnabled())?{??
  60. ????????????mWifiManager.setWifiEnabled(false);??
  61. ????????}??
  62. ????}??
  63. ??
  64. ????public?abstract?Intent?myRegisterReceiver(BroadcastReceiver?receiver,?IntentFilter?filter);??
  65. ??????
  66. ????public?abstract?void?myUnregisterReceiver(BroadcastReceiver?receiver);??
  67. ??????
  68. ????public?abstract?void?onNotifyWifiConnected();??
  69. ??????
  70. ????public?abstract?void?onNotifyWifiConnectFailed();??
  71. ??????
  72. ????//?添加一个网络并连接??
  73. ????public?void?addNetwork(WifiConfiguration?wcg)?{??
  74. ??????????
  75. ????????register();??
  76. ??????????
  77. ????????WifiApAdmin.closeWifiAp(mContext);??
  78. ??????????
  79. ????????int?wcgID?=?mWifiManager.addNetwork(wcg);??
  80. ????????boolean?b?=?mWifiManager.enableNetwork(wcgID,?true);??
  81. ????}??
  82. ??????
  83. ????public?static?final?int?TYPE_NO_PASSWD?=?0x11;??
  84. ????public?static?final?int?TYPE_WEP?=?0x12;??
  85. ????public?static?final?int?TYPE_WPA?=?0x13;??
  86. ??????
  87. ????public?void?addNetwork(String?ssid,?String?passwd,?int?type)?{??
  88. ????????if?(ssid?==?null?||?passwd?==?null?||?ssid.equals(""))?{??
  89. ????????????Log.e(TAG,?"addNetwork()?##?nullpointer?error!");??
  90. ????????????return;??
  91. ????????}??
  92. ??????????
  93. ????????if?(type?!=?TYPE_NO_PASSWD?&&?type?!=?TYPE_WEP?&&?type?!=?TYPE_WPA)?{??
  94. ????????????Log.e(TAG,?"addNetwork()?##?unknown?type?=?"?+?type);??
  95. ????????}??
  96. ??????????
  97. ????????stopTimer();??
  98. ????????unRegister();??
  99. ??????????
  100. ????????addNetwork(createWifiInfo(ssid,?passwd,?type));??
  101. ????}??
  102. ??
  103. ????private?BroadcastReceiver?mBroadcastReceiver?=?new?BroadcastReceiver()?{??
  104. ??
  105. ????????@Override??
  106. ????????public?void?onReceive(Context?context,?Intent?intent)?{??
  107. ????????????//?TODO?Auto-generated?method?stub??
  108. ????????????if?(intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION))?{??
  109. ????????????????Log.d(TAG,?"RSSI?changed");??
  110. ??????????????????
  111. ????????????????//有可能是正在获取,或者已经获取了??
  112. ????????????????Log.d(TAG,?"?intent?is?"?+?WifiManager.RSSI_CHANGED_ACTION);??
  113. ??????????????????
  114. ????????????????if?(isWifiContected(mContext)?==?WIFI_CONNECTED)?{??
  115. ????????????????????stopTimer();??
  116. ????????????????????onNotifyWifiConnected();??
  117. ????????????????????unRegister();??
  118. ????????????????}?else?if?(isWifiContected(mContext)?==?WIFI_CONNECT_FAILED)?{??
  119. ????????????????????stopTimer();??
  120. ????????????????????closeWifi();??
  121. ????????????????????onNotifyWifiConnectFailed();??
  122. ????????????????????unRegister();??
  123. ????????????????}?else?if?(isWifiContected(mContext)?==?WIFI_CONNECTING)?{??
  124. ??????????????????????
  125. ????????????????}??
  126. ????????????}??
  127. ????????}??
  128. ????};??
  129. ??????
  130. ????private?final?int?STATE_REGISTRING?=?0x01;??
  131. ????private?final?int?STATE_REGISTERED?=?0x02;??
  132. ????private?final?int?STATE_UNREGISTERING?=?0x03;??
  133. ????private?final?int?STATE_UNREGISTERED?=?0x04;??
  134. ??????
  135. ????private?int?mHaveRegister?=?STATE_UNREGISTERED;??
  136. ????private?synchronized?void?register()?{??
  137. ????????Log.v(TAG,?"register()?##mHaveRegister?=?"?+?mHaveRegister);??
  138. ??
  139. ????????if?(mHaveRegister?==?STATE_REGISTRING???
  140. ????????????????||?mHaveRegister?==?STATE_REGISTERED)?{??
  141. ????????????return?;??
  142. ????????}??
  143. ??????????
  144. ????????mHaveRegister?=?STATE_REGISTRING;??
  145. ????????myRegisterReceiver(mBroadcastReceiver,?new?IntentFilter(WifiManager.RSSI_CHANGED_ACTION));??
  146. ????????mHaveRegister?=?STATE_REGISTERED;??
  147. ??????????
  148. ????????startTimer();??
  149. ????}??
  150. ??????
  151. ????private?synchronized?void?unRegister()?{??
  152. ????????Log.v(TAG,?"unRegister()?##mHaveRegister?=?"?+?mHaveRegister);??
  153. ??????????
  154. ????????if?(mHaveRegister?==?STATE_UNREGISTERED???
  155. ????????????????||?mHaveRegister?==?STATE_UNREGISTERING)?{??
  156. ????????????return?;??
  157. ????????}??
  158. ??????????
  159. ????????mHaveRegister?=?STATE_UNREGISTERING;??
  160. ????????myUnregisterReceiver(mBroadcastReceiver);??
  161. ????????mHaveRegister?=?STATE_UNREGISTERED;??
  162. ????}??
  163. ??????
  164. ????private?Timer?mTimer?=?null;??
  165. ????private?void?startTimer()?{??
  166. ????????if?(mTimer?!=?null)?{??
  167. ????????????stopTimer();??
  168. ????????}??
  169. ??????????
  170. ????????mTimer?=?new?Timer(true);??
  171. //??????mTimer.schedule(mTimerTask,?0,?20?*?1000);//?20s??
  172. ????????mTimer.schedule(mTimerTask,?30?*?1000);??
  173. ????}??
  174. ??????
  175. ????private?TimerTask?mTimerTask?=?new?TimerTask()?{??
  176. ??????????
  177. ????????@Override??
  178. ????????public?void?run()?{??
  179. ????????????//?TODO?Auto-generated?method?stub??
  180. ????????????Log.e(TAG,?"timer?out!");??
  181. ????????????onNotifyWifiConnectFailed();??
  182. ????????????unRegister();??
  183. ????????}??
  184. ????};??
  185. ??????
  186. ????private?void?stopTimer()?{??
  187. ????????if?(mTimer?!=?null)?{??
  188. ????????????mTimer.cancel();??
  189. ????????????mTimer?=?null;??
  190. ????????}??
  191. ????}??
  192. ??????
  193. ????@Override??
  194. ????protected?void?finalize()?{??
  195. ????????try?{??
  196. ????????????super.finalize();??
  197. ????????????unRegister();??
  198. ????????}?catch?(Throwable?e)?{??
  199. ????????????//?TODO?Auto-generated?catch?block??
  200. ????????????e.printStackTrace();??
  201. ????????}??
  202. ????}??
  203. ??????
  204. ????public?WifiConfiguration?createWifiInfo(String?SSID,?String?password,?int?type)?{??
  205. ??????????
  206. ????????Log.v(TAG,?"SSID?=?"?+?SSID?+?"##?Password?=?"?+?password?+?"##?Type?=?"?+?type);??
  207. ??????????
  208. ????????WifiConfiguration?config?=?new?WifiConfiguration();??
  209. ????????config.allowedAuthAlgorithms.clear();??
  210. ????????config.allowedGroupCiphers.clear();??
  211. ????????config.allowedKeyManagement.clear();??
  212. ????????config.allowedPairwiseCiphers.clear();??
  213. ????????config.allowedProtocols.clear();??
  214. ????????config.SSID?=?"\""?+?SSID?+?"\"";??
  215. ??
  216. ????????WifiConfiguration?tempConfig?=?this.IsExsits(SSID);??
  217. ????????if?(tempConfig?!=?null)?{??
  218. ????????????mWifiManager.removeNetwork(tempConfig.networkId);??
  219. ????????}??
  220. ??????????
  221. ????????//?分为三种情况:1没有密码2用wep加密3用wpa加密??
  222. ????????if?(type?==?TYPE_NO_PASSWD)?{//?WIFICIPHER_NOPASS??
  223. ????????????config.wepKeys[0]?=?"";??
  224. ????????????config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);??
  225. ????????????config.wepTxKeyIndex?=?0;??
  226. ??????????????
  227. ????????}?else?if?(type?==?TYPE_WEP)?{??//??WIFICIPHER_WEP???
  228. ????????????config.hiddenSSID?=?true;??
  229. ????????????config.wepKeys[0]?=?"\""?+?password?+?"\"";??
  230. ????????????config.allowedAuthAlgorithms??
  231. ????????????????????.set(WifiConfiguration.AuthAlgorithm.SHARED);??
  232. ????????????config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);??
  233. ????????????config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);??
  234. ????????????config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);??
  235. ????????????config.allowedGroupCiphers??
  236. ????????????????????.set(WifiConfiguration.GroupCipher.WEP104);??
  237. ????????????config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);??
  238. ????????????config.wepTxKeyIndex?=?0;??
  239. ????????}?else?if?(type?==?TYPE_WPA)?{???//?WIFICIPHER_WPA??
  240. ????????????config.preSharedKey?=?"\""?+?password?+?"\"";??
  241. ????????????config.hiddenSSID?=?true;??
  242. ????????????config.allowedAuthAlgorithms??
  243. ????????????????????.set(WifiConfiguration.AuthAlgorithm.OPEN);??
  244. ????????????config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);??
  245. ????????????config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);??
  246. ????????????config.allowedPairwiseCiphers??
  247. ????????????????????.set(WifiConfiguration.PairwiseCipher.TKIP);??
  248. ????????????//?config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);??
  249. ????????????config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);??
  250. ????????????config.allowedPairwiseCiphers??
  251. ????????????????????.set(WifiConfiguration.PairwiseCipher.CCMP);??
  252. ????????????config.status?=?WifiConfiguration.Status.ENABLED;??
  253. ????????}???
  254. ??????????
  255. ????????return?config;??
  256. ????}??
  257. ??????
  258. ????public?static?final?int?WIFI_CONNECTED?=?0x01;??
  259. ????public?static?final?int?WIFI_CONNECT_FAILED?=?0x02;??
  260. ????public?static?final?int?WIFI_CONNECTING?=?0x03;??
  261. ????/**?
  262. ?????*?判断wifi是否连接成功,不是network?
  263. ?????*??
  264. [email protected]?
  265. [email protected]?
  266. ?????*/??
  267. ????public?int?isWifiContected(Context?context)?{??
  268. ????????ConnectivityManager?connectivityManager?=?(ConnectivityManager)?context??
  269. ????????????????.getSystemService(Context.CONNECTIVITY_SERVICE);??
  270. ????????NetworkInfo?wifiNetworkInfo?=?connectivityManager??
  271. ????????????????.getNetworkInfo(ConnectivityManager.TYPE_WIFI);??
  272. ??????????
  273. ????????Log.v(TAG,?"isConnectedOrConnecting?=?"?+?wifiNetworkInfo.isConnectedOrConnecting());??
  274. ????????Log.d(TAG,?"wifiNetworkInfo.getDetailedState()?=?"?+?wifiNetworkInfo.getDetailedState());??
  275. ????????if?(wifiNetworkInfo.getDetailedState()?==?DetailedState.OBTAINING_IPADDR??
  276. ????????????????||?wifiNetworkInfo.getDetailedState()?==?DetailedState.CONNECTING)?{??
  277. ????????????return?WIFI_CONNECTING;??
  278. ????????}?else?if?(wifiNetworkInfo.getDetailedState()?==?DetailedState.CONNECTED)?{??
  279. ????????????return?WIFI_CONNECTED;??
  280. ????????}?else?{??
  281. ????????????Log.d(TAG,?"getDetailedState()?==?"?+?wifiNetworkInfo.getDetailedState());??
  282. ????????????return?WIFI_CONNECT_FAILED;??
  283. ????????}??
  284. ????}??
  285. ??????
  286. ????private?WifiConfiguration?IsExsits(String?SSID)?{??
  287. ????????List<WifiConfiguration>?existingConfigs?=?mWifiManager.getConfiguredNetworks();??
  288. ????????for?(WifiConfiguration?existingConfig?:?existingConfigs)?{??
  289. ????????????if?(existingConfig.SSID.equals("\""?+?SSID?+?"\"")?/*&&?existingConfig.preSharedKey.equals("\""?+?password?+?"\"")*/)?{??
  290. ????????????????return?existingConfig;??
  291. ????????????}??
  292. ????????}??
  293. ????????return?null;??
  294. ????}??
  295. ??????
  296. ??
  297. ??
  298. ????//?断开指定ID的网络??
  299. ????public?void?disconnectWifi(int?netId)?{??
  300. ????????mWifiManager.disableNetwork(netId);??
  301. ????????mWifiManager.disconnect();??
  302. ????}??
  303. ??????
  304. ????//?检查当前WIFI状态??
  305. ????public?int?checkState()?{??
  306. ????????return?mWifiManager.getWifiState();??
  307. ????}??
  308. ??
  309. ????//?锁定WifiLock??
  310. ????public?void?acquireWifiLock()?{??
  311. ????????mWifiLock.acquire();??
  312. ????}??
  313. ??
  314. ????//?解锁WifiLock??
  315. ????public?void?releaseWifiLock()?{??
  316. ????????//?判断时候锁定??
  317. ????????if?(mWifiLock.isHeld())?{??
  318. ????????????mWifiLock.acquire();??
  319. ????????}??
  320. ????}??
  321. ??
  322. ????//?创建一个WifiLock??
  323. ????public?void?creatWifiLock()?{??
  324. ????????mWifiLock?=?mWifiManager.createWifiLock("Test");??
  325. ????}??
  326. ??
  327. ????//?得到配置好的网络??
  328. ????public?List<WifiConfiguration>?getConfiguration()?{??
  329. ????????return?mWifiConfiguration;??
  330. ????}??
  331. ??
  332. ????//?指定配置好的网络进行连接??
  333. ????public?void?connectConfiguration(int?index)?{??
  334. ????????//?索引大于配置好的网络索引返回??
  335. ????????if?(index?>?mWifiConfiguration.size())?{??
  336. ????????????return;??
  337. ????????}??
  338. ????????//?连接配置好的指定ID的网络??
  339. ????????mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,??
  340. ????????????????true);??
  341. ????}??
  342. ??
  343. ????public?void?startScan()?{??
  344. ????????mWifiManager.startScan();??
  345. ????????mWifiList?=?mWifiManager.getScanResults();??
  346. ????????mWifiConfiguration?=?mWifiManager.getConfiguredNetworks();??
  347. ????}??
  348. ??
  349. ????//?得到网络列表??
  350. ????public?List<ScanResult>?getWifiList()?{??
  351. ????????return?mWifiList;??
  352. ????}??
  353. ??
  354. ????//?查看扫描结果??
  355. ????public?StringBuilder?lookUpScan()?{??
  356. ????????StringBuilder?stringBuilder?=?new?StringBuilder();??
  357. ????????for?(int?i?=?0;?i?<?mWifiList.size();?i++)?{??
  358. ????????????stringBuilder??
  359. ????????????????????.append("Index_"?+?new?Integer(i?+?1).toString()?+?":");??
  360. ????????????//?将ScanResult信息转换成一个字符串包??
  361. ????????????//?其中把包括:BSSID、SSID、capabilities、frequency、level??
  362. ????????????stringBuilder.append((mWifiList.get(i)).toString());??
  363. ????????????stringBuilder.append("/n");??
  364. ????????}??
  365. ????????return?stringBuilder;??
  366. ????}??
  367. ??
  368. ????//?得到MAC地址??
  369. ????public?String?getMacAddress()?{??
  370. ????????return?(mWifiInfo?==?null)???"NULL"?:?mWifiInfo.getMacAddress();??
  371. ????}??
  372. ??
  373. ????//?得到接入点的BSSID??
  374. ????public?String?getBSSID()?{??
  375. ????????return?(mWifiInfo?==?null)???"NULL"?:?mWifiInfo.getBSSID();??
  376. ????}??
  377. ??
  378. ????//?得到IP地址??
  379. ????public?int?getIPAddress()?{??
  380. ????????return?(mWifiInfo?==?null)???0?:?mWifiInfo.getIpAddress();??
  381. ????}??
  382. ??
  383. ????//?得到连接的ID??
  384. ????public?int?getNetworkId()?{??
  385. ????????return?(mWifiInfo?==?null)???0?:?mWifiInfo.getNetworkId();??
  386. ????}??
  387. ??
  388. ????//?得到WifiInfo的所有信息包??
  389. ????public?String?getWifiInfo()?{??
  390. ????????return?(mWifiInfo?==?null)???"NULL"?:?mWifiInfo.toString();??
  391. ????}??
  392. }??



?

WifiApAdmin.java

参考了 http://blog.csdn.net/cxlmax/article/details/7827102

?

[java]?view plaincopy
?
  1. package?com.widget.hotspot;??
  2. ??
  3. import?java.lang.reflect.InvocationTargetException;??
  4. import?java.lang.reflect.Method;??
  5. import?java.util.Timer;??
  6. import?java.util.TimerTask;??
  7. ??
  8. import?android.content.Context;??
  9. import?android.net.wifi.WifiConfiguration;??
  10. import?android.net.wifi.WifiManager;??
  11. import?android.util.Log;??
  12. ??
  13. /**?
  14. ?*?创建热点?
  15. ?*?
  16. ?*/??
  17. public?class?WifiApAdmin?{??
  18. ????public?static?final?String?TAG?=?"WifiApAdmin";??
  19. ??????
  20. ????public?static?void?closeWifiAp(Context?context)?{??
  21. ????????WifiManager?wifiManager?=?(WifiManager)?context.getSystemService(Context.WIFI_SERVICE);???
  22. ????????closeWifiAp(wifiManager);??
  23. ????}??
  24. ??????
  25. ????private?WifiManager?mWifiManager?=?null;??
  26. ??????
  27. ????private?Context?mContext?=?null;??
  28. ????public?WifiApAdmin(Context?context)?{??
  29. ????????mContext?=?context;??
  30. ??????????
  31. ????????mWifiManager?=?(WifiManager)?mContext.getSystemService(Context.WIFI_SERVICE);????
  32. ??????????
  33. ????????closeWifiAp(mWifiManager);??
  34. ????}??
  35. ??????
  36. ????private?String?mSSID?=?"";??
  37. ????private?String?mPasswd?=?"";??
  38. ????public?void?startWifiAp(String?ssid,?String?passwd)?{??
  39. ????????mSSID?=?ssid;??
  40. ????????mPasswd?=?passwd;??
  41. ??????????
  42. ????????if?(mWifiManager.isWifiEnabled())?{??
  43. ????????????mWifiManager.setWifiEnabled(false);??
  44. ????????}???
  45. ??????????
  46. ????????stratWifiAp();??
  47. ??????????
  48. ????????MyTimerCheck?timerCheck?=?new?MyTimerCheck()?{??
  49. ??????????????
  50. ????????????@Override??
  51. ????????????public?void?doTimerCheckWork()?{??
  52. ????????????????//?TODO?Auto-generated?method?stub??
  53. ??????????????????
  54. ????????????????if?(isWifiApEnabled(mWifiManager))?{??
  55. ????????????????????Log.v(TAG,?"Wifi?enabled?success!");??
  56. ????????????????????this.exit();??
  57. ????????????????}?else?{??
  58. ????????????????????Log.v(TAG,?"Wifi?enabled?failed!");??
  59. ????????????????}??
  60. ????????????}??
  61. ??
  62. ????????????@Override??
  63. ????????????public?void?doTimeOutWork()?{??
  64. ????????????????//?TODO?Auto-generated?method?stub??
  65. ????????????????this.exit();??
  66. ????????????}??
  67. ????????};??
  68. ????????timerCheck.start(15,?1000);??
  69. ??????????
  70. ????}??
  71. ??
  72. ????public?void?stratWifiAp()?{??
  73. ????????Method?method1?=?null;??
  74. ????????try?{??
  75. ????????????method1?=?mWifiManager.getClass().getMethod("setWifiApEnabled",??
  76. ????????????????????WifiConfiguration.class,?boolean.class);??
  77. ????????????WifiConfiguration?netConfig?=?new?WifiConfiguration();??
  78. ??
  79. ????????????netConfig.SSID?=?mSSID;??
  80. ????????????netConfig.preSharedKey?=?mPasswd;??
  81. ??
  82. ????????????netConfig.allowedAuthAlgorithms??
  83. ????????????????????.set(WifiConfiguration.AuthAlgorithm.OPEN);??
  84. ????????????netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);??
  85. ????????????netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);??
  86. ????????????netConfig.allowedKeyManagement??
  87. ????????????????????.set(WifiConfiguration.KeyMgmt.WPA_PSK);??
  88. ????????????netConfig.allowedPairwiseCiphers??
  89. ????????????????????.set(WifiConfiguration.PairwiseCipher.CCMP);??
  90. ????????????netConfig.allowedPairwiseCiphers??
  91. ????????????????????.set(WifiConfiguration.PairwiseCipher.TKIP);??
  92. ????????????netConfig.allowedGroupCiphers??
  93. ????????????????????.set(WifiConfiguration.GroupCipher.CCMP);??
  94. ????????????netConfig.allowedGroupCiphers??
  95. ????????????????????.set(WifiConfiguration.GroupCipher.TKIP);??
  96. ??
  97. ????????????method1.invoke(mWifiManager,?netConfig,?true);??
  98. ??
  99. ????????}?catch?(IllegalArgumentException?e)?{??
  100. ????????????//?TODO?Auto-generated?catch?block??
  101. ????????????e.printStackTrace();??
  102. ????????}?catch?(IllegalAccessException?e)?{??
  103. ????????????//?TODO?Auto-generated?catch?block??
  104. ????????????e.printStackTrace();??
  105. ????????}?catch?(InvocationTargetException?e)?{??
  106. ????????????//?TODO?Auto-generated?catch?block??
  107. ????????????e.printStackTrace();??
  108. ????????}?catch?(SecurityException?e)?{??
  109. ????????????//?TODO?Auto-generated?catch?block??
  110. ????????????e.printStackTrace();??
  111. ????????}?catch?(NoSuchMethodException?e)?{??
  112. ????????????//?TODO?Auto-generated?catch?block??
  113. ????????????e.printStackTrace();??
  114. ????????}??
  115. ????}??
  116. ??
  117. ????private?static?void?closeWifiAp(WifiManager?wifiManager)?{??
  118. ????????if?(isWifiApEnabled(wifiManager))?{??
  119. ????????????try?{??
  120. ????????????????Method?method?=?wifiManager.getClass().getMethod("getWifiApConfiguration");??
  121. ????????????????method.setAccessible(true);??
  122. ??
  123. ????????????????WifiConfiguration?config?=?(WifiConfiguration)?method.invoke(wifiManager);??
  124. ??
  125. ????????????????Method?method2?=?wifiManager.getClass().getMethod("setWifiApEnabled",?WifiConfiguration.class,?boolean.class);??
  126. ????????????????method2.invoke(wifiManager,?config,?false);??
  127. ????????????}?catch?(NoSuchMethodException?e)?{??
  128. ????????????????//?TODO?Auto-generated?catch?block??
  129. ????????????????e.printStackTrace();??
  130. ????????????}?catch?(IllegalArgumentException?e)?{??
  131. ????????????????//?TODO?Auto-generated?catch?block??
  132. ????????????????e.printStackTrace();??
  133. ????????????}?catch?(IllegalAccessException?e)?{??
  134. ????????????????//?TODO?Auto-generated?catch?block??
  135. ????????????????e.printStackTrace();??
  136. ????????????}?catch?(InvocationTargetException?e)?{??
  137. ????????????????//?TODO?Auto-generated?catch?block??
  138. ????????????????e.printStackTrace();??
  139. ????????????}??
  140. ????????}??
  141. ????}??
  142. ??
  143. ????private?static?boolean?isWifiApEnabled(WifiManager?wifiManager)?{??
  144. ????????try?{??
  145. ????????????Method?method?=?wifiManager.getClass().getMethod("isWifiApEnabled");??
  146. ????????????method.setAccessible(true);??
  147. ????????????return?(Boolean)?method.invoke(wifiManager);??
  148. ??
  149. ????????}?catch?(NoSuchMethodException?e)?{??
  150. ????????????//?TODO?Auto-generated?catch?block??
  151. ????????????e.printStackTrace();??
  152. ????????}?catch?(Exception?e)?{??
  153. ????????????e.printStackTrace();??
  154. ????????}??
  155. ??
  156. ????????return?false;??
  157. ????}??
  158. ??
  159. }??



?

MyTimeCheck.java

?

[java]?view plaincopy
?
  1. package?com.widget.hotspot;??
  2. ??
  3. ??
  4. public?abstract?class?MyTimerCheck?{??
  5. ????private?int?mCount?=?0;??
  6. ????private?int?mTimeOutCount?=?1;??
  7. ????private?int?mSleepTime?=?1000;?//?1s??
  8. ????private?boolean?mExitFlag?=?false;??
  9. ????private?Thread?mThread?=?null;??
  10. ??????
  11. ????/**?
  12. ?????*?Do?not?process?UI?work?in?this.?
  13. ?????*/??
  14. ????public?abstract?void?doTimerCheckWork();??
  15. ??????
  16. ????public?abstract?void?doTimeOutWork();??
  17. ??????
  18. ????public?MyTimerCheck()?{??
  19. ????????mThread?=?new?Thread(new?Runnable()?{??
  20. ??????????????
  21. ????????????@Override??
  22. ????????????public?void?run()?{??
  23. ????????????????//?TODO?Auto-generated?method?stub??
  24. ????????????????while?(!mExitFlag)?{??
  25. ????????????????????mCount++;??
  26. ????????????????????if?(mCount?<?mTimeOutCount)?{??
  27. ????????????????????????doTimerCheckWork();??
  28. ????????????????????????try?{??
  29. ????????????????????????????mThread.sleep(mSleepTime);??
  30. ????????????????????????}?catch?(InterruptedException?e)?{??
  31. ????????????????????????????//?TODO?Auto-generated?catch?block??
  32. ????????????????????????????e.printStackTrace();??
  33. ????????????????????????????exit();??
  34. ????????????????????????}??
  35. ????????????????????}?else?{??
  36. ????????????????????????doTimeOutWork();??
  37. ????????????????????}??
  38. ????????????????}??
  39. ????????????}??
  40. ????????});??
  41. ????}??
  42. ??????
  43. ????/**?
  44. ?????*?start?
  45. [email protected]??
  46. [email protected],?Every?check?sleep?time.?
  47. ?????*/??
  48. ????public?void?start(int?timeOutCount,?int?sleepTime)?{??
  49. ????????mTimeOutCount?=?timeOutCount;??
  50. ????????mSleepTime?=?sleepTime;??
  51. ??????????
  52. ????????mThread.start();??
  53. ????}??
  54. ??????
  55. ????public?void?exit()?{??
  56. ????????mExitFlag?=?true;??
  57. ????}??
  58. ??????
  59. }?
  相关解决方案