2016-07-26 22 views
0

Ich mache eine wo ich 2 Telefone über Hotspot verbinden muss.Verbinden mit einem Hotspot

Es wird 2 Tasten geben (SEND und RECEIVE). Wenn eine Person die RECEIVE-Taste drückt, wird ihr Hotspot aktiviert. Wenn eine Person die SEND-Taste drückt, sollten sie sich mit diesem Hotspot verbinden.

Ich habe hier schon viel geforscht, und es hat mir sehr geholfen, den Code zu erstellen, aber es funktioniert immer noch nicht. Die Methode addNetwork gibt eine gültige ID zurück, das enableNetwork gibt true zurück, stellt jedoch weiterhin keine Verbindung zum Hotspot-Netzwerk her. Es deaktiviert die anderen, aber es verbindet sich nicht mit dem Hotspot.

Der Hotspot gut, da ich es beitreten können erstellt, wenn ich die Einstellungen, Wi-Fi zu gehen, usw.

ist hier mein Code:

Hier sind die Zuhörer für die Tasten:

 protected void setUpButtonListeners(){ 
     Log.i("SETUP BUTTON LISTENERS", "SETTING UP THE BUTTON LISTENERS"); 

    wificonfiguration=new WifiConfiguration(); 
    setUpTheWifiConfiguration(wificonfiguration); 
    wifimanager = (WifiManager) HighwayActivity.this.getSystemService(HighwayActivity.this.WIFI_SERVICE); 
    wifimanager.setWifiEnabled(true); 

    Button sendButton= (Button) findViewById(R.id.send); 
    Button receiveButton = (Button) findViewById(R.id.receive); 
    sendButton.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View arg0) 
     { 
       netID = wifimanager.addNetwork(wificonfiguration); 
       Log.e("NET ID ::: ", "O net id is : "+ netID); 

       boolean asad=wifimanager.disconnect(); 
       Log.e("wifi.manager disconnect"," bool is "+asad); 
       boolean aux = wifimanager.enableNetwork(netID, true); 
       Log.e("enableNetwork", "bool aux : " + aux); 
       boolean reconnect=wifimanager.reconnect(); 
       Log.e("Recconect ::: ", " recnect is : "+ reconnect); 

      wifireceiver=new WifiReceiver(); 
      registerReceiver(wifireceiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION)); 

     } 
    }); 

    receiveButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) 
     { 

      try 
      { 
       wifimanager.setWifiEnabled(false); 
       //USING REFLECTION TO GET METHOD "SetWifiAPEnabled" 
       Method method=wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); 
       method.invoke(wifimanager, wificonfiguration, true); 

       Toast.makeText(HighwayActivity.this, "Craeted a hotspot with the SSID : " + wificonfiguration.SSID, Toast.LENGTH_SHORT).show(); 

      } 
      catch (NoSuchMethodException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      catch (IllegalArgumentException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      catch (IllegalAccessException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      catch (InvocationTargetException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 

} 
Hier

ist mein wiFi-Konfiguration: (ich habe versucht, beide)

protected void setUpTheWifiConfiguration(WifiConfiguration wificonfiguration){ 
    wificonfiguration.SSID = "\"" + mySSID + "\""; 

    wificonfiguration.preSharedKey = "\"" + "pwteste123A"+ "\""; 
    /* 
    wificonfiguration.status = WifiConfiguration.Status.ENABLED; 
    wificonfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
    wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
    wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 
    // wificonfiguration.hiddenSSID = true; 
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); 
    wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
    wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
    wificonfiguration.priority = 40; 
    */ 
    // No security 
    wificonfiguration.status = WifiConfiguration.Status.DISABLED; 
    wificonfiguration.priority = 40; 

       wificonfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
    wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
    wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 
    wificonfiguration.allowedAuthAlgorithms.clear(); 
    wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
    wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); 
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
    wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
} 

Antwort

0

i ALR Eady hat das Problem gelöst.

Zuerst musste ich 2 weitere Anführungszeichen in der SSID auf der Klientenseite setzen. Dann musste ich alle GroupCipher aus der Wifi Konfiguration entfernen, ändere den Status auf aktiviert und ich musste keine Priorität setzen.

  WifiConfiguration conf = new WifiConfiguration(); 
      conf.SSID = "\"" + "TinyBox" + "\""; 

      conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); 

      conf.preSharedKey="\"password\""; 
      conf.hiddenSSID = true; 
      conf.status = WifiConfiguration.Status.ENABLED; 

      conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
      conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
      conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 

      int netid=wifi.addNetwork(conf); 

      List<WifiConfiguration> list = wifi.getConfiguredNetworks(); 
      for(WifiConfiguration i : list) { 

       if(i.SSID != null && i.SSID.equals("\"" + "TinyBox" + "\"")) { 
        Log.d("cONFIG nETOWKRS", "Found List of COnfigured Networks Tinybox"); 
        try { 
         boolean b=wifi.disconnect(); 

         boolean enab=wifi.enableNetwork(i.networkId, true); 

         wifi.reconnect(); 
         break; 
        } 
        catch (Exception e) { 
         e.printStackTrace(); 
        } 

       } 
      } 

Danke, Ich hoffe, die anderen Personen in der Zukunft

+0

Dank hilft die Antwort für die Buchung. Könnten Sie bitte auch den Code teilen, wie Sie einen Hotspot auf einem anderen Gerät erstellen? Vielen Dank. –