问题
I use the WiFi Network Request API to connect to a network on android Q. The connection becomes available but gets lost. Here is my code
public void registerNetworkCallback() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
WifiNetworkSpecifier specifier = new WifiNetworkSpecifier.Builder()
.setSsid("r_guest").setWpa2Passphrase("guest").build();
NetworkRequest networkRequest = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
.addCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED)
.setNetworkSpecifier(specifier)
.build();
NetworkRequest.Builder builder = new NetworkRequest.Builder();
connectivityManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Log.d(TAG, "ConnectivityManager onAvailable");
}
@Override
public void onLost(Network network) {
Log.d(TAG, "ConnectivityManager onLost");
}
);
} catch (Exception e) {
}
}
I already tried this solution and I also came across the posts like this.
These is the log that NetworkCallBack throws:
2020-01-17 13:51:24.895 ConnectivityManager available
2020-01-17 13:51:24.896 ConnectivityManager onCapabilitiesChanged 1071 [ Transports: WIFI Capabilities:
NOT_METERED&NOT_RESTRICTED&TRUSTED&NOT_VPN&NOT_ROAMING&FOREGROUND&NOT_CONGESTED&NOT_SUSPENDED LinkUpBandwidth>=1048576Kbps LinkDnBandwidth>=1048576Kbps SignalStrength: -47]
2020-01-17 13:51:24.897 ConnectivityManager onLinkPropertiesChanged 1071 {InterfaceName: wlan0 LinkAddresses: [ fe80::189e:ebff:fead:5f9d/64,192.168.1.57/24 ] DnsAddresses: [ /192.168.1.1,/8.8.8.8 ] Domains: null MTU: 0 TcpBufferSizes: 524288,1048576,4194304,524288,1048576,4194304 Routes: [ fe80::/64 -> :: wlan0,192.168.1.0/24 -> 0.0.0.0 wlan0,0.0.0.0/0 -> 192.168.1.1 wlan0 ]}
2020-01-17 13:51:24.897 ConnectivityManager onBlockedStatusChanged false
2020-01-17 13:51:24.935 ConnectivityManager onCapabilitiesChanged 1071 [ Transports: WIFI Capabilities: NOT_METERED&NOT_RESTRICTED&TRUSTED&NOT_VPN&VALIDATED&NOT_ROAMING&FOREGROUND&NOT_CONGESTED&NOT_SUSPENDED LinkUpBandwidth>=1048576Kbps LinkDnBandwidth>=1048576Kbps SignalStrength: -47]
2020-01-17 13:51:24.976 ConnectivityManager onLost
I would appreciate any ideas?
来源:https://stackoverflow.com/questions/59788055/wifi-network-connection-keeps-disconnecting-on-android-q