2017-05-04 158 views
0

我試圖按照這裏的示例創建將充當藍牙LE外圍設備的UWP應用程序: https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-server 但嘗試響應特徵性讀取請求與request.RespondWithValue我收到一個異常: System.Exception:'該對象已被提交。 (異常來自HRESULT:0x8000001E)」如何響應UWP應用程序中的藍牙LE特徵讀取請求

如果我設置爲特性的靜態值,該值被正確讀取

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

我試圖在Windows 10 PC和Windows 10物聯網核心上的代碼都並得到相同的例外。

是否還有其他需要響應讀取請求?

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)' 
    } 
} 

回答

0

根據這裏的評論 https://blogs.msdn.microsoft.com/btblog/2017/05/11/welcome-to-the-new-windows-bluetooth-core-team-blog/#comment-105 和意見在這裏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(); 
    }