【问题标题】:How to respond to Bluetooth LE characteristic read request in UWP application如何在 UWP 应用程序中响应蓝牙 LE 特征读取请求
【发布时间】:2017-05-19 14:20:22
【问题描述】:

我正在尝试按照此处的示例创建将充当蓝牙 LE 外围设备的 UWP 应用程序: https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-server 但是当尝试使用 request.RespondWithValue 响应特征读取请求时,我得到一个异常: System.Exception: '对象已提交。 (来自 HRESULT 的异常:0x8000001E)'

如果我为特征设置一个静态值,则该值被正确读取

 ReadParameters.StaticValue = (new byte[] { 0x21 }).AsBuffer();

我已在 Windows 10 PC 和 Windows 10 IoT Core 上尝试过代码,并得到相同的异常。

响应读取请求还需要其他什么吗?

public sealed partial class MainPage : Page
{
    GattLocalCharacteristic _readCharacteristic;
    GattServiceProvider _serviceProvider;
    public MainPage()
    {
        this.InitializeComponent();
        SetupBle();
    }
    public async Task<bool> SetupBle()
    {
        GattServiceProviderResult result = await GattServiceProvider.CreateAsync(GattServiceUuids.Battery);
        if (result.Error == BluetoothError.Success)
        {
            _serviceProvider = result.ServiceProvider;
            var ReadParameters = new GattLocalCharacteristicParameters();
            ReadParameters.CharacteristicProperties = GattCharacteristicProperties.Read;
            ReadParameters.UserDescription = "Battery service";
            //ReadParameters.StaticValue = (new byte[] { 0x21 }).AsBuffer(); //if this is uncommented the static battery level value is read correctly
            GattLocalCharacteristicResult characteristicResult = await _serviceProvider.Service.CreateCharacteristicAsync(GattCharacteristicUuids.BatteryLevel, 
                ReadParameters);
            if (characteristicResult.Error != BluetoothError.Success)
            {
                return false;
            }
            _readCharacteristic = characteristicResult.Characteristic;
            _readCharacteristic.ReadRequested += _readCharacteristic_ReadRequested;
            GattServiceProviderAdvertisingParameters advParameters = new GattServiceProviderAdvertisingParameters
            {
                IsDiscoverable = true,
                IsConnectable = true
            };
            _serviceProvider.StartAdvertising(advParameters);
            return true;
        }
        return false;
    }
    private async void _readCharacteristic_ReadRequested(GattLocalCharacteristic sender, GattReadRequestedEventArgs args)
    {
        var writer = new DataWriter();
        writer.WriteByte(0x21);
        var request = await args.GetRequestAsync();
        request.RespondWithValue(writer.DetachBuffer());//will throw System.Exception: 'The object has been committed. (Exception from HRESULT: 0x8000001E)'
    }
}

【问题讨论】:

    标签: uwp bluetooth-lowenergy


    【解决方案1】:

    根据这里的评论 https://blogs.msdn.microsoft.com/btblog/2017/05/11/welcome-to-the-new-windows-bluetooth-core-team-blog/#comment-105 还有这里的 cmets https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-server 文档已过时且不正确。

    此代码适用于我:

        private async void _readCharacteristic_ReadRequested(GattLocalCharacteristic sender, GattReadRequestedEventArgs args)
        {
            var deferral = args.GetDeferral();
            var writer = new DataWriter();
            writer.WriteByte(0x21);
            var request = await args.GetRequestAsync();
            request.RespondWithValue(writer.DetachBuffer());
            deferral.Complete();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-26
      • 2015-05-22
      • 2018-05-29
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多