连接到热点

时间:2016-07-26 20:58:33

标签: android android-wifi wifimanager wificonfiguration hotspot

我正在做一个我必须通过热点连接2部手机的地方。

将有2个按钮(SEND和RECEIVE)。如果有人按下RECEIVE按钮,他们的热点将被激活。如果有人按下SEND按钮,他们应该连接到该热点。

我已经在这里做了很多研究,它帮助我构建了很多代码,但它仍然没有用。 addNetwork方法返回一个有效的id,enableNetwork返回true,但它仍然没有连接到热点网络。它会禁用其他的,但它不会连接到热点。

如果我通过设置,wifi等,我可以加入热点,因此热点已经很好了。

这是我的代码:

以下是按钮的监听器:

      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();
            }
        }
    });

}

这是我的wifi配置:(我试过了两个)

    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);
}

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。

首先,我必须在客户端的SSID中再添加2个引号。 然后我必须从WifiConfiguration中删除所有GroupCiphers,将状态更改为启用,我不需要放任何优先级。

            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();
                    }

                }
            }

谢谢你, 我希望这有助于将来的其他人