Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot listen for changes on characteristic.startNotifications() #85

Open
kolan51 opened this issue Mar 18, 2024 · 5 comments
Open

Cannot listen for changes on characteristic.startNotifications() #85

kolan51 opened this issue Mar 18, 2024 · 5 comments

Comments

@kolan51
Copy link

kolan51 commented Mar 18, 2024

Does there exist a valueListener in Web ble for flutter to listen for changes in notifications?

example for web ble:


  function handleScaleMeasurementCharacteristic(characteristic) {
      console.log(characteristic);
      console.log("Starting notifications...");
      return characteristic.startNotifications().then((char) => {
        console.log("Adding event listener...");
        char.addEventListener(
          "oncharacteristicvaluechanged",
          onCharacteristicValueChanged
        );
      });
    }

@jeroen1602
Copy link
Owner

I think I found the problem.

The code adds an event for characteristicvaluechanged while it should be oncharacteristicvaluechanged.

If it works then the data should be received on the value stream

@jeroen1602
Copy link
Owner

So I made a commit where the code also listens on oncharacteristicvaluechanged

But I'm not sure which one is correct since Google chrome samples uses characteristicvaluechanged.
While the Web api draft uses oncharacteristicvaluechanged.

I also haven't gotten the notifications to work locally using the devices I own. I got one that does support notifications and that also works in other apps, but the event doesn't seem to fire when using the web api.

Are you able to test this for me by either adding a git dependency to your own project using:

dependencies:
  flutter_web_bluetooth:
    git: https://github.com/jeroen1602/flutter_web_bluetooth.git

In your pubspec.yml. Source

Or checking out this project and testing it using the example project?

If you go the example project route you may need to change the logic in bluetooth_buesiness.dart to get it to find your devices.

try {
final device = await FlutterWebBluetooth.instance.requestDevice(
RequestOptionsBuilder.acceptAllDevices(
optionalServices: BluetoothDefaultServiceUUIDS.values
.map((final e) => e.uuid)
.toList(),
optionalManufacturerData: BluetoothDefaultManufacturerIdentifiers
.values
.map((final e) => e.identifier)
.toList(growable: false)));
debugPrint("Device got! ${device.name}, ${device.id}");

@kolan51
Copy link
Author

kolan51 commented Mar 19, 2024

Hey, thanks for all the help.

Eventually I got it working by using this:

final List<BluetoothService> services = await device.discoverServices();

final BluetoothService service = services
    .firstWhere((BluetoothService service) => service.uuid == serviceId);

final BluetoothCharacteristic characteristic =
    await service.getCharacteristic(charId);

await characteristic.startNotifications();

characteristic.value.forEach((ByteData element) {
  final ByteBuffer buffer = element.buffer;
  final Uint8List received =
      buffer.asUint8List(element.offsetInBytes, element.lengthInBytes);}

all the data received is now in the list received. I provisioned the device using the return from this notifications so the data returned is correct.

@jeroen1602
Copy link
Owner

Is this using the release on live on pub.dev or using the code in master?

@kolan51
Copy link
Author

kolan51 commented Mar 19, 2024

Its from the release on pub.dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants