0

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

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

我已经在这里做了很多研究,它对我构建代码有很大帮助,但它仍然无法正常工作。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);
}
4

1 回答 1

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

                }
            }

谢谢,我希望这对以后的其他人有所帮助

于 2016-07-27T11:38:52.343 回答