wpf - c# Bluetooth LE - ValueChanged never called - additional configuration necessary? -
this question exact duplicate of:
my question kinda update last question: c# bluetooth le - write configuration error - valuechanged never called
the problem is, connect wpf c#-application ble device, characteristic.valuechanged never called. maybe miss configuration or need send additionall configuration device, idk.
here method connecting application device:
private async task connecttowatcher(deviceinformation deviceinfo) { try { // device bluetoothledevice device = await bluetoothledevice.fromidasync(deviceinfo.id); // gatt service thread.sleep(150); var gattservicesresult = await device.getgattservicesforuuidasync(new guid(rx_service_uuid)); service = gattservicesresult.services[0]; // gatt characteristic thread.sleep(150); var gattcharacteristicsresult = await service.getcharacteristicsforuuidasync(new guid(rx_char_uuid)); characteristic = gattcharacteristicsresult.characteristics[0]; // register notifications thread.sleep(150); characteristic.valuechanged += (sender, args) => { debug.writeline($"[{device.name}] received notification containing {args.characteristicvalue.length} bytes"); }; enabletxnotification(); } catch (exception ex) when ((uint)ex.hresult == 0x800710df) { debug.writeline("bluetooth error 1"); // error_device_not_available because bluetooth radio not on. } }
and here's method sending configuration:
public async void enabletxnotification() { debug.writeline("enabletxnotification"); var gattcharacteristicsresult = await service.getcharacteristicsforuuidasync(new guid(tx_char_uuid)); if (gattcharacteristicsresult == null || gattcharacteristicsresult.status != gattcommunicationstatus.success || gattcharacteristicsresult.characteristics == null || gattcharacteristicsresult.characteristics?.count < 1) { debug.writeline(" failed find gatt characteristic."); return; } txcharacteristic = gattcharacteristicsresult.characteristics[0]; txcharacteristic.valuechanged += (sender, args) => { debug.writeline("[{device.name}] received notification containing {args.characteristicvalue.length} bytes"); }; debug.writeline(" writing cccd..."); gattwriteresult result = await txcharacteristic.writeclientcharacteristicconfigurationdescriptorwithresultasync(gattclientcharacteristicconfigurationdescriptorvalue.notify); debug.writeline($" characteristics write result: status={result.status}, protocolerror={result.protocolerror}"); }
the last line creates output status=success. still no valuechanged method (i suspect second 1 unnecassary) being called when use device. i'm running out of ideas. can help?
you should first verify characteristicconfigurationdescriptor "notify", if not should set notify , define valuechanged listener. try change "connecttowatcher" following code:
private async task connecttowatcher(deviceinformation deviceinfo) { try { // device bluetoothledevice device = await bluetoothledevice.fromidasync(deviceinfo.id); // gatt service thread.sleep(150); var gattservicesresult = await device.getgattservicesforuuidasync(new guid(rx_service_uuid)); service = gattservicesresult.services[0]; // gatt characteristic thread.sleep(150); var gattcharacteristicsresult = await service.getcharacteristicsforuuidasync(new guid(rx_char_uuid)); characteristic = gattcharacteristicsresult.characteristics[0]; // check characteristic configuration descriptor var currentdescriptorvalue = await characteristic.readclientcharacteristicconfigurationdescriptorasync(); if (currentdescriptorvalue.clientcharacteristicconfigurationdescriptor != gattclientcharacteristicconfigurationdescriptorvalue.notify) { var status = await characteristic.writeclientcharacteristicconfigurationdescriptorasync(gattclientcharacteristicconfigurationdescriptorvalue.notify); } // register notifications thread.sleep(150); characteristic.valuechanged += (sender, args) => { debug.writeline($"[{device.name}] received notification containing {args.characteristicvalue.length} bytes"); }; enabletxnotification(); } catch (exception ex) when ((uint)ex.hresult == 0x800710df) { debug.writeline("bluetooth error 1"); // error_device_not_available because bluetooth radio not on. }
}
hope helped
Comments
Post a Comment