在android中以编程方式连接到热点

时间:2015-04-11 07:59:58

标签: android personal-hotspot

嗨,我知道这个问题需要很多时间,但非主题帮助我消除了我的问题 我想在设备中创建热点并从其他设备连接到它并通过wifi发送数据。

问题第一次我可以连接到我的热点并通过它传输数据,但是在热点禁用的时候,我再次创建它,直到我重新启动客户端电话才能连接到该网络。
这是连接到热点的代码

addWifiConfig(HotspotName, Hotspotpass, "WPA", "");

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
            if(i.SSID != null && i.SSID.equals("\"" + HotspotName + "\"")) {
                try { 
                    wifiManager.disconnect();

                    wifiManager.enableNetwork(i.networkId, true);
                    boolean res=wifiManager.reconnect(); 

                } 
                catch (Exception e) {
                    e.printStackTrace();
                } 

            }            
        }

addWifiConfig methode

 public static void addWifiConfig(String ssid,String password,String securityParam,String securityDetailParam) {

        if (ssid == null) {
            throw new IllegalArgumentException(
                    "Required parameters can not be NULL #"); 
        } 

        String wifiName = ssid;
        WifiConfiguration conf = new WifiConfiguration();
        if (Build.VERSION.SDK_INT >= 21) {
            conf.SSID = wifiName;
        } else { 
            conf.SSID = "\"" + wifiName + "\"";
        } 
        String security = securityParam;

        if (security.equalsIgnoreCase("WEP")) {
            conf.wepKeys[0] = password;
            conf.wepTxKeyIndex = 0;
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        } else if (security
                .equalsIgnoreCase("NONE")) {
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        } else if ("WPA" 
                .equalsIgnoreCase(security)
                || "WPA2" 
                        .equalsIgnoreCase(security)
                || "WPA/WPA2 PSK" 
                        .equalsIgnoreCase(security)) {
            // appropriate ciper is need to set according to security type used, 
            // ifcase of not added it will not be able to connect 
            conf.preSharedKey = "\""
                    + password + "\"";
            conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            conf.status = WifiConfiguration.Status.ENABLED;
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            conf.allowedPairwiseCiphers
                    .set(WifiConfiguration.PairwiseCipher.TKIP);
            conf.allowedPairwiseCiphers
                    .set(WifiConfiguration.PairwiseCipher.CCMP);
            conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        } 
        String securityDetails = securityDetailParam;

        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);


        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);



        int newNetworkId = wifiManager.addNetwork(conf);
            wifiManager.saveConfiguration();
            wifiManager.setWifiEnabled(true);
    } 

这是我在第二次尝试连接时遇到的错误

 java.net.ConnectException: failed to connect to /192.168.43.80 (port 8080): 

connect failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.connect(IoBridge.java:114)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.Socket.startupSocket(Socket.java:566)
at java.net.Socket.tryAllAddresses(Socket.java:128)
at java.net.Socket.<init>(Socket.java:178)
at java.net.Socket.<init>(Socket.java:150)
at drawerfragments.SettingsFragment$MyClientTask.doInBackground(SettingsFragment.java:477)
at drawerfragments.SettingsFragment$MyClientTask.doInBackground(SettingsFragment.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
at libcore.io.Posix.connect(Native Method)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
... 14 more

0 个答案:

没有答案