如何检查活动WIFI连接是否是android中的热点(不是移动热点)?

时间:2015-09-03 11:53:54

标签: java android url https connection

我想查看设备当前互联网连接的状态。

到目前为止,我曾经尝试将 HttpURLConnection connect()连接到网址http://www.google.com,然后通过响应代码处理结果。对于 200 ,它将是一个稳定的连接。如果URL重定向(例如,在这种情况下为热点登录页面),则响应将为 3xx

但是,这适用于运行Android 5.0.1 及更低版本的所有设备。从5.0.2开始,我对所有州都获得302:

  1. 已连接到热点但已登录(实际上应返回302)
  2. 已连接到热点并已登录
  3. 已连接到移动数据
  4. 我的想法是,它会重定向到 https 网址,从而返回错误的回复代码。

    如果我将网址切换为https://www.google.com,则最后两个状态工作正常,但如果设备已登录热点,则根本不会重定向(因此抛出TimeoutException。

    这是我的代码(请注意,它是AsyncTask的一部分,实际上是在片段中):

        protected Integer doInBackground(Void... voids) {
            Log.d(TAG, "checking internet...");
    
    
            ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    
            //if an active network or mobile data is available and is connected
            if (activeNetwork != null && activeNetwork.isConnected()) {
                try {
                    URL url = new URL(TEST_URL);
                    HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                    urlc.setRequestProperty("User_Agent", "test");
                    urlc.setRequestProperty("Connection", "close");
                    urlc.setConnectTimeout(TIMEOUT);
                    //tries to access a URL ("Google.com" in this case)
                    Log.d(TAG,"trying to access " + TEST_URL);
                    urlc.connect();
                    responseCode = urlc.getResponseCode();
                    Log.d(TAG, "Response Code: " + String.valueOf(responseCode));
                    if (responseCode >= INFORMATIONAL && responseCode < STABLE_CONNECTION) {
                        //1xx = informational
                        //TODO
    
                    } else if (responseCode >= STABLE_CONNECTION && responseCode < REDIRECTION) {
                        //2xx = stable
                        if (cm.isActiveNetworkMetered()) {
                            Log.d(TAG, "active connection is metered");
                            return MOBILE_DATA; //when data is metered
                        }
    
                        return INTERNET_AVAILABLE;
                    } else if (responseCode >= REDIRECTION && responseCode < C_ERROR) {
                        Log.d(TAG, "Active Network is a Hotspot (or similar)");
                        //3xx = redirecting / hotspot?
    
                        return HOTSPOT;
                    } else if (responseCode >= C_ERROR && responseCode < S_ERROR) {
                        //4xx = client error
                        Log.d(TAG, "client error");
                        return CLIENT_ERROR;
                    } else if (responseCode >= S_ERROR) {
                        //5xx = server error
                        Log.d(TAG, "server error");
                        return SERVER_ERROR;
                    }
    
    
                } catch (SocketTimeoutException e) {
                    Log.d(TAG, "Error checking internet connection (Timeout)");
                    return CONNECTION_TIMEOUT;
                } catch (IOException e) {
                    Log.d(TAG, "Error checking internet connection (IOException)", e);  //return false for errors
                    return NO_INTERNET;
                }
            }
    
            //return false, when there is neither an active network nor mobile data
            Log.d(TAG,"neither mobile data nor connected to wifi");
            return NO_INTERNET;
    
        }
    

    所以,我的问题是:是否有任何解决方法,我不会被重定向,当我不应该? 或者是否有更好的方法来检查,如果活动连接是公共热点(如Telekom Hotspot或类似)?

    提前致谢!

0 个答案:

没有答案