【问题标题】:Flutter Blue Setting NotificationsFlutter Blue 设置通知
【发布时间】:2018-10-18 17:32:04
【问题描述】:

我现在一直在使用颤振蓝色,但我坚持以下几点: 我正在使用我在https://github.com/pauldemarco/flutter_blue 上下载的示例应用程序,通过这里的基本想法是,一旦我连接到我的蓝牙设备,它就会开始检查服务“FFE0”是否存在,然后是特征“FFE1”。 这个特性会吐出我项目所需的随机字符串。

Image of screen with characteristic open

通过屏幕我可以看到上述情况是正确的,我只需要在连接到蓝牙设备后以某种方式自动为该特征设置通知。

这是我正在 _Connect 函数中测试的一些当前代码。

_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();
        });
      }
    });
  }

我也许有人对如何解决我的问题有建议。

罗宾

【问题讨论】:

标签: android ios bluetooth dart flutter


【解决方案1】:

我找到了https://github.com/Sensirion/smart-gadget-flutter/tree/master/lib,并且能够使用以下代码解决我的问题:

      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");
          }
        }
      }

这是在 _connect 函数中添加的。

_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();
        });
      }
    });
  }

【讨论】:

  • 如何定义特征和服务的 GUID?
猜你喜欢
  • 1970-01-01
  • 2020-10-01
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 2019-06-28
相关资源
最近更新 更多