无法通过蓝牙从Windows Runtime组件连接到Microsoft手环

9
我正在开发一款连接Microsoft Band并发送通知的Windows phone 8.1应用程序。我需要执行一些后台任务,因此我添加了一个Windows Runtime组件项目。
我从后台任务即Runtime组件项目中发送通知。但是出现了错误。错误如下:

Error: System.TypeInitializationException: 'Microsoft.Band.Store.StoreResources'的类型初始化程序引发了异常。---> System.Exception: 灾难性故障 (HRESULT 的异常:0x8000FFFF (E_UNEXPECTED)) at Windows.UI.Xaml.Application.get_Current() at Microsoft.Band.Store.StoreResources..cctor() --- 内部异常堆栈跟踪的结尾 --- at Microsoft.Band.Store.StoreResources.get_RfComm_FromId_ReturnedNull() at Microsoft.Band.Store.BluetoothTransport.GetTransport(RfcommDeviceService service, ILoggerProvider loggerProvider, UInt16 maxConnectAttempts) at Microsoft.Band.Store.BluetoothTransport.<>c__DisplayClass1.b__0() at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.Tasks.Task.Execute()

正如在这个问题的答案中所说,前台应用程序不应在后台应用程序尝试连接时尝试连接到手环。
  • 我的前台应用程序既没有尝试连接也没有与手环建立任何连接。
我认为错误是由于连接到蓝牙出现问题,因为我已经进行了调试并找到了错误的位置:
public async void Run(IBackgroundTaskInstance taskInstance)
    {
        var deferral = taskInstance.GetDeferral();

        try
        {
            Debug.WriteLine("Task Triggered " + DateTime.Now);
            taskInstance.Canceled += (s, e) => { };
            taskInstance.Progress = 0;

            // Get the list of Microsoft Bands paired to the phone.
            var pairedBands = await BandClientManager.Instance.GetBandsAsync();
            if (pairedBands.Length < 1)
            {
                Debug.WriteLine(
                    "This sample app requires a Microsoft Band paired to your device. Also make sure that you have the latest firmware installed on your Band, as provided by the latest Microsoft Health app.");
                return;
            }

            // This is the line I am getting the error
            using (var bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0]))
            {
                Debug.WriteLine("Tile creation started");

我的手环与微软健康应用程序的蓝牙连接良好,因此我认为我的手机和手环的蓝牙没有问题。
我前台应用程序的快照中的Package.appmanifest如下所示:

Foreground app Package.appmanifest

Windows Runtime组件项目的后台任务的Package.appmanifest如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/2010/manifest">
<Capabilities>
<DeviceCapability Name="bluetooth.rfcomm" xmlns="http://schemas.microsoft.com/appx/2013/manifest">
<Device Id="any">
    <!-- Used by the Microsoft Band SDK -->
    <Function Type="serviceId:A502CA9A-2BA5-413C-A4E0-13804E47B38F" />
    <!-- Used by the Microsoft Band SDK -->
    <Function Type="serviceId:C742E1A2-6320-5ABC-9643-D206C677E580" />
  </Device>
</DeviceCapability>

那么可能出现的问题是什么?你能提供一个解决方案或解决这个问题的方法吗?


你使用的NuGet包版本是什么?在之前的版本中,存在与加载资源字符串相关的已知问题,这些问题(希望)已经在最新版本(1.3.10702)中得到解决。 - Phil Hoff -- MSFT
@PhilHoff--MSFT 我正在使用版本(1.3.10702.1)。 - Utsav Dawn
2个回答

1
你是否在Package.appxmanifest文件中设置了正确的能力和声明?最少需要勾选"Proximity"来使用蓝牙,并在声明中指定后台任务的类型和入口点。

我已经更新了我的回答,加入了相关的能力,请在那里查看。而且我已经在声明中指定了任务类型为计时器,并添加了一个入口点。顺便说一下,我的任务已经被触发了,唯一的问题是无法与蓝牙建立连接。 - Utsav Dawn

0

经过多次尝试,我终于找到了一个解决方案。虽然我没有找到错误的实际原因,但我得到了一个可行的解决方案。

现在代码看起来像这样:

public async void Run(IBackgroundTaskInstance taskInstance)
    {
        Debug.WriteLine("Task triggered");

        var deferral = taskInstance.GetDeferral();
        var bandInfo = (await BandClientManager.Instance.GetBandsAsync()).FirstOrDefault();
        IBandClient bandClient = null;
        if (bandInfo != null)
        {
            Debug.WriteLine("Band found");
            try
            {
                bandClient = await BandClientManager.Instance.ConnectAsync(bandInfo);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex);
            }

            if (bandClient != null)
            {
                try
                {
                    Debug.WriteLine("Band connected: " + bandClient.GetFirmwareVersionAsync().Result);
                    var bandContactState = await bandClient.SensorManager.Contact.GetCurrentStateAsync();
                    Debug.WriteLine(bandContactState.State == BandContactState.NotWorn
                        ? "Band not worn"
                        : "Band worn");
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception 1: "+ ex);
                }
            }
        }
        if (bandClient != null)
        {
            deferral.Complete();
            bandClient.Dispose();
            bandClient = null;
        }
    }

前台和后台任务的包清单与问题中的相同。

唯一的区别是Microsoft.Band包版本:1.3.10417.1,我从微软提供的工作示例中获取。之前我使用的版本是1.3.10702.1


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接