0
const infoConnected = () => {//Show the connected devices
BleManager.getConnectedPeripherals([]).then((results) => {
  if (results.length == 0) {
    console.log('No connected peripherals')
  }
  //for (var i = 0; i < 1;/*list2.length;*/ i++) {//once for now
    console.log("Connected Device Variables:");
  
    //FULL INFO
    let deviceInfo=list2[0];
    console.log(deviceInfo);

    //Split INFO
    console.log("peripheralId INFO:");
    console.log(list2[0].id);

    console.log("serviceUUID INFO:");
    console.log(list2[0].advertising.serviceUUIDs);

    console.log("characteristicUUID INFO:");
    console.log(list2[0].id);

    //write and take a promis
    //const data=stringToBytesToBytes('dsadsa');
    //const dataByte = convertString.UTF8.stringToBytes(data);

    //const data = stringToBytes(String(12345));
    //const dataByte = convertString.UTF8.stringToBytes(data);

    //const data = stringToBytes("XXXXXXXXXXXXXXXXXXXXXX");
    
    let command = ['XX', '00', 'XX', '00', '00', '00','00','00','00','00','00','00','00','00','00','00','00','00', 'XX','XX'];
    let data = command.map(x => {return parseInt(x, 16);});

    response=BleManager.write(
      "XX:24:XX:XX:37:XX",
      'XX-0000-1000-8000-XX',
      'XX-0000-1000-8000-XX',
      data,
    )
      .then(() => {
        console.log(`*******************************Sent ${data}`);
      })
      .catch((error) => {
        console.log(error);
      });

      console.log("*******************************Response From Slave(peripheral)="+JSON.stringify(response));

  });



  BleManager.read(
    "4C:XX:XX:XX:XX:XX",
    'XX-0000-XXXX-XXXX-XX',
    'XX-0000-XXXX-XXXX-XX'
  )
    .then((readData) => {
      // Success code
      console.log("*******************************Read: " + readData);
  
      buffer = Buffer.Buffer.from(readData); //https://github.com/feross/buffer#convert-arraybuffer-to-buffer
      const sensorData=buffer.readUInt8(1, true);
      console.log("*******************************Buffer read:"+ buffer);
      console.log("*******************************SensorData read:"+sensorData);
      //const sensorData = buffer.readUInt8(1, true);
    })
    .catch((error) => {
      // Failure code
      console.log(error);
    });   

Output Hey, I'm new to react native, I want to use write function to communicate with my bluetooth device.

The return value is; Response From Slave(peripheral)={"_1":0,"_2":0,"_3":null,"_4":null}

The function above can't communicate with my bluetooth device the response shown below is wrong. Am I doing smt wrong. Thanks for help :) The Output

HUV
  • 59
  • 7

1 Answers1

1

Your response is an unresolved promise. Your .then block will get the response, but since you're not returning it in that block (or using it), it can't be stored or displayed. Try this instead

  .then(response => {
    console.log(`*******************************Sent ${data}`);
    console.log("*******************************Response From Slave(peripheral)="+JSON.stringify(response));
  });

If you want to store the response, consider using async/await syntax; it's a simpler mental model.

  const response = await BleManager.write(
    // ...
  );
  console.log(response);

You will have to mark the function this is inside as async, of course.

Abe
  • 4,500
  • 2
  • 11
  • 25