【问题标题】:Task ends when getting GattService [C# UWP]获取 GattService 时任务结束 [C# UWP]
【发布时间】:2017-04-13 04:24:57
【问题描述】:

使用:Windows 10、C# .NET 2015 社区、UWP

我正在尝试构建一个 windows-universal-app 以将我的 PC 与 BLE 设备配对。

目前正在运行的是枚举附近的设备,与选定的设备配对并获取电池电量和固件版本等信息。

现在的问题是,当我尝试获取自定义服务时,我的任务因 .GetGattService 处的“System.Exception”而结束

System.Exception.Message:“未找到元素。(来自 HRESULT 的异常:0x80070490)”

System.Exception.Stack : "在 Windows.Devices.Bluetooth.BluetoothLEDevice.GetGattService(Guid serviceUuid)\r\n 在 SettingsCs.Settings.d__23.MoveNext()"

这是不工作的代码:

private async Task<SettingsReturn> writeSettingTransition(BluetoothLEDevice device, byte[] byteSettings)
    {
        //Check if device is available
        if (device != null)
        {
            Guid SERVICE_CUSTOM = new Guid("7e0bc6be-8271-4f5a-a126-c24220e6250c");
            GattDeviceService service = device.GetGattService(SERVICE_CUSTOM);
            //Check if service is available
            if (service == null)
            {
                return SettingsReturn.INIT_ERROR;
            }
            GattCharacteristic characteristic = service.GetCharacteristics(BLETestApp.CHAR_SETTINGS)[0];
            //Check if characteristic is available
            if (characteristic == null)
            {
                return SettingsReturn.INIT_ERROR;
            }

            var writer = new DataWriter();
            writer.WriteBytes(byteSettings);
            var buffer = writer.DetachBuffer();
            await characteristic.WriteValueAsync(buffer);//********
            bool success = characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Write);

            if (success == true)
            {
                // Take care of the 8 bit byte for the counter (max = 255 (unsigned))
                if (TRANSACTION_ID > 250)
                {
                    TRANSACTION_ID = 0;
                }
                else
                {
                    // Count TANSACTION_ID one up
                    TRANSACTION_ID++;
                }
                return SettingsReturn.OK;
            }
            else
            {
                return SettingsReturn.WRITE_ERROR;
            }
        }
        else
        {
            return SettingsReturn.INIT_ERROR;
        }            
    }

我希望有人可以帮助我或告诉我我做错了什么。

【问题讨论】:

  • 您可以编辑您的帖子并添加异常详细信息(消息和堆栈跟踪)吗?
  • @PedroLamas 抱歉,我忘记了。现在添加详细信息。
  • 您是否设置了必要的功能?您可能需要检查this
  • @PedroLamas 首先感谢您的帮助。在我的代码中,我有 &lt;DeviceCapability Name="bluetooth.genericAttributeProfile"&gt; &lt;Device Id="any"&gt; &lt;Function Type="serviceId:7e0bc6be-8271-4f5a-a126-c24220e6250c" /&gt; &lt;/Device&gt; &lt;/DeviceCapability&gt;

标签: c# win-universal-app bluetooth-lowenergy


【解决方案1】:

我无法确定您遇到的错误,请使用调试器检查您的特征是否具有写入权限。

我已经更改了您的代码,以便成功写入我的设备。 还添加了一个 try catch 块。 这里是:

 private async Task<SettingsReturn> writeSettingTransition(BluetoothLEDevice device, byte[] byteSettings)
        {
            bool success = false;
            //Check if device is available
            if (device != null)
            {
                Guid SERVICE_CUSTOM = new Guid("7e0bc6be-8271-4f5a-a126-c24220e6250c");
                GattDeviceService service = device.GetGattService(SERVICE_CUSTOM);
                //Check if service is available
                if (service == null)
                {
                    return SettingsReturn.INIT_ERROR;
                }
                GattCharacteristic characteristic = service.GetCharacteristics(BLETestApp.CHAR_SETTINGS)[0];
                //Check if characteristic is available
                if (characteristic == null)
                {
                    return SettingsReturn.INIT_ERROR;
                }

                IBuffer writeBuffer = byteSettings.AsBuffer();// using Windows.Storage.Streams

                try
                {
                    // BT_Code: Writes the value from the buffer to the characteristic.
                    var result = await characteristic.WriteValueAsync(writeBuffer);
                    if (result == GattCommunicationStatus.Success)
                    {
                        // NotifyUser("Successfully wrote value to device" );
                        success = true;
                    }
                    else
                    {
                        // NotifyUser($"Write failed: {result}");
                        success = false;
                    }
                }
                catch (Exception ex) when ((uint)ex.HResult == 0x80650003 || (uint)ex.HResult == 0x80070005)
                {
                    // E_BLUETOOTH_ATT_WRITE_NOT_PERMITTED or E_ACCESSDENIED
                    // This usually happens when a device reports that it support writing, but it actually doesn't.
                    // NotifyUser(ex.Message, NotifyType.ErrorMessage);
                }

                if (success)
                {
                    // Take care of the 8 bit byte for the counter (max = 255 (unsigned))
                    if (TRANSACTION_ID > 250)
                    {
                        TRANSACTION_ID = 0;
                    }
                    else
                    {
                        // Count TANSACTION_ID one up
                        TRANSACTION_ID++;
                    }
                    return SettingsReturn.OK;
                }
                else
                {
                    return SettingsReturn.WRITE_ERROR;
                }
            }
            else
            {
                return SettingsReturn.INIT_ERROR;
            }
        }

可能存在拼写错误和其他事故,希望对您有所帮助。

【讨论】:

  • 感谢您的帮助。可悲的是,我仍然在GattDeviceService service = device.GetGattService(SERVICE_CUSTOM); 得到同样的异常
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 2016-02-27
  • 1970-01-01
  • 2020-01-22
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
相关资源
最近更新 更多