简体   繁体   English

反应原生 ble 管理器 BleManager.write function 没有按预期响应

[英]react native ble manager BleManager.write function doesn't respond as expected

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. Output嘿,我是新手反应原生,我想用写 function 与我的蓝牙设备通信。

The return value is;返回值为; Response From Slave(peripheral)={"_1":0,"_2":0,"_3":null,"_4":null}从机的响应(外设)={"_1":0,"_2":0,"_3":null,"_4":null}

The function above can't communicate with my bluetooth device the response shown below is wrong.上面的 function 无法与我的蓝牙设备通信,下面显示的响应是错误的。 Am I doing smt wrong.我做错了吗。 Thanks for help:) The Output感谢您的帮助:) Output

Your response is an unresolved promise.您的回复是未解决的 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.您的.then块将获得响应,但由于您没有在该块中返回它(或使用它),因此无法存储或显示它。 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;如果要存储响应,请考虑使用 async/await 语法; it's a simpler mental model.这是一个更简单的心理 model。

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

You will have to mark the function this is inside as async, of course.当然,您必须将内部的 function 标记为异步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React-Native BleManager.Write 不返回任何内容 - React-Native BleManager.Write doesn't return anything BleManager.Write 不返回任何内容(React-Native) - BleManager.Write not returning anything (React-Native) BleManager react-native - BleManager react-native 无法使用 react-native ble-manager 向 BLE 硬件写入或发送数据[写入错误状态 3] - Not able to write or send data to BLE hardware using react-native ble-manager[ write error status-3] 将 write() 与状态为 3 的“react-native-ble-manager”库一起使用时出错 - Error when using write() with "react-native-ble-manager" library with status=3 调用 api 的 js 函数没有以预期值响应 - js function calling an api doesn't respond with expected values 将数据写入 BLE 外设的函数 writeValueWithoutResponse 不存在 - The function writeValueWithoutResponse to write data to a BLE pheripheral doesn't exist 无法使用 react-native-ble-manager 通过蓝牙连接到设备 - Can't connect to device via bluetooth using react-native-ble-manager React Native中的`fetch`不会从URL返回预期的数据 - `fetch` in React Native doesn't return expected data from URL 为什么这种样式属性分配在 React Native 中没有按预期工作? - Why doesn't this style property assignment work as expected in React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM