dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.28k stars 4.74k forks source link

`NetworkInterface.GetAllNetworkInterfaces()` is returning `Unknown` for the propery `NetworkInterfaceType` on Android #99979

Open mugetsu2012 opened 7 months ago

mugetsu2012 commented 7 months ago

Description

When trying to get the current available networks using NetworkInterface.GetAllNetworkInterfaces() on Adroid, we are getting Unknown for the NetworkInterfaceType property instead of a real value

Reproduction Steps

  1. Create a new Maui/Blazor Hybrid app project using VS2022 and .NET 8.
  2. Paste the below provided code that executes
  3. Run the program on Android device
  4. Inspect the result from NetworkInterface.GetAllNetworkInterfaces() and check that we don't have a Wireless NetworkInterfaceType but instead we have Unknown

using System.Net.NetworkInformation;

var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

Expected behavior

When using NetworkInterface.GetAllNetworkInterfaces() the propery NetworkInterfaceType should be populated correctly to reflect a Wireless network interface

Actual behavior

When using NetworkInterface.GetAllNetworkInterfaces() the propery NetworkInterfaceType is being set to Unknown

Regression?

First time

Known Workarounds

None

Configuration

Other information

Possibly related to: https://github.com/dotnet/runtime/issues/99721

dotnet-policy-service[bot] commented 7 months ago

Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.

steveisok commented 7 months ago

/cc @simonrozsival @vitek-karas

simonrozsival commented 7 months ago

@mugetsu2012 unfortunately, this API is fairly limited on Android. I would recommend using MAUI's Connectivity helpers (see https://learn.microsoft.com/dotnet/maui/platform-integration/communication/networking?tabs=android) or alternatively Android's platform APIs (see https://learn.microsoft.com/en-us/dotnet/api/android.net.connectivitymanager.requestnetwork?view=xamarin-android-sdk-13 and the official Android documentation for more details).

@vitek-karas we tried fixing NetworkInterface APIs on Android in .NET 7 to make them more reliable but they are still very limited and unreliable. In "recent" releases, Android's unix networking APIs were either removed, or limited in some other way. The Android APIs aren't 1:1 to the .NET APIs so we didn't use those. We should either revisit this area or we might consider deprecating these APIs completely and guiding customers to use the MAUI Connectivity APIs instead.