5

Ich verwende den folgenden Code zum Erstellen der Wifi-Hotspot-Konfiguration. Ich kann die Hotspot-Konfiguration erstellen und aktivieren. aber ich habe Konfiguration für WPA-PSK gegeben, aber es hat immer als OPEN-Netzwerk genommen.Wie erstellt man benutzerdefinierte WPA Hotspot mit SSID und Passwort in Android?

public boolean setHotSpot(String SSID,String passWord){ 
Method[] mMethods = mWifiManager.getClass().getDeclaredMethods(); 

    for(Method mMethod: mMethods){ 

     if(mMethod.getName().equals("setWifiApEnabled")) { 
      WifiConfiguration netConfig = new WifiConfiguration(); 
      netConfig.SSID = SSID ; 
       netConfig.preSharedKey = passWord; 
       netConfig.hiddenSSID = true; 
       netConfig.status = WifiConfiguration.Status.ENABLED; 
       netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); 
       netConfig.allowedKeyManagement.set(4); 
       netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
       netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); 
       netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
       netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
       netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
       netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
       netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
       netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 

      try {    
       mMethod.invoke(mWifiManager, netConfig,true); 
       mWifiManager.saveConfiguration(); 
       return true; 

      } catch (Exception e) { 
       e.printStackTrace(); 
       CommonUtil.log(TAG,"Exception : "+e.getLocalizedMessage()); 
      } 
     } 
    } 
return false; 

}

Nach Durchlauf dieser App der Hotspot aktiviert wurde. Bitte überprüfen Sie das Bild unten.

enter image description here

Wie die WPA_PSK wifi Konfiguration in android app gesetzt?

Wie unten beantwortet ich habe den Code unten geändert.

public boolean setHotSpot(String SSID, String passWord) { 
    boolean apstatus; 
    WifiConfiguration netConfig = new WifiConfiguration(); 
    if (passWord == "") { 
     netConfig.SSID = SSID; 
     netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 
     netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
     netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 
     netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
    } else { 
     netConfig.SSID = SSID; 
     netConfig.preSharedKey = passWord; 
       /*netConfig.hiddenSSID = true; 
       netConfig.status = WifiConfiguration.Status.ENABLED; 
       netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); 
       netConfig.allowedKeyManagement.set(4); 
       netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
       netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); 
       netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
       netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
       netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
       netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
       netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
       netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);*/ 
     netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); 
     netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
     netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 
     netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
    } 

    try { 
     Method setWifiApMethod = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); 
     //setWifiApMethod.invoke(mWifiManager, previousConfigurations, false); 
     //Thread.sleep(2000); 
     apstatus = (Boolean) setWifiApMethod.invoke(mWifiManager, netConfig, true); 
    } catch (Exception e) { 
     CommonUtil.log(TAG, e.getMessage()); 
     return false; 
    } 
    return apstatus; 
} 

Antwort

0
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 

oben OPEN ersetzen, um GEMEINSAM

netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); 
+0

seine nicht funktioniert. es zeigt die gleiche Ausgabe wie oben erwähnt. Haben Sie es überprüft mit Android 6 – Yuvaraja

0

sollution

/** 
* Uppsala University 
* 
* Project CS course, Fall 2012 
* 
* Projekt DV/Project CS, is a course in which the students develop software for 
* distributed systems. The aim of the course is to give insights into how a big 
* project is run (from planning to realization), how to construct a complex 
* distributed system and to give hands-on experience on modern construction 
* principles and programming methods. 
* 
* All rights reserved. 
* 
* Copyright (C) 2012 LISA team 
*/ 
package project.cs.lisa.networksettings; 

import java.util.List; 

import android.content.Context; 
import android.net.wifi.ScanResult; 
import android.net.wifi.WifiConfiguration; 
import android.net.wifi.WifiInfo; 
import android.net.wifi.WifiManager; 
import android.util.Log; 

public class WifiHandler { 
    //constants 
    public static final int WEP = 1; 
    public static final int WAP = 2; 
    public static final int OPEN_NETWORK = 3; 

