Search code examples
javascriptreact-nativebluetooth-lowenergyreact-native-ble-plxreact-native-ble-manager

React-Native BleManager.Write doesn't return anything


Hello I'm trying to communicate between my bluetooth sensor and my phone using BLE Manager.

I'm trying to create a React-Native app.

  • I can find & connect to my Bluetooth sensor.
  • I can send data.
  • I can recieve data
  • I can't collect the sensor response in a variable
  • I want to put the response in a variable(In this case writeBuffer).

async function onlyWrite(){
  let command = ['XX', 'XX', 'XX', 'XX', 'XX', '00','00','00','00','00','00','00','00','00','00','00','00','00', 'XX','XX'];
  let data = command.map(x => {return parseInt(x, 16);});
        const writeBuffer = await BleManager.write(
            "XX:XX:XX:XX:XX:XX",
            'XXXXXXXX-0000-XXXX-XXXX-XXXXXXXXXXXX',
            'XXXXXXXX-0000-XXXX-XXXX-XXXXXXXXXXXX',
            data,
            35
          )
            .then(async() => {
              console.log(`*******************************Sent ${data}`);
              console.log("*******************************ONLYWRITE Response From Slave(peripheral)="+JSON.stringify(writeBuffer));
            })
            .catch((error) => {
              console.log(error);
            });
            
            console.log("writeBuffer:"+ writeBuffer);
            return writeBuffer;//The respond this is the output 
}

HERE I CALL THE FUNCTION WITH A BUTTON

          <View style={{margin: 10}}>
              <Button title=" OnlyWrite " onPress={ () =>  console.log("OnlyWriteResponse:"+onlyWrite()) } />
          </View>

!!!THE OUTPUT HERE!!!: PICTURE The Log of the function.


Solution

  • There was a function I didn't saw here is the solution. Just get the data.value into a variable.

      const handleUpdateValueForCharacteristic = (data) => {//Just LOG
        console.log('Received data from ' + data.peripheral + ' characteristic ' + data.characteristic, data.value);
        writeBuffer=data.value;
      }