Unit testing Microsoft Band

2

Trying to do unit testing using MSTest in VS2015 with the Microsoft Band nuGet package and running into the following error

"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)".

Code runs fine when run inside the application. It fails on the line to call BandClientManager.Instance.ConnectAsync.

c#
unit-testing
bluetooth
asked on Stack Overflow Nov 28, 2015 by toddabel • edited Nov 28, 2015 by Pavlin

1 Answer

1

The exception and error message are not helpful here, but you must establish Bluetooth connections on a UI thread. This is because the app might prompt the user and ask if they want to allow access to the Bluetooth device.

For example, in a UWP app, you can do the following to ensure UI thread execution:

await Windows.ApplicationModel.Core.CoreApplication.MainView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
    IBandClient client = await BandClientManager.Instance.ConnectAsync(...);
    ...
});

Alternatively, if you have access to a UI control, you can use its Dispatcher directly.

Any code that ultimately calls BluetoothLEDevice.FromBluetoothAddressAsync must do it on a UI thread. The Bluetooth access prompt will happen whenever the app package manifest (.appxmanifest) changes.

I can't imagine this fix being dependable for unit tests since there's no UI. I'm not sure what the intended fix is besides mocking the client interfaces and just avoiding Bluetooth altogether.

answered on Stack Overflow Dec 9, 2015 by Chris Schmich • edited Dec 15, 2015 by Chris Schmich

User contributions licensed under CC BY-SA 3.0