您好,登錄后才能下訂單哦!
Android WiFi熱點開發?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
創建熱點
1、根據加密類型、密碼、是否隱藏等參數來創建熱點
static WifiConfiguration createWifiConfig(String SSID, @WifiSecurityType int wifiCipherType, String password, boolean hiddenSSID) { WifiConfiguration wifiConfiguration = new WifiConfiguration(); wifiConfiguration.SSID = convertToQuotedString(SSID); wifiConfiguration.hiddenSSID=hiddenSSID;//是否隱藏熱點true=隱藏,如果隱藏需要其他設備手動添加網絡 switch (wifiCipherType) { case WifiSecurityType.SECURITY_NONE: wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); break; case WifiSecurityType.SECURITY_WEP: wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE); wifiConfiguration.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); wifiConfiguration.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED); if (!TextUtils.isEmpty(password)) { int length = password.length(); // WEP-40, WEP-104, and 256-bit WEP (WEP-232?) if ((length == 10 || length == 26 || length == 58) && password.matches("[0-9A-Fa-f]*")) { wifiConfiguration.wepKeys[0] = password; } else { wifiConfiguration.wepKeys[0] = '"' + password + '"'; } } break; case WifiSecurityType.SECURITY_WPA_PSK: wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_PSK); if (!TextUtils.isEmpty(password)) { if (password.matches("[0-9A-Fa-f]{64}")) { wifiConfiguration.preSharedKey = password; } else { wifiConfiguration.preSharedKey = '"' + password + '"'; } } break; case WifiSecurityType.SECURITY_WPA_EAP: wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_EAP); wifiConfiguration.allowedKeyManagement.set(KeyMgmt.IEEE8021X); wifiConfiguration.enterpriseConfig = new WifiEnterpriseConfig(); int eapMethod = 0; int phase2Method = 0; wifiConfiguration.enterpriseConfig.setEapMethod(eapMethod); wifiConfiguration.enterpriseConfig.setPhase2Method(phase2Method); if (!TextUtils.isEmpty(password)) { wifiConfiguration.enterpriseConfig.setPassword(password); } break; default: break; } return wifiConfiguration; }
然后調用WifiManager的setWifiApEnabled方法來設置wifiConfiguration,因為是隱藏的,需要通過反射:
try { Method method = mWifManager.getClass().getMethod( "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE); boolean enable = (Boolean) method.invoke(mWifManager, config, true); if (enable) { Log.d("WiFi", "熱點已開啟"); } else { Log.d("WiFi", "創建熱點失敗"); } } catch (Exception e) { e.printStackTrace(); }
關閉熱點
關閉熱點比較簡單,也是用上面的方法,第二個參數傳false就行了:
public void closeWifiAp() { try { Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); method.invoke(mWifiManager, null, false); } catch (Exception e) { e.printStackTrace(); } }
監聽熱點狀態
熱點的狀態可以通過廣播的方式來監聽:
public static final String WIFI_AP_STATE_CHANGED_ACTION = "android.net.wifi.WIFI_AP_STATE_CHANGED";
不過這個變量是隱藏的,只能直接通過值來注冊廣播:
IntentFilter filter = new IntentFilter(); filter.addAction("android.net.wifi.WIFI_AP_STATE_CHANGED");
然后在廣播中獲取state:
int state = intent.getIntExtra("wifi_state", 0);
wifi熱點有如下幾種狀態:
#WIFI_AP_STATE_DISABLED #WIFI_AP_STATE_DISABLING #WIFI_AP_STATE_ENABLED #WIFI_AP_STATE_ENABLING #WIFI_AP_STATE_FAILED
其他API:
獲取WiFI熱點當前狀態,返回值就是上面五種狀態:
public int getWifiApState()
判斷WiFi熱點是否打開:
public boolean isWifiApEnabled()
獲取當前wifi熱點的WifiConfiguration:
public WifiConfiguration getWifiApConfiguration()
看完上述內容,你們掌握Android WiFi熱點開發的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。