這篇文章主要介紹“Android WiFi熱點(diǎn)怎么開發(fā)”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“Android WiFi熱點(diǎn)怎么開發(fā)”文章能幫助大家解決問題。
創(chuàng)建熱點(diǎn)
1、根據(jù)加密類型、密碼、是否隱藏等參數(shù)來(lái)創(chuàng)建熱點(diǎn)
static WifiConfiguration createWifiConfig(String SSID, @WifiSecurityType int wifiCipherType, String password, boolean hiddenSSID) { WifiConfiguration wifiConfiguration = new WifiConfiguration(); wifiConfiguration.SSID = convertToQuotedString(SSID); wifiConfiguration.hiddenSSID=hiddenSSID;//是否隱藏?zé)狳c(diǎn)true=隱藏,如果隱藏需要其他設(shè)備手動(dòng)添加網(wǎng)絡(luò) 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; }
然后調(diào)用WifiManager的setWifiApEnabled方法來(lái)設(shè)置wifiConfiguration,因?yàn)槭请[藏的,需要通過(guò)反射:
try { Method method = mWifManager.getClass().getMethod( "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE); boolean enable = (Boolean) method.invoke(mWifManager, config, true); if (enable) { Log.d("WiFi", "熱點(diǎn)已開啟"); } else { Log.d("WiFi", "創(chuàng)建熱點(diǎn)失敗"); } } catch (Exception e) { e.printStackTrace(); }
關(guān)閉熱點(diǎn)
關(guān)閉熱點(diǎn)比較簡(jiǎn)單,也是用上面的方法,第二個(gè)參數(shù)傳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(); } }
監(jiān)聽熱點(diǎn)狀態(tài)
熱點(diǎn)的狀態(tài)可以通過(guò)廣播的方式來(lái)監(jiān)聽:
public static final String WIFI_AP_STATE_CHANGED_ACTION = "android.net.wifi.WIFI_AP_STATE_CHANGED";
不過(guò)這個(gè)變量是隱藏的,只能直接通過(guò)值來(lái)注冊(cè)廣播:
IntentFilter filter = new IntentFilter(); filter.addAction("android.net.wifi.WIFI_AP_STATE_CHANGED");
然后在廣播中獲取state:
int state = intent.getIntExtra("wifi_state", 0);
wifi熱點(diǎn)有如下幾種狀態(tài):
#WIFI_AP_STATE_DISABLED #WIFI_AP_STATE_DISABLING #WIFI_AP_STATE_ENABLED #WIFI_AP_STATE_ENABLING #WIFI_AP_STATE_FAILED
其他API:
獲取WiFI熱點(diǎn)當(dāng)前狀態(tài),返回值就是上面五種狀態(tài):
public int getWifiApState()
判斷WiFi熱點(diǎn)是否打開:
public boolean isWifiApEnabled()
獲取當(dāng)前wifi熱點(diǎn)的WifiConfiguration:
public WifiConfiguration getWifiApConfiguration()
關(guān)于“Android WiFi熱點(diǎn)怎么開發(fā)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
網(wǎng)站名稱:AndroidWiFi熱點(diǎn)怎么開發(fā)-創(chuàng)新互聯(lián)
URL地址:http://www.chinadenli.net/article4/dgsdoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、Google、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容