首页 > 解决方案 > 无法在 Flutter 中使用 flutter_blue 读取多个服务的数据

问题描述

我在我的颤振应用程序中使用颤振蓝包,但问题是我无法读取第二个服务的数据。虽然我得到了第一服务特征的价值。
我从 BASIC Services UUID 获取数据,但不是从 PV Services UUID 获取数据。
但是如果我睡了大约 20~30 秒,那么它就可以工作了。分享代码:

discoverServices() async {
    List<BluetoothService> services =await widget.device.discoverServices();
    EasyLoading.show(status: 'Fetching data from device...');
    Future.delayed(Duration(seconds: 7), () async {
      Logger().d("Sensor Screen: Waiting for services to be read");
      for(BluetoothService service in services){
        String formattedServiceUUID =
            "0x${service.uuid.toString().toUpperCase().substring(4, 8)}";
        if(formattedServiceUUID==ServicesAndCharacteristics.PV_SERVICES_UUID){
          Logger().d("Sensor Screen: PV_SERVICES_UUID ==FOUND==");
          for(BluetoothCharacteristic characteristic in service.characteristics){
            String formattedCharacteristic = "0x${characteristic.uuid.toString().toUpperCase().substring(4, 8)}";
            if (formattedCharacteristic == ServicesAndCharacteristics.CHAR_2A11) {
              if(characteristic.isNotifying==false){
                await characteristic.setNotifyValue(true);
              }
              await characteristic.read();
              var value=characteristic.value;
              Logger().d("Sensor Screen: Value of VV1 value is-"+value.toString());
              CHAR_2A11_STREAM = characteristic.value;
              setState(() {
                if(isReady==false){
                  isReady = true;
                  EasyLoading.dismiss();
                }
              });
            }
          }
        }
        if(formattedServiceUUID==ServicesAndCharacteristics.BASIC_SERVICES_UUID){
          Logger().d("Sensor Screen: CHAT_SERVICE_UUID ==FOUND==");
          for(BluetoothCharacteristic characteristic in service.characteristics){
            String formattedCharacteristic = "0x${characteristic.uuid.toString().toUpperCase().substring(4, 8)}";
            Logger().d("Sensor Screen: CHAR FOUND-> "+formattedCharacteristic);
         /*     if (formattedCharacteristic == BATTERY_CHAR_UUID) {
            Logger().d("Sensor Screen: BATTERY_CHAR_UUID ==FOUND==");
            if(characteristic.properties.notify==true){
              if(characteristic.isNotifying==false){
                await characteristic.setNotifyValue(true);
              }
            }

           await characteristic.read();
            CHAR_2A02_STREAM = characteristic.value;
            setState(() {
              if(isReady==false){
                isReady = true;
                EasyLoading.dismiss();
              }
            });
          }
            if (formattedCharacteristic == CHAT_CHAR_UUID) {
              Logger().d("Sensor Screen: CHAT_CHAR_UUID ==FOUND==");
              if(characteristic.properties.notify==true){
                if(characteristic.isNotifying==false){
                  await characteristic.setNotifyValue(true);
                }
              }

              await characteristic.read();
              CHAR_2A0A_STREAM = characteristic.value;
              setState(() {
                if(isReady==false){
                  isReady = true;
                  EasyLoading.dismiss();
                }
              });
            }*/

            if (formattedCharacteristic == ServicesAndCharacteristics.CHAR_2A03) {
              if(characteristic.properties.notify==true){
                Logger().d("Sensor Screen: Notify property FOUND For -> "+formattedCharacteristic);
                if(characteristic.isNotifying==false){
                  await  characteristic.setNotifyValue(true);
                }
              }
              await  characteristic.read();
              CHAR_2A03_STREAM = characteristic.value;
              setState(() {
                if(isReady==false){
                  isReady = true;
                  EasyLoading.dismiss();
                }
              });
            }
            if (formattedCharacteristic == ServicesAndCharacteristics.CHAR_2A0A) {
              if(characteristic.properties.notify==true){
                Logger().d("Sensor Screen: Notify property FOUND For -> "+formattedCharacteristic);
                if(characteristic.isNotifying==false){
                  await  characteristic.setNotifyValue(true);
                }
              }
              await  characteristic.read();
              CHAR_2A0A_STREAM = characteristic.value;
              Logger().d("Sensor Screen: NOW CHANGE STATE OF SCREEN");
              setState(() {
                if(isReady==false){
                  isReady = true;
                  EasyLoading.dismiss();
                }
              });
            }
            if (formattedCharacteristic == ServicesAndCharacteristics.CHAR_2A04) {
              if(characteristic.properties.notify==true){
                Logger().d("Sensor Screen: Notify property FOUND For -> "+formattedCharacteristic);
                if(characteristic.isNotifying==false){
                  await characteristic.setNotifyValue(true);
                }
              }
              await characteristic.read();
              CHAR_2A04_STREAM = characteristic.value;
              setState(() {
                if(isReady==false){
                  isReady = true;
                  EasyLoading.dismiss();
                }
              });
            }
            if (formattedCharacteristic == ServicesAndCharacteristics.CHAR_2A02) {
              if(characteristic.properties.notify==true){
                Logger().d("Sensor Screen: Notify property FOUND For -> "+formattedCharacteristic);
                if(characteristic.isNotifying==false){
                  await characteristic.setNotifyValue(true);
                }
              }
              await  characteristic.read();
              CHAR_2A02_STREAM = characteristic.value;
              setState(() {
                if(isReady==false){
                  isReady = true;
                  EasyLoading.dismiss();
                }
              });
            }
            /*if (formattedCharacteristic == ServicesAndCharacteristics.CHAR_2A11) {
              if(characteristic.isNotifying==false){
                await characteristic.setNotifyValue(true);
              }
              await characteristic.read();
              CHAR_2A11_STREAM = characteristic.value;
              setState(() {
                if(isReady==false){
                  isReady = true;
                  EasyLoading.dismiss();
                }
              });
            }*/
          }
        }
      }
    });
  }

标签: flutterdartbluetooth-lowenergy

解决方案


推荐阅读