From 98a9061ee7ae7980eae9f1082a71623aa64372db Mon Sep 17 00:00:00 2001 From: Arash Zandi Date: Sun, 30 Nov 2025 14:42:01 +0330 Subject: [PATCH 1/2] Wifi DeviceName added --- System.Device.Wifi/WifiAdapter.cs | 32 ++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/System.Device.Wifi/WifiAdapter.cs b/System.Device.Wifi/WifiAdapter.cs index a49c53f..fdba020 100644 --- a/System.Device.Wifi/WifiAdapter.cs +++ b/System.Device.Wifi/WifiAdapter.cs @@ -94,13 +94,13 @@ private WifiAvailableNetwork[] ParseNativeReports(byte[] nativeReport) byte[] rawSsid = new byte[33]; Array.Copy(nativeReport, bytePos, rawSsid, 0, 33); - + int ssidLength = Array.IndexOf(rawSsid, (byte)0); - if (ssidLength < 0) + if (ssidLength < 0) { ssidLength = 33; } - + WifiNetworks[index].Ssid = Encoding.UTF8.GetString(rawSsid, 0, ssidLength); bytePos += 33; @@ -191,6 +191,29 @@ public static WifiAdapter[] FindAllAdapters() return adapters; } + /// + /// Sets the device name advertised by this Wi-Fi adapter when connecting to a network. + /// + /// The device name to assign to this adapter. + /// If is , empty or the length over 32 characters. + /// Thrown when the current firmware was built without Wi-Fi and setting the device name is not supported on this platform. + /// Thrown on platforms where setting the device name is not yet implemented. + /// Thrown when the underlying Wi-Fi stack rejects to specify device name. On ESP32 this corresponds to a failure of the native esp_netif_set_hostname call. + /// + /// The device name is sent to the access point during connection and may appear in the router’s + /// client list or DHCP lease table. + /// This method must be called before initiating a connection to take effect. + /// + public void SetDeviceName(string deviceName) + { + if (string.IsNullOrEmpty(deviceName) || deviceName.Length > 32) + { + throw new ArgumentException(); + } + + NativeSetDeviceName(deviceName); + } + /// /// Directs this adapter to initiate an asynchronous network scan. /// @@ -276,6 +299,9 @@ public void Dispose() [MethodImpl(MethodImplOptions.InternalCall)] private extern byte[] GetNativeScanReport(); + [MethodImpl(MethodImplOptions.InternalCall)] + private extern void NativeSetDeviceName(string deviceName); + #endregion } From 26dd03d008a9d5c4cf9394f5f3a55287e9ff5034 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Tue, 2 Dec 2025 13:00:54 +0000 Subject: [PATCH 2/2] Apply suggestion from @josesimoes --- System.Device.Wifi/WifiAdapter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/System.Device.Wifi/WifiAdapter.cs b/System.Device.Wifi/WifiAdapter.cs index fdba020..23148fd 100644 --- a/System.Device.Wifi/WifiAdapter.cs +++ b/System.Device.Wifi/WifiAdapter.cs @@ -198,7 +198,7 @@ public static WifiAdapter[] FindAllAdapters() /// If is , empty or the length over 32 characters. /// Thrown when the current firmware was built without Wi-Fi and setting the device name is not supported on this platform. /// Thrown on platforms where setting the device name is not yet implemented. - /// Thrown when the underlying Wi-Fi stack rejects to specify device name. On ESP32 this corresponds to a failure of the native esp_netif_set_hostname call. + /// Thrown when the underlying Wi-Fi stack rejects to specify device name. /// /// The device name is sent to the access point during connection and may appear in the router’s /// client list or DHCP lease table.