当前位置:   article > 正文

Android Wifi和蓝牙操作(一)_wificonfig.disablereason不存在

wificonfig.disablereason不存在

/** 本文中代码仅供参考, 无法直接运行 **/

权限申明:

<!-- 访问网络权限 -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>    
  	<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>       
  	<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 
	<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
	<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission> 
	<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission> 
	<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>  
	
	<!-- after 6.0 for BT -->
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
	

 

1. 扫描WIFI热点

调用WifiManager.startScan()

  1. final int MSG_SCAN_START = 0x01;
  2. //final int MSG_SCAN_COMPLETE = 0x02;
  3. private Handler mHandler = new Handler() {
  4. public void handleMessage(Message msg) {
  5. if (MSG_SCAN_START == msg.what) {
  6. if(debugScan)ALog.d(TAG, "start scan wifi access point");
  7. mWifiManager.startScan();//启动扫描
  8. //we should start scan again to refresh wifi list state after every 10 sec
  9. mHandler.sendEmptyMessageDelayed(MSG_SCAN_START, 10 * 1000);
  10. }
  11. /* if (MSG_SCAN_COMPLETE == msg.what) {
  12. onWifiScanSuccess();
  13. }*/
  14. };
  15. };

 监听WIFI状态变化

  1. IntentFilter filter = new IntentFilter();
  2. //WIFI 开关
  3. filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
  4. //网络连接状态,
  5. filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
  6. //扫描结果
  7. filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
  8. wifiReceiver = new WifiChangedReceiver();
  9. registerReceiver(wifiReceiver, filter);

 扫描完成后,通过获取ScanResult来显示列表, 

  1. //ScanResult 中, 包含了SSID, BSSID, 加密码类型等信息.
  2. mWifiManager.getScanResults();

 要完整的状态还需要配合 WifiConfiguration一起:

  1. //如验证失败原因
  2. switch (mConfig.disableReason) {
  3. case WifiConfiguration.DISABLED_AUTH_FAILURE:
  4. summary.append(context.getString(R.string.wifi_disabled_password_failure));
  5. break;
  6. case WifiConfiguration.DISABLED_DHCP_FAILURE:
  7. case WifiConfiguration.DISABLED_DNS_FAILURE:
  8. summary.append(context.getString(R.string.wifi_disabled_network_failure));
  9. break;
  10. case WifiConfiguration.DISABLED_UNKNOWN_REASON:
  11. case WifiConfiguration.DISABLED_ASSOCIATION_REJECT:
  12. summary.append(context.getString(R.string.wifi_disabled_generic));
  13. break;

 

2.  WIFI开关

  1. //开关的代码比较简单:
  2. WifiManager.setWifiEnabled(boolean open)

 

状态监听:

  1. if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {// 这个监听wifi的打开与关闭,与wifi的连接无关
  2. int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
  3. Log.i(TAG, "wifiState" + wifiState);
  4. switch (wifiState) {
  5. case WifiManager.WIFI_STATE_DISABLED:
  6. break;
  7. case WifiManager.WIFI_STATE_DISABLING:
  8. break;
  9. case WifiManager.WIFI_STATE_ENABLED:{
  10. Log.i(TAG, "WIFI_STATE_ENABLED");
  11. mHandler.sendEmptyMessageDelayed(MSG_SCAN_START, 1000);
  12. break;
  13. }
  14. //
  15. }

 

3. WIFI热点连接

  1. /***
  2. * 配置要连接的WIFI热点信息
  3. * @param SSID
  4. * @param password
  5. * @param type 加密类型
  6. * @return
  7. */
  8. public WifiConfiguration createWifiInfo(String SSID, String password, int type) {
  9. Log.e(TAG, "SSID = " + SSID + "## Password = " + password + "## Type = " + type);
  10. WifiConfiguration config = new WifiConfiguration();
  11. config.allowedAuthAlgorithms.clear();
  12. config.allowedGroupCiphers.clear();
  13. config.allowedKeyManagement.clear();
  14. config.allowedPairwiseCiphers.clear();
  15. config.allowedProtocols.clear();
  16. config.SSID = "\"" + SSID + "\"";
  17. /*//增加热点时候 如果已经存在SSID 则将SSID先删除以防止重复SSID出现
  18. WifiConfiguration tempConfig = wifiAdmin.IsExsits(SSID);
  19. if (tempConfig != null) {
  20. settingWifimin.AdwifiManager.removeNetwork(tempConfig.networkId);
  21. } */
  22. // 分为三种情况:没有密码 用wep加密 用wpa加密
  23. if (type == SECURITY_NONE) { // WIFICIPHER_NOPASS
  24. config.wepKeys[0] = "";
  25. config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
  26. config.wepTxKeyIndex = 0;
  27. } else if (type == SECURITY_WEP) { // WIFICIPHER_WEP
  28. config.hiddenSSID = true;
  29. config.wepKeys[0] = "\"" + password + "\"";
  30. config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
  31. config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
  32. config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
  33. config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
  34. config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
  35. config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
  36. config.wepTxKeyIndex = 0;
  37. } else if (type == SECURITY_WPA) { // WIFICIPHER_WPA
  38. config.preSharedKey = "\"" + password + "\"";
  39. /*config.hiddenSSID = true;
  40. config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
  41. config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
  42. config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
  43. config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
  44. config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
  45. config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
  46. config.status = WifiConfiguration.Status.ENABLED; */
  47. config.hiddenSSID = true;
  48. config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
  49. config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
  50. config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
  51. config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
  52. config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
  53. config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
  54. config.allowedPairwiseCiphers
  55. .set(WifiConfiguration.PairwiseCipher.CCMP);
  56. config.status = WifiConfiguration.Status.ENABLED;
  57. }
  58. /*config.proxySettings = ProxySettings.NONE;
  59. config.ipAssignment = m_ipAssignment;
  60. config.linkProperties = new LinkProperties(m_linkProperties);*/
  61. return config;
  62. }

 连接:

  1. int networkId = WifiManager.addNetwork(WifiConfiguration config);
  2. //若已存在, 无需调用, 当然, 也可调用remove
  3. WifiManager.removeNetwork(int networkId);
  4. //最后
  5. WifiManager.enableNetwork(int NetworkId, boolean disableOther)

 

断开连接:

  1. //获取networkId
  2. public int getNetworkIdFromConfigByName(String name){
  3. int id = -1;
  4. List<WifiConfiguration> mWifiConfigList = WifiManager.getConfiguredNetworks();
  5. for(WifiConfiguration conf : mWifiConfigList){
  6. if(conf.SSID.equals(name)){
  7. return conf.networkId;
  8. }
  9. }
  10. return id;
  11. }
  12. //删除WIFI并断开:
  13. private boolean RemoveWifi(int netId){
  14. return mWifiManager.removeNetwork(netId);
  15. }

 

蓝牙部分待续.....

 

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/192442
推荐阅读
相关标签
  

闽ICP备14008679号