簡體   English   中英

如何在flutter中使用flutter_blue接收數據

[英]how to recieve data using flutter_blue in flutter

我實際上很困惑如何使用 flutter_blue 包接收數據,我可以發送數據但我無法接收,這是我一直在使用的示例:

  startScan() {
    setState(() {
      connectionText = "Start Scanning";
    });

    scanSubScription = flutterBlue.scan().listen((scanResult) {
      if (scanResult.device.name == TARGET_DEVICE_NAME) {
        print('DEVICE found');
        stopScan();
        setState(() {
          connectionText = "Found Target Device";
        });

        targetDevice = scanResult.device;
        connectToDevice();
      }
    }, onDone: () => stopScan());
  }

  stopScan() {
    scanSubScription?.cancel();
    scanSubScription = null;
  }

  connectToDevice() async {
    if (targetDevice == null) return;

    setState(() {
      connectionText = "Device Connecting";
    });

    await targetDevice.connect();
    print('DEVICE CONNECTED');
    setState(() {
      connectionText = "Device Connected";
    });

    discoverServices();
  }

  disconnectFromDevice() {
    if (targetDevice == null) {}

    targetDevice.disconnect();

    setState(() {
      connectionText = "Device Disconnected";
    });
  }

  discoverServices() async {
    if (targetDevice == null) return;

    List<BluetoothService> services = await targetDevice.discoverServices();
    services.forEach((service) {
      // do something with service
      if (service.uuid.toString() == SERVICE_UUID) {
        service.characteristics.forEach((characteristic) {
          if (characteristic.uuid.toString() == CHARACTERISTIC_UUID) {
            targetCharacteristic = characteristic;
            // writeData("A 300 300 300");
            setState(() {
              connectionText = "All Ready with ${targetDevice.name}";
            });
          }
        });
      }
    });
  }
    
  writeDataAndWaitForRespond() async {
    writeData("A 300 300 300");
    List<BluetoothService> services = await targetDevice.discoverServices();
    print("////////////////We're here, listening to Hive...");
    // isDeviceTurnedOn = true;
    services.forEach((service) async {
      Future.delayed(const Duration(milliseconds: 500), () async {
        print("Entered the loop...");
        var characteristics = service.characteristics;
        for (BluetoothCharacteristic c in characteristics) {
          List<int> value = await c.read();
          String stringValue = new String.fromCharCodes(value);
          print("The recieved Characteristic Value is $stringValue and $value");
          print("Entered the second loop...");
          var descriptors = c.descriptors;
          print("The descriptors value is equal to: $descriptors");
          for (BluetoothDescriptor d in descriptors) {
            List<int> value = await d.read();
            print("Entered the third loop...");
            String stringValue = new String.fromCharCodes(value);
            print("The recieved Value is $stringValue and $value");
          }
        }
      });
    });
  }

  writeData(String data) {
    if (targetCharacteristic == null) return;

    List<int> bytes = utf8.encode(data);
    targetCharacteristic.write(bytes);
  }

由於您將數據寫入targetCharacteristic使用

  writeData(String data) {
    if (targetCharacteristic == null) return;

    List<int> bytes = utf8.encode(data);
    targetCharacteristic.write(bytes);
  }

您使用相同的方法讀取數據

  Future<List<int>> readData() async {
        if (targetCharacteristic == null) return;
        return await targetCharacteristic.read();
  }

這對我來說是為所有人共享代碼。

(widget.readValues[characteristic.uuid]==null) ? Text('Value: widget.readValues[characteristic.uuid].toString()): Text('Value: ' + String.fromCharCodes(widget.readValues[characteristic.uuid]))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM