Flutter蓝牙设置通知

5
我已经使用flutter Blue有一段时间了,但我卡在以下问题上:我正在使用我从https://github.com/pauldemarco/flutter_blue下载的示例应用程序。基本思想是,一旦连接到我的蓝牙设备,它就开始检查是否存在服务“FFE0”,然后检查特征“FFE1”。 这个特征会输出我需要用于我的项目的随机字符串。

打开特征屏幕的图像

通过屏幕我可以看到以上情况是正确的,我只需要在连接到蓝牙设备时自动设置特征通知。

以下是我正在测试_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();
        });
      }
    });
  }

也许有人能提供解决我的问题的建议。

Robin


我使用 https://github.com/Sensirion/smart-gadget-flutter/tree/master/lib 解决了这个问题。 - Robin
Robin,欢迎来到SO。请您写一篇回答并解释“如何”。您是如何解决的?在您的示例中更改了哪些代码等等?这将有助于整个社区。谢谢。 - Sebastian Roth
1个回答

7
我找到了 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? - Ameer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接