Android WiFi熱點(diǎn)開發(fā)的示例代碼
上次寫了Android連接匿名WiFi的內(nèi)容。WiFI開發(fā)對(duì)于應(yīng)用層開發(fā)是比較小眾的知識(shí)點(diǎn),不過既然用到了就在此記錄下。
創(chuàng)建熱點(diǎn)
1、根據(jù)加密類型、密碼、是否隱藏等參數(shù)來創(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方法來設(shè)置wifiConfiguration,因?yàn)槭请[藏的,需要通過反射:
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)可以通過廣播的方式來監(jiān)聽:
public static final String WIFI_AP_STATE_CHANGED_ACTION =
"android.net.wifi.WIFI_AP_STATE_CHANGED";
不過這個(gè)變量是隱藏的,只能直接通過值來注冊(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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android init.rc文件詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android init.rc文件詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android中系統(tǒng)默認(rèn)輸入法設(shè)置的方法(輸入法的顯示和隱藏)
這篇文章主要介紹了Android中系統(tǒng)默認(rèn)輸入法設(shè)置的方法(輸入法的顯示和隱藏)的相關(guān)資料,需要的朋友可以參考下2016-01-01
Flutter實(shí)現(xiàn)手勢(shì)識(shí)別功能詳解方法
在Flutter中使用GestureDetector可以來完成對(duì)手勢(shì)的識(shí)別,包括長(zhǎng)按、滑動(dòng)、雙擊等手勢(shì),這篇文章主要介紹了Flutter實(shí)現(xiàn)手勢(shì)識(shí)別功能2022-11-11
Android對(duì)話框AlertDialog與DatePickerDialog及TimePickerDialog使用詳解
這篇文章主要介紹了Android對(duì)話框中的提醒對(duì)話框AlertDialog、日期對(duì)話框DatePickerDialog、時(shí)間對(duì)話框TimePickerDialog使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09
ASM的tree?api對(duì)匿名線程的hook操作詳解
這篇文章主要為大家介紹了ASM的tree?api對(duì)匿名線程的hook操作詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android中使用TextView實(shí)現(xiàn)圖文混排的方法
向TextView或EditText中添加圖像比直接添加文本復(fù)雜一點(diǎn)點(diǎn),需要用到<img>標(biāo)簽。接下來通過本文給大家介紹Android中使用TextView實(shí)現(xiàn)圖文混排的方法,希望對(duì)大家有所幫助2016-02-02
Android實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03

