0

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

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

  • ........I can find & connect to my Bluetooth sensor.XX
  • ........I can send data.XX
  • ........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','XX','XX','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-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
            'XXXXXXXX-XXXX-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.

THANKS FOR THE HELP <3

  • 1
    Hello [Hüseyin Safa ÜNLÜ](https://stackoverflow.com/users/19941172/h%c3%bcseyin-safa-%c3%9cnl%c3%9c) aka [flyingOrangutan](https://stackoverflow.com/users/19992024/flyingorangutan). Creating new accounts and sending the [same question](https://stackoverflow.com/q/73712211/8124605) repeatedly is rarely crowned with success. Your question would probably be more likely to be answered if you took the time for a [tour] and learn how to ask a [good question](https://stackoverflow.com/help/how-to-ask). – Risto Sep 14 '22 at 08:15
  • @Risto I apolicise for being immature. You are absolutely right I'm being impatient. I thoughed my other account is cursed so, I created new one. I'm dealing with this problem for 3 days and It looks like an easy problem to solve. I think I'm losing it :D – flyingOrangutan Sep 14 '22 at 08:18
  • Hi, the write method return an empty promise, remove the `const writeBuffer = await` part. – Marco Sep 14 '22 at 09:04
  • 1
    Does this answer your question? [React-Native BleManager.Write doesn't return anything](https://stackoverflow.com/questions/73712211/react-native-blemanager-write-doesnt-return-anything) – Pandapip1 Sep 14 '22 at 14:45

1 Answers1

0

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

  const handleUpdateValueForCharacteristic = (data) => {//Just LOG
    console.log('Received data from ' + data.peripheral + ' characteristic ' + data.characteristic, data.value);
    writeBuffer=data.value;
  }
Iva
  • 2,447
  • 1
  • 18
  • 28