ここでは、Bluetooth LE周辺機器として動作するUWPアプリケーションを作成する例を示します。 https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-server しかし、特性読み取り要求request.RespondWithValueと私は例外が発生します: System.Exception: 'オブジェクトがコミットされました。 (HRESULTからの例外:0x8000001E)は、」UWPアプリケーションでのBluetooth LE特性の読み取り要求への対応方法
私は特性のための静的な値を設定した場合、値が私は、Windows 10 PCおよびWindows 10のIoTコア上のコードの両方を試してみました
ReadParameters.StaticValue = (new byte[] { 0x21 }).AsBuffer();
正しく読み込まれ同じ例外が発生します。
読み取り要求に応答するために必要なことはありますか?
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)'
}
}