AndroidWiFi熱點開發(fā)

Android WiFi熱點開發(fā)?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)新互聯(lián)主營團(tuán)風(fēng)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,APP應(yīng)用開發(fā),團(tuán)風(fēng)h5小程序開發(fā)搭建,團(tuán)風(fēng)網(wǎng)站營銷推廣歡迎團(tuán)風(fēng)等地區(qū)企業(yè)咨詢

創(chuàng)建熱點

1、根據(jù)加密類型、密碼、是否隱藏等參數(shù)來創(chuàng)建熱點

 static WifiConfiguration createWifiConfig(String SSID, @WifiSecurityType int wifiCipherType, String password, boolean hiddenSSID) {

    WifiConfiguration wifiConfiguration = new WifiConfiguration();
    wifiConfiguration.SSID = convertToQuotedString(SSID);
    wifiConfiguration.hiddenSSID=hiddenSSID;//是否隱藏?zé)狳ctrue=隱藏,如果隱藏需要其他設(shè)備手動添加網(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,因為是隱藏的,需要通過反射:

 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", "創(chuàng)建熱點失敗");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

關(guān)閉熱點

關(guān)閉熱點比較簡單,也是用上面的方法,第二個參數(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)聽熱點狀態(tài)

熱點的狀態(tài)可以通過廣播的方式來監(jiān)聽:

 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熱點有如下幾種狀態(tài):

#WIFI_AP_STATE_DISABLED  
#WIFI_AP_STATE_DISABLING
#WIFI_AP_STATE_ENABLED
#WIFI_AP_STATE_ENABLING
#WIFI_AP_STATE_FAILED

其他API:

獲取WiFI熱點當(dāng)前狀態(tài),返回值就是上面五種狀態(tài):

public int getWifiApState()

判斷WiFi熱點是否打開:

public boolean isWifiApEnabled()

獲取當(dāng)前wifi熱點的WifiConfiguration:

public WifiConfiguration getWifiApConfiguration()

看完上述內(nèi)容,你們掌握Android WiFi熱點開發(fā)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站標(biāo)題:AndroidWiFi熱點開發(fā)
分享URL:http://muchs.cn/article20/ihjdco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、虛擬主機、做網(wǎng)站域名注冊、外貿(mào)建站、服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)