    public static final String TAG = "LISA_Network"; 

    /** dfsdfsdfsdf. */ 
    WifiConfiguration wifiConf;    /* WifiConfiguration object */ 

    /** dfsdfsdfsdf. */ 
    WifiManager wifiMgr;       /* WifiManager object */ 

    /** dfsdfsdfsdf. */ 
    WifiInfo wifiInfo;        /* WifiInfo object */ 

    /** dfsdfsdfsdf. */ 
    List<ScanResult> wifiScan;    /* List of ScanResult objects */ 

    /** 
    * Constructor initializes WifiManager and WifiInfo. 
    * @param context 
    */ 
    public WifiHandler(Context context) { 
     wifiMgr = getWifiManager(context);  // gets wifiMgr in the current context 
     wifiInfo = getWifiInfo(context);   // gets wifiInfo in the current context 
     wifiConf = getWifiConf(context);   // gets wifiConf in the current context 
     wifiScan = getWifiInRange();     // gets wifiScan in the current context 
    } 

    /** 
    * Function checkWifiEnabled checks if the WiFi connection 
    * is enabled on the device. 
    * @param wifiMgr 
    * @return true if the WiFi connection is enabled, 
    *    false if the WiFi connection is disabled 
    */ 
    public boolean checkWifiEnabled() { 
     // checks if WiFi is enabled 
     return (wifiMgr != null && wifiMgr.isWifiEnabled()); 
    } 

    /** 
    * Function enableWifi enables WiFi connection on the device. 
    * @param wifiMgr 
    * @return true if the attempt to enable WiFi succeeded, 
    *    false if the attempt to enable WiFi failed. 
    */ 
    public boolean enableWifi() { 
     // enables WiFi connection 
     return wifiMgr.setWifiEnabled(true); 
    } 

    /** 
    * Function disableWifi disables WiFi connection on the device. 
    * @param wifiMgr 
    * @return true if WiFi connection was disabled, 
    *    false if attempt to disable WiFi failed. 
    */ 
    public boolean disableWifi() { 
     // disables WiFi connection 
     return wifiMgr.setWifiEnabled(false); 
    } 

    /** 
    * Function getWifiManager gets the WiFiManager object from the device. 
    * @param context 
    * @return WifiManager object. Also sets the class variable 
    *    wifiMgr as the WifiManager object returned. 
    */ 
    public WifiManager getWifiManager(Context context) { 
     WifiManager wifiMgr = null; 

     // gets WifiManager obj from the system 
     wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 

     if (wifiMgr == null) { 
      Log.d("TAG", "WIFI_SERVICE is the wrong service name."); 
     } 

     return wifiMgr; 
    } 

    /** 
    * Function getWifiInfo gets the current WiFi connection information in a 
    * WifiInfo object from the device. 
    * @param context 
    * @return wifiInfo created object or 
    *    null  if wifi is not enabled. 
    */ 
    public WifiInfo getWifiInfo(Context context) { 
     WifiInfo wifiInfo = null; 

     // gets WiFi network info of the current connection 
     if (checkWifiEnabled()) { 
      wifiInfo = (WifiInfo) wifiMgr.getConnectionInfo(); 
     } 

     if (wifiInfo == null) { 
      Log.d("TAG", "WifiInfo object is empty."); 
     } 

     return wifiInfo; 
    } 

    /** 
    * Function that returns a WifiConfiguration object from the WifiInfo 
    * object from the class. If wifiInfo exists, then we are able to retrieve 
    * information from the current connection 
    * @param context 
    * @return WifiConfiguration object created. 
    */ 
    public WifiConfiguration getWifiConf(Context context) { 
     WifiConfiguration wifiConfiguration = new WifiConfiguration(); 

     if (wifiInfo == null) { 
      Log.d("TAG", "WifiInfo object is empty"); 
      return null; 
     } 

     wifiConfiguration.SSID = wifiInfo.getSSID(); 
     wifiConfiguration.networkId = wifiInfo.getNetworkId(); 

     return wifiConfiguration; 
    } 

