【问题标题】:How to use Flutter Reactive BLE to send a UUID to a Bluetooth device and save response to string如何使用 Flutter Reactive BLE 将 UUID 发送到蓝牙设备并保存对字符串的响应
【发布时间】:2023-01-05 00:37:31
【问题描述】:

我一直在尝试找出 Flutter Reactive BLE dart 库。我浏览了 Github 示例应用程序并了解了我想工作的时间。我想知道是否有更简单的方法来做我正在做的事情。

我试过弄乱 readCharacteristic 但我没有运气。我认为我没有正确连接到设备。

final characteristic = QualifiedCharacteristic(serviceId: serviceUuid, characteristicId: characteristicUuid, deviceId: foundDeviceId);
final response = await flutterReactiveBle.readCharacteristic(characteristic);

目标是让所有相关函数和变量连接到一个文件中的 BLE 设备,所以我所要做的就是 1 行并获得我对不同 UUID 的响应。

我对 flutter 和 dart 也很陌生,所以如果我做错了,请告诉我。

【问题讨论】:

  • 你能把你的问题说清楚一点吗?
  • 我希望能够连接到蓝牙设备,向它发送一个 UUID,并将该响应保存到字符串中。基本上只有一个功能,我输入我想要读取的设备 ID 和 UUID。函数输出响应。

标签: flutter dart bluetooth-lowenergy


【解决方案1】:

您将需要知道该设备的服务 UUID 和特征 UUID。

首先执行扫描并保存设备 ID。

String? deviceId;
final flutterReactiveBle = FlutterReactiveBle();
flutterReactiveBle.scanForDevices(withServices: [serviceId], scanMode: ScanMode.lowLatency).listen((device) {
      //save the device id to a variable
      deviceId = device.id;
    }, onError: () {
      //code for handling error
    });

然后使用此设备 ID 连接到该设备。 您需要知道该设备的服务 ID 和特征 ID。

flutterReactiveBle.connectToDevice(
      id: deviceId,
      servicesWithCharacteristicsToDiscover: {serviceId: [charId1, charId2]},
      connectionTimeout: const Duration(seconds: 2),
    ).listen((connectionState) {
      if (connectionState.connectionState == DeviceConnectionState.connected) {
        
        // send the data
        final characteristic = QualifiedCharacteristic(serviceId: serviceUuid, 
        characteristicId: charId1, deviceId: deviceId); 

        var response = await flutterReactiveBle.writeCharacteristicWithResponse(characteristic, value: [0x00]);

      }
    }, onError: (Object error) {
      // Handle a possible error
    });

请注意,在许多情况下,您将获得另一个特征 UUID 的响应。因此,您将不得不听取该特征 UUID 以获得响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    相关资源
    最近更新 更多