连接到热点

Connecting to a Hotspot

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

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

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

我已经解决了这个问题。

首先,我必须在客户端的 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();
                    }

                }
            }

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