android studio 1.5.1, marshmallow no internet access detected won't automatically reconnect

一世执手 提交于 2020-01-10 05:53:05

问题


I'm having this problem on some 2013 Nexus 7's.

got some code to work (please see below).

package acme.wifi;
import android.content.*;
import android.net.wifi.*;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import java.math.*;
import java.net.*;
import java.nio.*;
import java.util.*;
public class MainActivity extends AppCompatActivity {
    void retry() {
        WifiManager wifiMan=(WifiManager)getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInf;
        int ipAddress;
        List<WifiConfiguration> wifiConfigurations=wifiMan.getConfiguredNetworks();
        System.out.println("wifi configurations (all): "+wifiConfigurations.size());
        for(WifiConfiguration wifiConfiguration : wifiConfigurations) {
            if(wifiConfiguration.toString().contains("192.168.0.")) {
                System.out.println("found our network: "+wifiConfiguration.toString());
                System.out.println("SSID: "+wifiConfiguration.SSID);
                System.out.println("wifi configuration status is disabled: "+(wifiConfiguration.status==WifiConfiguration.Status.DISABLED));
                int networkId=wifiConfiguration.networkId;
                boolean ok=wifiMan.disconnect();
                System.out.println("disconnect() returns: "+ok);
                System.out.println("enabling our network: "+networkId);
                // https://code.google.com/p/android-developer-preview/issues/detail?id=2218
                ok=wifiMan.enableNetwork(networkId,true);
                System.out.println("enableNetwork() returns: "+ok);
                System.out.println("trying to recommect to wifi.");
                ok=wifiMan.reconnect();
                System.out.println("reconnect returns: "+ok);
                if(ok) {
                    wifiMan=(WifiManager)getSystemService(Context.WIFI_SERVICE);
                    wifiInf=wifiMan.getConnectionInfo();
                    ipAddress=wifiInf.getIpAddress();
                    System.out.println("wifi ip adress: "+ipAddress);
                }
                break;
            }
        }
    }
    String getIpAddressFromWifiManager() {
        WifiManager wifiMan=(WifiManager)getSystemService(Context.WIFI_SERVICE);
        System.out.println("isWifiEnabled() returns: "+wifiMan.isWifiEnabled());
        WifiInfo wifiInf=wifiMan.getConnectionInfo();
        System.out.println("wifi info: "+wifiInf);
        int ipAddress=wifiInf.getIpAddress();
        System.out.println("wifi ip adress: "+ipAddress);
        if(ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN))
            ipAddress=Integer.reverseBytes(ipAddress);
        byte[] ipByteArray=BigInteger.valueOf(ipAddress).toByteArray();
        String ipAddressString=null;
        try {
            ipAddressString=InetAddress.getByAddress(ipByteArray).getHostAddress();
        } catch(UnknownHostException ex) {
            System.out.println("Unable to get host address.");
            if(true)
                retry();
        }
        return ipAddressString;
    }
    public class W extends Thread {
        W(int sleep) {
            this.sleep=sleep;
        }
        @Override public void run() {
            try {
                Thread.sleep(sleep);
            } catch(InterruptedException e) {
                System.out.println("1 caught: "+e);
            }
            String ipAddress=getIpAddressFromWifiManager();
            System.out.println("ip address: "+ipAddress);
            if(ipAddress==null)
                new W(5_000).start();
        }
        int sleep;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view,"Replace with your own action",Snackbar.LENGTH_LONG).setAction("Action",null).show();
            }
        });
        new W(0).start();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id=item.getItemId();
        //noinspection SimplifiableIfStatement
        if(id==R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

02-22 19:41:08.637 2715-2734/? I/System.out: isWifiEnabled() returns: true
02-22 19:41:08.638 2715-2734/? I/System.out: wifi info: SSID: , BSSID: 00:00:00:00:00:00, MAC: 02:00:00:00:00:00, Supplicant state: SCANNING, RSSI: -127, Link speed: -1Mbps, Frequency: -1MHz, Net ID: -1, Metered hint: false, score: 0
02-22 19:41:08.638 2715-2734/? I/System.out: wifi ip adress: 0
02-22 19:41:08.638 2715-2734/? I/System.out: Unable to get host address.
02-22 19:41:08.644 2715-2734/? I/System.out: wifi configurations (all): 1
02-22 19:41:08.646 2715-2734/? I/System.out: found our network: ID: 0 SSID: "tablets" PROVIDER-NAME: null BSSID: null FQDN: null PRIO: 2
02-22 19:41:08.646 2715-2734/? I/System.out:  numAssociation 74
02-22 19:41:08.646 2715-2734/? I/System.out:  numNoInternetAccessReports 11
02-22 19:41:08.646 2715-2734/? I/System.out:  KeyMgmt: NONE Protocols: WPA RSN
02-22 19:41:08.647 2715-2734/? I/System.out:  AuthAlgorithms: OPEN SHARED
02-22 19:41:08.647 2715-2734/? I/System.out:  PairwiseCiphers: TKIP CCMP
02-22 19:41:08.647 2715-2734/? I/System.out:  GroupCiphers: TKIP CCMP
02-22 19:41:08.647 2715-2734/? I/System.out:  PSK: 
02-22 19:41:08.647 2715-2734/? I/System.out: Enterprise config:
02-22 19:41:08.647 2715-2734/? I/System.out: password NULL
02-22 19:41:08.647 2715-2734/? I/System.out: engine 0
02-22 19:41:08.647 2715-2734/? I/System.out: client_cert NULL
02-22 19:41:08.647 2715-2734/? I/System.out: anonymous_identity NULL
02-22 19:41:08.647 2715-2734/? I/System.out: identity NULL
02-22 19:41:08.647 2715-2734/? I/System.out: domain_suffix_match NULL
02-22 19:41:08.647 2715-2734/? I/System.out: phase2 NULL
02-22 19:41:08.647 2715-2734/? I/System.out: altsubject_match NULL
02-22 19:41:08.647 2715-2734/? I/System.out: subject_match NULL
02-22 19:41:08.647 2715-2734/? I/System.out: ca_cert NULL
02-22 19:41:08.647 2715-2734/? I/System.out: phase1 NULL
02-22 19:41:08.647 2715-2734/? I/System.out: key_id NULL
02-22 19:41:08.647 2715-2734/? I/System.out: engine_id NULL
02-22 19:41:08.647 2715-2734/? I/System.out: eap NULL
02-22 19:41:08.647 2715-2734/? I/System.out: IP config:
02-22 19:41:08.647 2715-2734/? I/System.out: IP assignment: STATIC
02-22 19:41:08.647 2715-2734/? I/System.out: Static configuration: IP address 192.168.0.11/24 Gateway 192.168.0.1  DNS servers: [ 8.8.8.8 ] Domains
02-22 19:41:08.647 2715-2734/? I/System.out: Proxy settings: NONE
02-22 19:41:08.647 2715-2734/? I/System.out:  cuid=1000 cname=android.uid.system:1000 luid=1000 lname=android.uid.system:1000 lcuid=10044 userApproved=USER_APPROVED noInternetAccessExpected=false roamingFailureBlackListTimeMilli: 1000
02-22 19:41:08.647 2715-2734/? I/System.out: triggeredLow: 0 triggeredBad: 0 triggeredNotHigh: 0
02-22 19:41:08.647 2715-2734/? I/System.out: ticksLow: 0 ticksBad: 0 ticksNotHigh: 0
02-22 19:41:08.647 2715-2734/? I/System.out: triggeredJoin: 0
02-22 19:41:08.648 2715-2734/? I/System.out: autoJoinBailedDueToLowRssi: false
02-22 19:41:08.648 2715-2734/? I/System.out: autoJoinUseAggressiveJoinAttemptThreshold: 0
02-22 19:41:08.648 2715-2734/? I/System.out: SSID: "tablets"
02-22 19:41:08.648 2715-2734/? I/System.out: wifi configuration status is disabled: false
02-22 19:41:08.649 2715-2734/? I/System.out: disconnect() returns: true
02-22 19:41:08.649 2715-2734/? I/System.out: enabling our network: 0
02-22 19:41:08.738 2715-2734/? I/System.out: enableNetwork() returns: true
02-22 19:41:08.738 2715-2734/? I/System.out: trying to recommect to wifi.
02-22 19:41:08.739 2715-2734/? I/System.out: reconnect returns: true
02-22 19:41:08.740 2715-2734/? I/System.out: wifi ip adress: 0
02-22 19:41:08.740 2715-2734/? I/System.out: ip address: null
02-22 19:41:13.744 2715-2741/? I/System.out: isWifiEnabled() returns: true
02-22 19:41:13.747 2715-2741/? I/System.out: wifi info: SSID: tablets, BSSID: 60:e3:27:fb:8d:5a, MAC: 02:00:00:00:00:00, Supplicant state: COMPLETED, RSSI: -40, Link speed: 54Mbps, Frequency: 2462MHz, Net ID: 0, Metered hint: false, score: 60
02-22 19:41:13.747 2715-2741/? I/System.out: wifi ip adress: 184592576
02-22 19:41:13.748 2715-2741/? I/System.out: ip address: 192.168.0.11

回答1:


I was facing the same problem, but I have solved it.

Add these permissions:

  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.ACCESS_COARSE_LOCATION

and Keep Location Service ON.

Strange but true.




回答2:


I have same issue with 5.1.1 and Try to ping server of google.com like:

public Boolean isOnline() {
    try {
// Process  p1 = runtime.exec("/system/bin/ping -c 1 8.8.8.8");

        Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
        int returnVal = p1.waitFor();
        boolean reachable = (returnVal==0);
        return reachable;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}

this will give you correct response.




回答3:


I think need to add Network State Access permission in AndroidManifest.xml

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



回答4:


Add permission in the manifest file:

  1. android.permission.ACCESS_FINE_LOCATION
  2. android.permission.ACCESS_COARSE_LOCATION
  3. Turn on Location Service



回答5:


The new code in the question seems to work.

4/16 now the code has stopped working :(



来源:https://stackoverflow.com/questions/35282807/android-studio-1-5-1-marshmallow-no-internet-access-detected-wont-automaticall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!