jeroen1602 / flutter_web_bluetooth

A dart library to use the web bluetooth api.
https://jeroen1602.github.io/flutter_web_bluetooth/
MIT License
19 stars 15 forks source link

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

Open kolan51 opened 7 months ago

kolan51 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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. https://github.com/jeroen1602/flutter_web_bluetooth/blob/6bd5dd318ccb7f27c5f76988067974da2ec15f93/example/lib/business/bluetooth_business.dart#L48-L58

kolan51 commented 7 months ago

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 commented 7 months ago

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

kolan51 commented 7 months ago

Its from the release on pub.dev