toddabel
toddabel

Reputation: 27

Unit testing Microsoft Band

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.

Upvotes: 2

Views: 146

Answers (1)

Chris Schmich
Chris Schmich

Reputation: 29496

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.

Upvotes: 1

Related Questions