Skip to content

Commit

Permalink
Listen to oncharacteristicvaluechanged as well.
Browse files Browse the repository at this point in the history
It is ambiguous what event needs to be handled.

(Google chrome samples)[https://googlechrome.github.io/samples/web-bluetooth/notifications.html] uses `characteristicvaluechanged`.
While the (Web api draft)[https://webbluetoothcg.github.io/web-bluetooth/#dom-characteristiceventhandlers-oncharacteristicvaluechanged] uses `oncharacteristicvaluechanged`.

I haven't got either one to work with the devices on my range though.

Fixed bug in example where it would not update the `notify` button once it has started.

Fixed bug where example couldn't handle a service or characteristic that isn't in the default list.
  • Loading branch information
jeroen1602 committed Mar 18, 2024
1 parent 5c1a231 commit 6bd5dd3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions example/lib/widgets/characteristic_actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class ActionsSate extends State<ActionsWidget> {
return () async {
try {
await method.call();
setState(() {
// Make sure `isNotifying` is updated.
});
} catch (e, s) {
debugPrint("$e\n$s");
if (mounted) {
Expand Down
3 changes: 2 additions & 1 deletion example/lib/widgets/characteristic_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class CharacteristicWidget extends StatefulWidget {
CharacteristicWidget({required this.characteristic, super.key}) {
characteristicName = BluetoothDefaultCharacteristicUUIDS.values
.cast<BluetoothDefaultCharacteristicUUIDS?>()
.firstWhere((final element) => element?.uuid == characteristic.uuid)
.firstWhere((final element) => element?.uuid == characteristic.uuid,
orElse: () => null)
?.name;
}

Expand Down
3 changes: 2 additions & 1 deletion example/lib/widgets/service_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class ServiceWidget extends StatefulWidget {
ServiceWidget({required this.service, super.key}) {
serviceName = BluetoothDefaultServiceUUIDS.values
.cast<BluetoothDefaultServiceUUIDS?>()
.firstWhere((final element) => element?.uuid == service.uuid)
.firstWhere((final element) => element?.uuid == service.uuid,
orElse: () => null)
?.name;
}

Expand Down
7 changes: 7 additions & 0 deletions lib/src/bluetooth_characteristic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class BluetoothCharacteristic {
_value.add(data);
}
});
_characteristic.addEventListener("oncharacteristicvaluechanged",
(final event) {
final data = _characteristic.value;
if (data != null) {
_value.add(data);
}
});
}

final WebBluetoothRemoteGATTCharacteristic _characteristic;
Expand Down

0 comments on commit 6bd5dd3

Please sign in to comment.