C# 单元测试微软乐队
尝试在VS2015中使用MSTest和Microsoft Band nuGet软件包进行单元测试时遇到以下错误C# 单元测试微软乐队,c#,unit-testing,bluetooth,C#,Unit Testing,Bluetooth,尝试在VS2015中使用MSTest和Microsoft Band nuGet软件包进行单元测试时遇到以下错误 "Microsoft.Band.BandIOException: An error occurred while attempting to acquire the Bluetooth device service. This error can occur if the paired device is unreachable or has become unpaired from
"Microsoft.Band.BandIOException: An error occurred while attempting to acquire the Bluetooth device service.
This error can occur if the paired device is unreachable or has become unpaired from the current host.
System.InvalidOperationException: A method was called at an unexpected time. (Exception from HRESULT: 0x8000000E)".
代码在应用程序内部运行时运行良好。调用
BandClientManager.Instance.ConnectAsync
线路失败异常和错误消息在这里没有帮助,但您必须在UI线程上建立蓝牙连接。这是因为应用程序可能会提示用户并询问他们是否允许访问蓝牙设备
例如,在UWP应用程序中,可以执行以下操作以确保UI线程执行:
await Windows.ApplicationModel.Core.CoreApplication.MainView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
IBandClient client = await BandClientManager.Instance.ConnectAsync(...);
...
});
或者,如果您有权访问UI控件,则可以直接使用其Dispatcher
任何最终调用BluetoothLEDevice.FromBluetoothAddressAsync
的代码都必须在UI线程上执行。只要应用程序包清单(.appxmanifest
)发生更改,就会出现蓝牙访问提示
我无法想象这个修复对于单元测试是可靠的,因为没有UI。除了模仿客户端接口和完全避免蓝牙之外,我不确定预期的修复方案是什么