Flutter Blue Setting Notifications

2019-08-05 07:11发布

问题:

I've been using flutter Blue for some now and I'm stuck on the following: I'm using the example App I downloaded on https://github.com/pauldemarco/flutter_blue, through here the basic idea is that as soon as I connect to my bluetooth device it starts checking if the service "FFE0" exists and then the characteristic "FFE1". This Characteristic spits out random strings I need for my project.

Image of screen with characteristic open

Through the screen I can see the above is true I just need to somehow automatically set notifications for the characteristic as soon as it connects to the bluetooth device.

This is some current code i'm testing out in the _Connect Function.

_connect(BluetoothDevice d) async {
    device = d;
    // Connect to device
    deviceConnection = _flutterBlue
        .connect(device, timeout: const Duration(seconds: 4))
        .listen(
      null,
      onDone: _disconnect,
    );

// Update the connection state immediately
    device.state.then((s) {
      setState(() {
        deviceState = s;
      });
    });

// Subscribe to connection changes
    deviceStateSubscription = device.onStateChanged().listen((s) {
      setState(() {
        deviceState = s;
      });
      if (s == BluetoothDeviceState.connected) {
        device.discoverServices().then((service) {
          service.forEach((_service){
            var characteristics = _service.characteristics;
            _service.characteristics.map((c) {
              print(c);
            });
            for(BluetoothCharacteristic _characteristic in characteristics) {
              device.readCharacteristic(_characteristic).then((_value){
                print(_value);
                if (_value.contains("FFE0")) {
                  print("Found!!");
              // do something 
                }
              });
            }
          });
          setState(() {
            services = service;
          });
          _getServices();
        });
      }
    });
  }

I maybe someone has a suggestion on how to approach my problem.

Robin

回答1:

I found https://github.com/Sensirion/smart-gadget-flutter/tree/master/lib and I was able to fix my problem using the following code:

      for(BluetoothService service in services) {
        for(BluetoothCharacteristic c in service.characteristics) {
          if(c.uuid == new Guid("0000ffe1-0000-1000-8000-00805f9b34fb")) {
            _setNotification(c);
          } else {
            print("Nope");
          }
        }
      }

This was added in the _connect function.

_connect(BluetoothDevice d) async {
    device = d;
// Connect to device
    deviceConnection = _flutterBlue
        .connect(device, timeout: const Duration(seconds: 4))
        .listen(
      null,
      onDone: _disconnect,
    );

// Update the connection state immediately
    device.state.then((s) {
      setState(() {
        deviceState = s;
      });
    });

// Subscribe to connection changes
    deviceStateSubscription = device.onStateChanged().listen((s) {
      setState(() {
        deviceState = s;
      });
      if (s == BluetoothDeviceState.connected) {

        device.discoverServices().then((s) {

         services = s;

          for(BluetoothService service in services) {
            for(BluetoothCharacteristic c in service.characteristics) {
              if(c.uuid == new Guid("0000ffe1-0000-1000-8000-00805f9b34fb")) {
                _setNotification(c);
              } else {
                print("Nope");
              }
            }
          }
          setState(() {
            services = s;
          });
          _getServices();
        });
      }
    });
  }


回答2:

i found trouble if i Set multiple notifications and listen to changes