5

我使用WiFi 网络请求 API连接到 android Q 上的网络。连接可用但丢失。这是我的代码

           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) {

        }
    } 

我已经尝试过这个解决方案,我也遇到过这样的帖子

这些是 NetworkCallBack 抛出的日志:

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

我会很感激任何想法?

4

0 回答 0