microsoft / WSA

Developer-related issues and feature requests for Windows Subsystem for Android
MIT License
1.52k stars 836 forks source link

WSA not detecting device to be offline #477

Open n-kumari opened 10 months ago

n-kumari commented 10 months ago

Steps to reproduce

I want to detect when Internet connection is lost in my app and perform certain operations accordingly. I am using following function to detect the connection

fun checkInternetConnection(context: Context): Boolean {
  val result = false;
  val connectivityManager = getSystemService(ConnectivityManager::class.java) as ConnectivityManager
  val networkRequest = NetworkRequest.Builder()
    .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
    .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
    .build()

    connectivityManager.registerNetworkCallback(networkRequest, object : NetworkCallback() {
        override fun onAvailable(network: Network) {
            super.onAvailable(network)
           result = true
        }

        override fun onLost(network: Network) {
            super.onLost(network)
           result = false
        }
    });
   Log.e("TEST", "Is network available: " + result);
   return result;
}

Create a button and set it's on click listener app as following:

        findViewById<Button>(R.id.check_connection)
            .setOnClickListener {
                if(!checkInternetConnection(applicationContext)) {
                    Toast.makeText(this, "Connection LOST!!!!!", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this, "Connected to Internet", Toast.LENGTH_SHORT).show()
                }
            }

Switch off WiFi and turned on airplane mode on Windows 11 laptop to make the WSA offline.

✔️ Expected Behavior

The toast with message Connection LOST!!!!! is displayed on button click

❌ Actual Behavior

The toast with message Connected to Internet is displayed on button click

Notes:

Other Software

No response

Please specify the version of Windows Subsystem for Android

2309.40000.10.0

parveen-bhatia commented 9 months ago

Any update on this @microsoftopensource ?