Microsoft Band SDK Sensors.Windows 示例异常

Microsoft Band SDK Sensors.Windows sample exception

当我尝试在 Windows 10 机器上 运行 Microsoft Band SDK (1.3.10417.1) 的 Sensors.Windows 示例项目时,出现以下异常:

System.ArgumentException: Value does not fall within the expected range.
   at Windows.ApplicationModel.Store.CurrentApp.get_AppId()
   at Microsoft.Band.StoreApplicationPlatformProvider`2.GetApplicationIdAsync(CancellationToken token)
   at Microsoft.Band.BandClient.StartOrAwakeStreamingSubscriptionTasks()
   at Microsoft.Band.BandClient.SensorSubscribe(SubscriptionType type)
   at Microsoft.Band.Sensors.BandSensorBase`1.<>c__DisplayClass4.<StartReadingsAsync>b__3()
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Band.Sensors.BandSensorBase`1.<StartReadingsAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at PunchingBand.Models.PunchingModel.<Connect>d__48.MoveNext()

它似乎抛出了异常,因为 SDK 使用 CurrentApp,根据 CurrentAppSimulator 上的备注部分 here,如果未列出该应用程序,则无法使用 CurrentApp在 Windows 商店中。

如果 SDK 需要访问 CurrentApp,我如何在开发我的应用程序时让它工作?这不像我可以在预编译程序集中将 CurrentAppCurrentAppSimulator 交换。

更新:这已在 Microsoft Band SDK 版本 1.3.10702 中修复。如果可能升级到该版本,否则使用下面的 hack。

在对 .NET Reflector 进行一些调查后,我想出了一个让这个工作正常的 hack。只需在 BandClient 上设置一个名为 currentAppId 的私有字段,SDK 将不会尝试从 CurrentApp 获取它。在与客户端建立连接之后和尝试传输任何传感器之前 运行 以下内容:

using (IBandClient bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0]))
{
    Type.GetType("Microsoft.Band.BandClient, Microsoft.Band")
        .GetRuntimeFields()
        .First(field => field.Name == "currentAppId")
        .SetValue(bandClient, Guid.NewGuid());

一定要包含 System.LinqSystem.Reflection 的 using。这显然是一个非常棘手的解决方法,因此希望它在 Band SDK 的未来版本中得到解决。