以编程方式连接到程序的热点

时间:2015-11-28 06:57:40

标签: java android-networking

在我的课程中有两种能力

  1. 创建热点
  2. 连接到此热点
  3. 我在两个不同的设备上安装此程序。三星gallaxyS2和HTC oneM8。创建热点没有问题。如果我在三星gallaxyS2上创建热点,我可以轻松连接到HTC oneM8,但如果我在HTC oneM8上创建热点并尝试连接三星gallaxyM8,则addNetwork函数返回-1。 这是我的连接代码:

    private void JoinToNetWork(){
        ScanResult AP= scanAP();
        if (AP!=null){
            try{
                txt.setText("Hotspot named ''" + AP.SSID +"'' is found !  \n");
                wConfig=new WifiConfiguration();
                wConfig.SSID=AP.SSID;
                wConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                wConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                wConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                wConfig.allowedAuthAlgorithms.clear();
                wConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                wConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                wConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                wConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
                wConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                wConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                int NetId=wfMgr.addNetwork(wConfig);
                try{
                    txt.append("Disconnect : " + wfMgr.disconnect() +"\n");
                    txt.append("Enabling network ... " + wfMgr.enableNetwork(NetId, true) + "\n");
                    txt.append("Reconnect to " + AP.BSSID + "/" + AP.SSID + ".... " + wfMgr.reconnect() + "\n");
                    wfMgr.setWifiEnabled(true);
                    txt.append("connected");
                }catch(Exception e){
                    txt.setText(e.toString());
                }
            }catch(Exception e){
                Toast.makeText(this.getBaseContext(), e.getMessage()+"..."+e.toString(),Toast.LENGTH_LONG).show();
            }
        }else 
            Toast.makeText(this.getBaseContext(), "There is no BluffGame AccessPoint",Toast.LENGTH_LONG).show();
    }
    

0 个答案:

没有答案