单元测试Microsoft Band

时间:2015-11-28 09:29:21

标签: c# unit-testing bluetooth

尝试使用Microsoft Band nuGet包在VS2015中使用MSTest进行单元测试并遇到以下错误

"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

1 个答案:

答案 0 :(得分:1)

此处的异常和错误消息没有帮助,但您必须在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。除了模拟客户端界面并完全避免蓝牙之外,我不确定预期的修复是什么。

相关问题