android - Flutter Blue 设置通知

标签 android ios bluetooth dart flutter

我现在一直在使用 flutter Blue,但我坚持以下几点: 我正在使用在 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();
        });
      }
    });
  }

我可能有人对如何解决我的问题有建议。

罗宾

最佳答案

我找到了 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();
        });
      }
    });
  }

关于android - Flutter Blue 设置通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52879548/

相关文章:

java - 如何正确运行后台服务?

ios - Objective-C中的“发现扩展时遇到的错误”

ios - 在不损失质量的情况下调整图像大小的最佳方法是什么?

java - 每次在 libgdx 中单击按钮时,如何向前和向后移动 Sprite?

android - textCapSentences 适用于模拟器,但不适用于真实设备

android - 如何在cordova android App中使用proguard

ios - 在 UITableViewController 上编辑 UITextField 时滚动区域不正确

java - BluetoothSocket isConnected() 函数在运行时导致 java.lang.NoSuchMethodError

java - toString 不适用于 getSelectedItem - Android

c++ - 低功耗蓝牙 : setting characteristic to byte array sends wrong values