inthehand / 32feet

Personal Area Networking for .NET. Open source and professionally supported
https://inthehand.com/components/32feet/
MIT License
799 stars 201 forks source link

Win11 C# - BluetoothRadio.Default 'Operation is not supported on this platform.' #398

Open SomeYahoo opened 4 months ago

SomeYahoo commented 4 months ago

Microsoft Visual Studio Community 2022 Version 17.8.3 VisualStudio.17.Release/17.8.3+34330.188 Microsoft .NET Framework Version 4.8.09032

Installed Version: Community

Windows 11 Pro

InTheHand.Net.Bluetooth version 4.1.40 from NuGet (latest version)

BluetoothRadio.Default yields 'Operation is not supported on this platform.' ...and returns null.

ChatGPT seems to think I need BluetoothRadio.PrimaryRadio, but this is not recognized.

static void EnableBluetooth()
{
    BluetoothRadio radio = BluetoothRadio.Default;
    if (radio == null)
    {
        Console.WriteLine("No Bluetooth radio hardware or unsupported software stack");
    }
    else
    {
        if (radio.LocalAddress != null)
        {
            // Bluetooth is enabled
            Console.WriteLine("Bluetooth is already enabled");
        }
        else
        {
            // Bluetooth is not enabled, attempt to enable it
            try
            {
                radio.Mode = RadioMode.Connectable;
                Console.WriteLine("Bluetooth is now enabled");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to enable Bluetooth: {ex.Message}");
            }
        }
    }

    Console.WriteLine("Well, I tried.");
    Console.ReadLine();

}
SomeYahoo commented 4 months ago

What I'm trying to do is find the default radio, and enable it if it's off.

peterfoot commented 4 months ago

This tends to occur when your project doesn't target a specific enough TFM and the .NET Standard dll is used from the NuGet package. The consuming project needs to target net6.0-windows7.0 or net6.0-windows10.0.19041.0 or higher to pick up the Win32 or WinRT implementations respectively.

perkjelsvik commented 4 months ago

version: InTheHand.Net.Bluetooth 4.1.40 Framework: WPF

Just to chime in here:

I reference WinRTNetworkStream directly in one of mye Bluetooth classes. If I target net6.0-windows10.0.19041.0 I can't compile the application:

CS0246: The type or namespace name 'WinRTNetworkStream' could not be found (are you missing a using directive or an assembly reference?)

So it seems that targeting net6.0-windows10.0.19041.0 forces it to Win32, not WinRT. The same happens when I target net6.0-windows10.0.20348.0. I am able to reference it when targeting net6.0-windows10.0.22000.0 however.

Based on that it would seem that Win32 is used for all versions before 10.0.22000.0?

If it's supposed to be for 10.0.19041.0 and up then maybe something is wrong with the package? I can reproduce it if I create a new blank WPF project. For my use case it would be great if 10.0.19041.0 and up used WinRT. If you'd like I can report it as a new issue if it's supposed to use WinRT.

peterfoot commented 4 months ago

@PerKjelsvik any reason you need WinRTNetworkStream over NetworkStream?

peterfoot commented 4 months ago

Also the switch between using Win32 or WinRT should be for any Windows 10 target. But for .NET 7.0 it is built against 22000, there isn't a 19041 version. For .NET 6 you should be getting WinRT as long as your target SDK is 19041 or later.

perkjelsvik commented 4 months ago

@PerKjelsvik any reason you need WinRTNetworkStream over NetworkStream?

No, not really! I have only used it in my class to be able to easily navigate directly to the platform-specific NetworkStream. Normally I would just use NetworkStream, but when I explored using the WinRT version in November (#361) I referenced it directly to look into the implementation, and that's also how I discovered it didn't work for build version 10.0.19041.0.

For .NET 6 you should be getting WinRT as long as your target SDK is 19041 or later.

Hmm, that doesn't seem to be the case? Assuming WinRTNetworkStream should be accessible when targeting net6.0-10.0.19041.0 in the application project file. Here's what I have in a new blank WPF project:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
    <RootNamespace>_32FeetTest</RootNamespace>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseWPF>true</UseWPF>
    <SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="InTheHand.Net.Bluetooth" Version="4.1.40" />
  </ItemGroup>

</Project>

In code:

using InTheHand.Net.Sockets;

BluetoothClient client = new();
var stream = client.GetStream();
var winRT = (WinRTNetworkStream)stream; // can't compile, CS0246

Inspecting the BluetoothClient I also see this:

devenv_LC6wznFKnF

In this case the compiler flag WINDOWS10_0_17763_0_OR_GREATER should have triggered, right?

Or is there something wrong with my setup?

EDIT: It's a bit strange. When I add the same flag to the default MainWindow code-behind of the blank WPF project it clearly triggers as it should:

devenv_s05S0ouvzl