Flutter Blue Plus 从体重秤获取按钮按下数据并将其转换为双倍

问题描述 投票:0回答:1

当按下按钮时,我需要使用 Flutter Blue Plus 包通过 BLE 获取准确的重量值(以克为单位),这将调用 getWeight 函数。 getWeight 函数获取从重量发送的数据,然后返回以克为单位的双精度值。不幸的是,我无法将从秤发送的数据分配给列表并将相应的位转换为克。我想寻求帮助。

包内置打印结果:

重量335.0克

flutter:[FBP] [[ OnCharacteristicReceived ]] 结果:{remote_id:D5522E08-AAA5-292F-1ADF-6207EF692C2A, secondary_service_uuid:null,characteristic_uuid:fff1,成功:1,值:1012004101020501f80d167e1f0258 020181,错误代码:0,service_uuid:fff0, error_string: 成功}

重量0.0克

lfutter:[FBP] [[ OnCharacteristicReceived ]] 结果:{remote_id:D5522E08-AAA5-292F-1ADF-6207EF692C2A, secondary_service_uuid:null,characteristic_uuid:fff1,成功:1,值:1012004101020501f900007e1f0258 02015f,错误代码:0,service_uuid:fff0, error_string: 成功}

我尝试打开特征的订阅,然后将发送的值分配给int列表,不幸的是它不起作用。

代码如下。

// ignore_for_file: unused_import

import 'dart:async';
import 'dart:ffi';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';

class ReadData extends StatefulWidget {
  const ReadData({Key? key}) : super(key: key);

  @override
  State<ReadData> createState() => _ReadDataState();
}

class _ReadDataState extends State<ReadData> {
  String weightRemoteID = 'D5522E08-AAA5-292F-1ADF-6207EF692C2A';
  String weightServiceID = 'FFF0';
  String weightCharacterisicsID = 'FFF1';
  List<int> weightReceivedValue = [];
  double weightValue = 0;

  @override
  void initState() {
    _wait();
    _startScanning();
    _listenScanResult();
    super.initState();
  }

  void _wait() async {
    await FlutterBluePlus.adapterState
        .where((state) => state == BluetoothAdapterState.on)
        .first;
  }

  void _startScanning() {
    FlutterBluePlus.startScan(timeout: const Duration(seconds: 2));
  }

  void _listenScanResult() {
    FlutterBluePlus.setLogLevel(LogLevel.verbose, color: false);
    FlutterBluePlus.scanResults.listen((scanList) async {
      for (ScanResult result in scanList) {
        if (result.device.remoteId.toString() == weightRemoteID) {
          result.device.connect(autoConnect: true);
          List<BluetoothService> services =
              await result.device.discoverServices();
          services.forEach((service) async {
            var characteristics = service.characteristics;
            for (BluetoothCharacteristic c in characteristics) {
              await c.setNotifyValue(true);
              weightReceivedValue = await c.read();
              setState(() {
              });
            }
          });
        }
      }
    });
  }

  double getWeight(List<int> weightReceivedValue) {
    // to code

    return weightValue;
  }

  Widget buildButton(BuildContext context) {
    // to code

    return const FloatingActionButton(onPressed: null);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: SizedBox(
        width: double.infinity,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text("Weight"),
            const SizedBox(height: 16.0),
            Text("$getWeight g"),
          ],
        ),
      ),
    );
  }
}
flutter dart bluetooth bluetooth-lowenergy data-conversion
1个回答
0
投票

在您的代码中,您正在订阅当前连接的设备中的每个特征,之后您正在读取每个特征(??)

等待c.setNotifyValue(true);如果你不为特征添加监听器,这一行是没有用的。

然后,如果您想将值作为双精度值添加到列表 WeightReceivedValue 中,您需要执行以下操作:

列出重量接收值 = [];

var fun =等待c.read();

weightReceivedValue.add(double.parse(fun));

© www.soinside.com 2019 - 2024. All rights reserved.