Skip to content

Commit

Permalink
Stop notifying of value changes when a write happens (pauldemarco#526)
Browse files Browse the repository at this point in the history
* Stop writes from triggering value changes. Fixes #311, #378, #351.

* Read after write to automatically update the UI.
  • Loading branch information
pauldemarco authored Apr 2, 2020
1 parent 5e15126 commit 487d98e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
11 changes: 8 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,14 @@ class DeviceScreen extends StatelessWidget {
(c) => CharacteristicTile(
characteristic: c,
onReadPressed: () => c.read(),
onWritePressed: () => c.write(_getRandomBytes()),
onNotificationPressed: () =>
c.setNotifyValue(!c.isNotifying),
onWritePressed: () async {
await c.write(_getRandomBytes(), withoutResponse: true);
await c.read();
},
onNotificationPressed: () async {
await c.setNotifyValue(!c.isNotifying);
await c.read();
},
descriptorTiles: c.descriptors
.map(
(d) => DescriptorTile(
Expand Down
5 changes: 0 additions & 5 deletions lib/src/bluetooth_characteristic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class BluetoothCharacteristic {
.map((c) {
// Update the characteristic with the new values
_updateDescriptors(c.descriptors);
// _value.add(c.lastValue);
// print('c.lastValue: ${c.lastValue}');
return c;
});

Expand Down Expand Up @@ -123,7 +121,6 @@ class BluetoothCharacteristic {
.invokeMethod('writeCharacteristic', request.writeToBuffer());

if (type == CharacteristicWriteType.withoutResponse) {
_value.add(value);
return result;
}

Expand All @@ -141,7 +138,6 @@ class BluetoothCharacteristic {
.then((success) => (!success)
? throw new Exception('Failed to write the characteristic')
: null)
.then((_) => _value.add(value))
.then((_) => null);
}

Expand All @@ -168,7 +164,6 @@ class BluetoothCharacteristic {
.then((p) => new BluetoothCharacteristic.fromProto(p.characteristic))
.then((c) {
_updateDescriptors(c.descriptors);
_value.add(c.lastValue);
return (c.isNotifying == notify);
});
}
Expand Down

0 comments on commit 487d98e

Please sign in to comment.