    /** 
    * Creates a new WifiConfiguration object for wifiConf. 
    */ 
    public void clearWifiConfig() { 
     wifiConf = new WifiConfiguration(); 
    } 

    /** 
    * Function getWifiInRange returns all the WiFi networks that are 
    * accessible through the access point (device AP) found during the 
    * last scan. 
    * @param wifi 
    * @return List of ScanResult containing information on all WiFi networks 
    *    discovered in the range. 
    */ 
    public List<ScanResult> getWifiInRange() { 
     // gets ~last~ list of WiFi networks accessible through the access point. 
     return (wifiScan = (List<ScanResult>) wifiMgr.getScanResults()); 
    } 

    /** 
    * Function that scans for wifi networks available in the devices range. 
    * @return true if scan started 
    *    false if scan could not be started 
    */ 
    public boolean scanWifiInRange() { 
     if (!checkWifiEnabled()) { 
      return false; 
     } 

     if (!wifiMgr.startScan()) { 
      Log.d("TAG", "Failed to scan wifi's in range."); 
      return false; 
     } 

     return true; 
    } 

    /** 
    * Function to disconnect from the currently connected WiFi AP. 
    * @return true if disconnection succeeded 
    *    false if disconnection failed 
    */ 
    public boolean disconnectFromWifi() { 
     return (wifiMgr.disconnect()); 
    } 

    /** 
    * Function to connect to a selected network 
    * @param networkSSID   network SSID name 
    * @param networkPassword  network password 
    * @param networkId   network ID from WifiManager 
    * @param SecurityProtocol network security protocol 
    * @return true if connection to selected network succeeded 
    *    false if connection to selected network failed 
    */ 
    public boolean connectToSelectedNetwork(String networkSSID, String networkPassword) { 
     int networkId; 
     int SecurityProtocol = WEP; 

     // Clear wifi configuration variable 
     clearWifiConfig(); 

     // Sets network SSID name on wifiConf 
     wifiConf.SSID = "\"" + networkSSID + "\""; 
     Log.d(TAG, "SSID Received: " + wifiConf.SSID); 
     switch(SecurityProtocol) { 
      // WEP "security". 
      case WEP: 
       wifiConf.wepKeys[0] = "\"" + networkPassword + "\""; 
       wifiConf.wepTxKeyIndex = 0; 
       wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
       wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 
       break; 

       // WAP security. We have to set preSharedKey. 
      case WAP: 
       wifiConf.preSharedKey = "\""+ networkPassword +"\""; 
       break; 

       // Network without security. 
      case OPEN_NETWORK: 
       wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
       break; 
     } 

     // Add WiFi configuration to list of recognizable networks 
     if ((networkId = wifiMgr.addNetwork(wifiConf)) == -1) { 
      Log.d("TAG", "Failed to add network configuration!"); 
      return false; 
     } 

     // Disconnect from current WiFi connection 
     if (!disconnectFromWifi()) { 
      Log.d("TAG", "Failed to disconnect from network!"); 
      return false; 
     } 

     // Enable network to be connected 
     if (!wifiMgr.enableNetwork(networkId, true)) { 
      Log.d("TAG", "Failed to enable network!"); 
      return false; 
     } 

     // Connect to network 
     if (!wifiMgr.reconnect()) { 
      Log.d("TAG", "Failed to connect!"); 
      return false; 
     } 

     return true; 
    } 
} 
+0

@ Yuvaraja hey dude versuchen Sie diese Klasse und votes up mich plzzz – Sendy

+0

Der Code ist für den Anschluss von anderen Wifi-Netzwerk per Programm. Mein Problem ist das Erstellen von WLAN-Hotspot-Konfigurationsformular App. – Yuvaraja

0

Ich habe dies in meiner app getan. Bitte überprüfen Sie meinen Code unten.

Fügen Sie zunächst die untenstehenden Berechtigungen zu Ihrem Manifest hinzu.

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 

unter MyHotspotManager erstellen Dann .JAVA Klasse

