如何以编程方式连接到Wifi时诊断Android应用程序

时间:2017-09-28 00:11:57

标签: java android android-studio android-wifi

我有一个没有身份验证且已打开的接入点。它也没有连接到互联网。当我在手机设置中使用Wifi时,我可以将我的Android手机连接到接入点而没有任何问题。我只需点击接入点名称,手机就可以连接到它。但是它显示一条消息,表明没有Internet访问(这是正确的)。

但是,当我尝试以编程方式执行相同操作时,它不会连接到AP。我可以从时钟旁边的图标看到它连接到AP一瞬间,然后快速连接到最后一个已知良好的网络,这是我的家庭网络。

这是我的代码:

package com.my_appconn;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.net.wifi.WifiManager;
import android.net.wifi.ScanResult;
import android.content.BroadcastReceiver;

import java.util.ArrayList;
import java.util.List;

public class scandevices extends AppCompatActivity {

        WifiManager mainWifi;
        WifiReceiver receiverWifi;

        StringBuilder sb = new StringBuilder();

        private final Handler handler = new Handler();

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.scandevices);
            TextView txtStatus =(TextView) findViewById(R.id.txtStatus);
            txtStatus.setText("Scanning...");
            mainWifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

            receiverWifi = new WifiReceiver();
            registerReceiver(receiverWifi, new IntentFilter(
                    WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            if(mainWifi.isWifiEnabled()==false)
            {
                mainWifi.setWifiEnabled(true);
            }

            doInback();
        }

        public void doInback()
        {
            final TextView txtStatus =(TextView) findViewById(R.id.txtStatus);
            handler.postDelayed(new Runnable() {

                @Override
                public void run()
                {
                    // TODO Auto-generated method stub

                    receiverWifi = new WifiReceiver();
                    registerReceiver(receiverWifi, new IntentFilter(
                            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
                    mainWifi.startScan();
                    doInback();
                    txtStatus.setText("Scanning...");
                }
            }, 2000);

        }

        @Override
        protected void onPause()
        {
            unregisterReceiver(receiverWifi);
            super.onPause();
        }

        @Override
        protected void onResume()
        {
            registerReceiver(receiverWifi, new IntentFilter(
                    WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            super.onResume();
        }

        class WifiReceiver extends BroadcastReceiver
        {
            ListView resultView = (ListView) findViewById(R.id.lstWifiScanResult);
            ArrayAdapter<String> resultsAdaptr;
            TextView txtStatus =(TextView) findViewById(R.id.txtStatus);

            WifiScanResultsAdapter adapter;


            public void onReceive(Context c, Intent intent)
            {

                ArrayList<String> connections=new ArrayList<String>();
                ArrayList<Float> Signal_Strenth= new ArrayList<Float>();

                sb = new StringBuilder();
                List<ScanResult> wifiList;
                wifiList = mainWifi.getScanResults();

                for(int i = 0; i < wifiList.size(); i++)
                {
                    connections.add(wifiList.get(i).SSID);

                }
                txtStatus.setText("Scan done.");
                adapter = new WifiScanResultsAdapter(scandevices.this, connections);

                resultView.setAdapter(adapter);
                resultView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                   @Override
                   public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                        Integer netId;

                       WifiConfiguration wifiConfig= new WifiConfiguration();
                       wifiConfig.SSID = "\"" + adapter.getItem(i).toString() + "\"";

                       wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

                       netId= mainWifi.addNetwork(wifiConfig);
                       mainWifi.disconnect();
                       mainWifi.enableNetwork(netId, true);
                       mainWifi.reconnect();

                   }
               });

            }
        }
    }

我刚做了一些实验,我意识到每次更改接入点SSID时,我的应用都会连接到它而不会出现任何问题。如果我断开连接并再次尝试连接,我会遇到同样的问题。它短暂连接并重新连接到我的家庭网络。

0 个答案:

没有答案