DEV Community

eduardo0797
eduardo0797

Posted on

Problem with react-native-ble-plx library when receiving data

I have a problem with my react native app when receiving data, since I cannot see the complete message that is sent to me, I can only see a part of the message, this is not a problem of where the message is sent since I can see in another app and if the complete message is sent.

I want to know if there is any type of additional configuration to be able to receive the complete message

I put the part of the code where I receive the data

  //Connect the device and start monitoring characteristics
  async function connectDevice(device) {

    device
      .connect()
      .then(device => {
        setConnectedDevice(device);
        setIsConnected(true);
        return device.discoverAllServicesAndCharacteristics();
      })
      .then(device => {
        //  Set what to do when DC is detected
        BLTManager.onDeviceDisconnected(device.id, (error, device) => {

          setIsConnected(false);
        });

        //Read inital values
        //Message
        device
          .readCharacteristicForService(SERVICE_UUID, MESSAGE_UUID)
          .then(valenc => {
            setMessage(base64.decode(valenc?.value));
          });

          //monitor values and tell what to do when receiving an update
          const MAX_MESSAGES = 11; // Número máximo de mensajes a almacenar
          const messages = {}; // Diccionario para almacenar los mensajes recibidos
          let currentIndex = 0; // Índice actual en el diccionario
          //Message
          device.monitorCharacteristicForService(
            SERVICE_UUID,
            MESSAGE_UUID,
            (error, characteristic) => {
              if (characteristic?.value != null) {
                const decodedMessage = base64.decode(characteristic?.value);

                messages[currentIndex] = decodedMessage; // Almacenar el mensaje en el diccionario
                currentIndex = (currentIndex + 1) % MAX_MESSAGES; // Actualizar el índice circularmente

                setVolt(messages[0]);
                setFre(messages[1]);
                setFac(messages[2]);
                setRLC1(messages[3]);
                setCor(messages[4]);
                setPot(messages[5]);
                setPRea(messages[6]);
                setPApa(messages[7]);
                setEAct(messages[8]);
                setERea(messages[9]);
                setT(messages[10]);

              }
            },
            'messagetransaction',
          );
      });
  }
Enter fullscreen mode Exit fullscreen mode

Since I can't see the whole message, the solution of my code is to send it divided into 11 parts and in code to separate the data and put it in the corresponding place, but I want to know if there is any way to receive a single long message with all content

Top comments (0)