inthehand / 32feet

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

Setting BluetoothRadio.Mode in WPF app produces an exception #432

Open MariusMichelPrimes opened 3 weeks ago

MariusMichelPrimes commented 3 weeks ago

I want to do something like this:

  var myRadio = BluetoothRadio.Default;
  var mode = myRadio.Mode;

  if (mode != RadioMode.Connectable)
  {  
      // produces an exception, as the lib uses a Windows RT Dispatcher function
      myRadio.Mode = RadioMode.Connectable;
  }

Code from BluetoothRadio.Windows, where the Windows.UI.Core.CoreWindow.GetForCurrentThread() returns null.

public RadioMode Mode
 {
     get
     {
         return _radio.State == RadioState.On ? RadioMode.Connectable : RadioMode.PowerOff;
     }
     set
     {
         Windows.UI.Core.CoreWindow.GetForCurrentThread().DispatcherQueue.TryEnqueue(async () =>
             {
                 if (await Radio.RequestAccessAsync() == RadioAccessStatus.Allowed)
                 {
                     await _radio.SetStateAsync(value == RadioMode.PowerOff ? RadioState.Off : RadioState.On);
                 }
             });
     }
 }

I suppose, that this would be a problem in a console app, too.