public class MyHotspotManager { 
    private WifiConfiguration wifiCon; 
    private HotspotDetails hotspotDetails; 
    private WifiManager mWifiManager; 

    public MyHotspotManager(WifiManager wifiManager){ 
     this.mWifiManager = wifiManager; 
    } 

    public boolean setHotspot(HotspotDetails hotspotDetails) { 
     this.hotspotDetails = hotspotDetails; 
     boolean apstatus; 
     wifiCon = new WifiConfiguration(); 
     wifiCon.SSID = hotspotDetails.getSsid(); 
     wifiCon.preSharedKey = hotspotDetails.getPassword(); 
     wifiCon.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); 
     wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
     wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 
     wifiCon.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
     try { 
      Method setWifiApMethod = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); 
      //setWifiApMethod.invoke(mWifiManager, previousConfigurations, false); 
      //Thread.sleep(2000); 
      apstatus = (Boolean) setWifiApMethod.invoke(mWifiManager, wifiCon, true); 
     } catch (Exception e) { 
      Log.e(this.getClass().toString(), "", e); 
      return false; 
     } 

     return apstatus; 
    } 

} 

Danach können Sie die setHotspot Methode überall in Ihnen Aktivität wie unten aufrufen können. Hier musste ich überprüfen, ob die App die ACTION_MANAGE_WRITE_SETTINGS-Berechtigung hat.

public void OnButtonClick(View view) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (!Settings.System.canWrite(getApplicationContext())) { 
       Toast.makeText(this, "Please try again after giving access to Change system settings", Toast.LENGTH_SHORT).show(); 
       Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName())); 
       startActivityForResult(intent, 200); 

      }else{ 
       createHotspot(); 
      } 
     } 

    } 

    private void createHotspot(){ 
     HotspotDetails hotspotDetails = new HotspotDetails(); 
     hotspotDetails.setSsid("TestStackOverFlow"); 
     hotspotDetails.setPassword("654321"); 
     new MyHotspotManager((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE)) 
       .setHotspot(hotspotDetails); 
    } 

Aber wenn Sie möchten, können Sie, indem Sie Ihre targetSdkVersion bis 21 in build.gradle Berechtigungsanfrage Teil wegzulassen. Jetzt können Sie es wie unter

nennen
public void OnButtonClick(View view) { 
//  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
//   if (!Settings.System.canWrite(getApplicationContext())) { 
//    Toast.makeText(this, "Please try again after giving access to Change system settings", Toast.LENGTH_SHORT).show(); 
//    Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName())); 
//    startActivityForResult(intent, 200); 
// 
//   }else{ 
//   } 
//  } 

     createHotspot(); 
    } 

    private void createHotspot(){ 
     HotspotDetails hotspotDetails = new HotspotDetails(); 
     hotspotDetails.setSsid("TestStackOverFlow"); 
     hotspotDetails.setPassword("654321"); 
     new MyHotspotManager((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE)) 
       .setHotspot(hotspotDetails); 
    } 

Das ist mein HotspotDetails.java Klasse

public class HotspotDetails { 
    private String ssid; 
    private String password; 

    public String getSsid() { 
     return ssid; 
    } 

    public void setSsid(String ssid) { 
     this.ssid = ssid; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    @Override 
    public String toString() { 
     return ssid + ":" + password; 
    } 
} 
+0

Danke für die Wiederholung, Das gleiche Zeug habe ich bereits implementiert. Es erstellt immer das offene Netzwerk, die WPA-Konfiguration und das Passwort sind nicht gesetzt. Ich habe deinen Code auch versucht. Kein Glück. – Yuvaraja

+0

@Yuvaraja Es funktioniert bereits für mich. Dies ist der Code, den ich in meinem Projekt verwende. Es funktioniert auch auf Android M. Können Sie mir die aktualisierte Hotspot-Erstellungsmethode anzeigen? – TdSoft

+0

Überprüfen Sie den aktualisierten Code oben. Ich habe die Logik entsprechend Ihrer Eingabe geändert. aber immer noch kein Glück. – Yuvaraja

Verwandte Themen