I am trying scan the Bluetooth devices using 32feet.NET library using the following code.
private void Button_Click(object sender, RoutedEventArgs e)
{
BluetoothClient client = new BluetoothClient();
foreach (BluetoothDeviceInfo bdi in client.DiscoverDevices())
{
System.Diagnostics.Debug.WriteLine(bdi.DeviceName + " " + bdi.DeviceAddress);
}
}
The above code returns list of available Bluetooth devices in WPF application. But when I use the same code in UWP application I am getting the following exception.
System.Runtime.InteropServices.COMException: 'A method was called at an unexpected time.
When I tried to use the async/await as per the suggestion given here to fix this exception, I am not able to use async/await in collections. So I used Task.FromResult method as below then also the exception is not fixed.
private async void Button_Click(object sender, RoutedEventArgs e)
{
BluetoothClient client = new BluetoothClient();
var dev = await Task.FromResult(client.DiscoverDevices());
foreach (BluetoothDeviceInfo bdi in dev)
{
System.Diagnostics.Debug.WriteLine(bdi.DeviceName + " " + bdi.DeviceAddress);
}
}
Can someone help me why the same code works in WPF application but not in UWP application?