UWP应用中如何响应蓝牙LE特征读取请求
我尝试按照这里的示例创建一个充当蓝牙LE外设的UWP应用程序: 但当尝试使用request.RespondWithValue响应特征读取请求时,我遇到一个异常: System.Exception:'对象已提交。(来自HRESULT:0x800001E的例外情况)' 如果我为特征设置了静态值,则正确读取该值UWP应用中如何响应蓝牙LE特征读取请求,uwp,bluetooth-lowenergy,Uwp,Bluetooth Lowenergy,我尝试按照这里的示例创建一个充当蓝牙LE外设的UWP应用程序: 但当尝试使用request.RespondWithValue响应特征读取请求时,我遇到一个异常: System.Exception:'对象已提交。(来自HRESULT:0x800001E的例外情况)' 如果我为特征设置了静态值,则正确读取该值 ReadParameters.StaticValue = (new byte[] { 0x21 }).AsBuffer(); 我在Windows 10 PC和Windows 10 IoT内
ReadParameters.StaticValue = (new byte[] { 0x21 }).AsBuffer();
我在Windows 10 PC和Windows 10 IoT内核上都尝试过该代码,但得到了相同的异常
响应读取请求还需要其他什么吗
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)'
}
}
公共密封部分类主页面:第页
{
GattLocalCharacteristic\u readCharacteristic;
GattServiceProvider\u服务提供商;
公共主页()
{
this.InitializeComponent();
SetupBle();
}
公共异步任务SetupBle()
{
GattServiceProviderResult=等待GattServiceProvider.CreateAsync(GattServiceUuids.Battery);
if(result.Error==BluetoothError.Success)
{
_serviceProvider=result.serviceProvider;
var ReadParameters=新的GattLocalCharacteristicParameters();
ReadParameters.CharacteristicProperties=GattCharacteristicProperties.Read;
ReadParameters.UserDescription=“电池服务”;
//ReadParameters.StaticValue=(新字节[]{0x21}).AsBuffer();//如果未注释,则静态电池电量值读取正确
GattLocalCharacteristicResult characteristicResult=Wait_serviceProvider.Service.CreateCharacteristicAsync(GattCharacteristicUuids.BatteryLevel,
读取参数);
if(characteristicResult.Error!=BluetoothError.Success)
{
返回false;
}
_readCharacteristic=characteristicResult.Characteristic;
_readCharacteristic.ReadRequested+=\u readCharacteristic\u ReadRequested;
GattServiceProviderAdvertisingParameters advParameters=新的GattServiceProviderAdvertisingParameters
{
IsDiscoverable=true,
不可连接=真
};
_serviceProvider.startAvertising(advParameters);
返回true;
}
返回false;
}
专用异步无效\u readCharacteristic\u ReadRequested(GattLocalCharacteristic发送方,GattReadRequestedEventArgs参数)
{
var writer=newdatawriter();
writer.WriteByte(0x21);
var request=await args.GetRequestAsync();
request.RespondWithValue(writer.DetachBuffer());//将引发System.Exception:“该对象已提交。(来自HRESULT的异常:0x800001E)”
}
}
根据这里的评论
还有这里的评论
文件过时且不正确
此代码适用于我:
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();
}