Skip to content

Commit cadc08b

Browse files
Arlodotexemichael-hawker
authored andcommitted
Properly attach / detach events when the characteristic is changed
1 parent c9895b2 commit cadc08b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Microsoft.Toolkit.Uwp.Connectivity/BluetoothLEHelper/ObservableGattCharacteristics.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ public ObservableGattCharacteristics(GattCharacteristic characteristic, Observab
134134
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
135135
ReadValueAsync();
136136
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
137-
138-
characteristic.ValueChanged += Characteristic_ValueChanged;
139137
}
140138

141139
/// <summary>
@@ -152,6 +150,16 @@ public GattCharacteristic Characteristic
152150
{
153151
if (_characteristic != value)
154152
{
153+
if (value is not null)
154+
{
155+
value.ValueChanged += Characteristic_ValueChanged;
156+
}
157+
158+
if (_characteristic is not null)
159+
{
160+
_characteristic.ValueChanged -= Characteristic_ValueChanged;
161+
}
162+
155163
_characteristic = value;
156164
OnPropertyChanged();
157165
}

0 commit comments

Comments
 (0)