dart 更新时从连接的蓝牙设备读取通知值

xriantvc  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(330)

我尝试在写入蓝牙设备后读取通知值的更新。This question类似,但没有答案。我已使用nRF Connect应用程序测试设备以写入并观察通知值变化,但当我尝试使用Flutter_Blue_Plus包执行此操作时(包括their example app)通知值始终为空。当我使用nRF Connect应用程序时,通知值从不为空。

如何读取更新的通知值?

用于连接到设备和发现服务的代码:

List<ScanResult> result = [];
try {
  result = await FlutterBluePlus.instance.startScan(timeout: Duration(seconds: 5)) as List<ScanResult>;

  resultIndex = getResultIndex(result, event.dataTrac.sensor.dtId.getOrCrash());
} 
  catch (ex){
     log("Scan failed because another scan was already in progress.");
}

try {
  if (resultIndex == -1){
      for (int i = 0; i < 5; i++){
        result = await FlutterBluePlus.instance.startScan(timeout: Duration(seconds: 5)) as List<ScanResult>;
        resultIndex = getResultIndex(result, event.dataTrac.sensor.dtId.getOrCrash());
        if (resultIndex > -1){
          break;
        }
      }
   }
}
catch (ex){
   log("Exception while scanning: $ex");
}

if (resultIndex > -1 && result.isNotEmpty){
  final ScanResult r = result.elementAt(resultIndex);
  //Make sure we aren't already connected to the device
  if (!(await FlutterBluePlus.instance.connectedDevices).contains(r.device)) {
    //log("Had not connected to device; connecting now");
    await r.device.connect(autoConnect: false);
    await r.device.mtu.first;
    final int response = await r.device.requestMtu(165);
    log("New Mtu: $response");
    final List<BluetoothService> serviceList = await r.device.discoverServices();
}

用于与器械交互的代码:

for (final s in serviceList) {
  //log("Service UUID: ${s.uuid}", name: "programming_status_bloc.dart");
  if (s.uuid.toString() ==
      "6e400001-b5a3-f393-e0a9-e50e24dcca9e") {
    serviceFound = true;
    final actualTxChar = s.characteristics.firstWhere((element) => element.uuid.toString() == txChar);
    final success = await actualTxChar.setNotifyValue(true);
    if (success){
      log("Set notify value.");
    }
    else {
      log("**Failed to set notify value.**");
    }
    
    final actualRxChar = s.characteristics.firstWhere((element) => element.uuid.toString() == rxChar);
    //log("Char UUID: ${char.uuid}");
    if (continueWriting) {
      continueWriting = false;
      //TODO: change CID for production
      final List<int> payload = createPayload(event.dataTrac, event.changedValuesMap, 151);
      //final payload = [12, 12, 12, 12, 12, 0, 70];
      add(ProgrammingStatusEvent.updated(true));
      
      await actualRxChar.write(payload); 
      log("Wrote to device.");

      // final readResult = await actualRxChar.read();
      // log("Read result: $readResult");
    }  
    //r.device.disconnect();
    bool readYet = false;

    final txCharSubscription = actualTxChar.value.listen((value){
      log("Notify value: $value");
    });

    await Future.delayed(Duration(seconds: 4));
    await txCharSubscription.cancel();
    await r.device.disconnect();
    await FlutterBluePlus.instance.stopScan();                      
  }
}
if (!serviceFound){
  r.device.disconnect();
  await FlutterBluePlus.instance.stopScan();
}
tsm1rwdh

tsm1rwdh1#

实际上,问题在于Flutter Blue Plus(或者可能是手机蓝牙)需要几秒钟(我测试时超过了4秒)才能接收到notify值。PC应用程序马上就接收到了。如果我把取消订阅前的延迟时间增加到10秒,notify值就能正常通过。

相关问题