dart - Flutter Blue 自动书写

标签 dart flutter

我在大学作业中使用了 Flutter Blue,我需要创建一个应用程序来获取信息并将其传递给设备。像在任何应用程序中一样,此数据的传递必须是自动的(毕竟最终用户不应该寻找执行该过程所需的服务和特征)。问题是我无法在与设备连接后立即执行数据传递。

我正在使用在 https://github.com/pauldemarco/flutter_blue 下载的 App 示例,所以基本的想法是,只要我连接到我的蓝牙设备,我就会向某个设备发送一条消息。在 Flutter Blue Setting Notifications 连接时,已经有一个有兴趣设置通知的已回答问题 我遵循了相同的示例,但没有使用 _setNotification (c),而是使用了 _writeCharacteristic (c),但它不起作用。

  _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("06d1e5e7-79ad-4a71-8faa-373789f7d93c")) {
                _writeCharacteristic(c);
              } else {
                print("Nope");
              }
            }
          }
          setState(() {
            services = s;
          });
        });
      }
    });
  }

我已经更改了原始代码,以便在我执行写入方法后立即向我打印通知。通知应该向我显示设备固件中的标准消息,但它会向我打印蓝牙芯片的本地名称,因为如果我手动选择服务和特性,则返回的是正确的消息。

最佳答案

您需要详细说明如何在描述符上执行写入 - 在 _writeCharacteristic(c) 中。

BluetoothDescriptor.write() 根据 docs 是一个 Future ,您应该能够捕获写入期间抛出的任何错误。

关于dart - Flutter Blue 自动书写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55456544/

相关文章:

flutter - Dart,Flutter : Meaning of const modifier, Colors.yellow [200]不是一个常量吗?

flutter - 如何自定义SliverAppBar Flutter?

android - VS 代码 : Failed to install the following Android SDK packages as some licences have not been accepted

Flutter 中的 Azure API(面部识别)

listview - flutter : How to remove space between row in Listview

image - 如何在不使用 go-flutter 的情况下在 flutter 桌面中使用 image_picker 插件

Flutter Doctor找不到Android SDK,但一切都设置好了

flutter - 当在 chrome 中点击 localhost flutter web url 时,出现 404 Not Found

flutter - 从列表导航到联系人

flutter - 获取 Flutter ListView 中当前可见的 widget