Android M中程序化连接WiFi自动断开问题排查求助
Hey there, this is a super common pain point with Android 6.0 (API 23) and above—even with the right permissions granted, there are a few key code oversights that lead to that 10-20 second disconnect. Let’s walk through the most likely issues and fixes based on your scenario:
1. You’re not handling "no-internet" WiFi networks correctly
Starting with Android 6.0, the system automatically checks if a WiFi network has internet access. If your network is a local-only setup (like a device hotspot or offline LAN), Android will mark it as "invalid" and drop the connection after that short window.
What to check in your code:
If your WiFi doesn’t need internet, make sure you’re configuring it to skip this validation. Add these lines to your WifiConfiguration:
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); // For Android 6.0+, explicitly mark the network as usable without internet if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { wifiConfig.setNetworkSelectionStatus(WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_ENABLED); }
Alternatively, use the newer ConnectivityManager API to request a network without internet capabilities—this is more reliable for Android 6+:
NetworkRequest networkRequest = new NetworkRequest.Builder() .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) .build(); ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); connManager.requestNetwork(networkRequest, new NetworkCallback() { @Override public void onAvailable(Network network) { // Bind your app to this network to keep it active connManager.bindProcessToNetwork(network); } });
2. Old WiFi API usage is causing instability
If you’re still using the legacy WifiManager.addNetwork() + enableNetwork() flow, it can be flaky on Android 6+. Even if it works on older devices, there are easy steps you might be missing:
Key checks:
- Did you forget to call
reconnect()after enabling the network?
int networkId = wifiManager.addNetwork(wifiConfig); wifiManager.enableNetwork(networkId, true); wifiManager.saveConfiguration(); // Optional for 6+, but safe for older devices wifiManager.reconnect(); // Critical—triggers an immediate reconnection attempt
- Are you setting the correct status and priority for the network?
wifiConfig.status = WifiConfiguration.Status.ENABLED; wifiConfig.priority = 10000; // Boost priority so Android picks this network first
3. Location permissions/services might not be fully active
Even if you’ve requested runtime permissions, Android 6+ requires precise location access for WiFi scanning/connecting. Double-check these:
- Are you requesting
ACCESS_FINE_LOCATION(not justCOARSE)? The latter isn’t sufficient for WiFi operations on 6+. - Is location service actually enabled? Add a check before connecting:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); boolean isLocationEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isLocationEnabled) { // Prompt user to turn on location services }
- Are you handling permission revocation? Users can turn off permissions after your app starts—add a check before each connection attempt to re-request if needed.
4. You’re not listening for system network changes
Android 6+ actively manages network connections, so if you don’t listen for disconnect events, you won’t be able to react quickly. Register a NetworkCallback to handle drops and trigger reconnections:
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkRequest request = new NetworkRequest.Builder() .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) .build(); connManager.registerNetworkCallback(request, new NetworkCallback() { @Override public void onLost(Network network) { super.onLost(network); // Trigger your reconnection logic here reconnectToTargetWifi(); } });
Wrap-up
The most likely culprit here is the system’s internet validation for local WiFi networks. Start by updating your WifiConfiguration to skip that check, then consider migrating to the newer ConnectivityManager APIs for more reliable connections on Android 6+.
内容的提问来源于stack exchange,提问作者Kikki

