Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从缓冲区渲染数据?反应本机Javascript_Javascript_Android_React Native_Bluetooth - Fatal编程技术网

如何从缓冲区渲染数据?反应本机Javascript

如何从缓冲区渲染数据?反应本机Javascript,javascript,android,react-native,bluetooth,Javascript,Android,React Native,Bluetooth,我是react native的新手,我正在使用react native ble plx库将心脏传感器连接到我的应用程序并显示其值 在这一点上,我设法通过蓝牙将其连接起来,并将信息存储在一个缓冲区中,在这里我可以使用console.log(buffer)查看,如下图所示 我的问题是如何将这些信息呈现给应用程序?我不知道如何从缓冲区处理它 编辑:我特别想要该缓冲区的第二个值(通常是70) 代码如下: scanAndConnect() { this.manager.startDevice

我是react native的新手,我正在使用react native ble plx库将心脏传感器连接到我的应用程序并显示其值

在这一点上,我设法通过蓝牙将其连接起来,并将信息存储在一个缓冲区中,在这里我可以使用console.log(buffer)查看,如下图所示

我的问题是如何将这些信息呈现给应用程序?我不知道如何从缓冲区处理它

编辑:我特别想要该缓冲区的第二个值(通常是70)

代码如下:

scanAndConnect() {
      this.manager.startDeviceScan(null,
                                   null, (error, device) => {
          this.info("Scanning...");

          if (error) {
            this.error(error.message);
            return;
          }
          console.log(device.name)
          //if (device && device.name == 'Inspire HR') {
            if (device && device.name == 'HX-00043494') {
            this.manager.stopDeviceScan()
             device.connect()
               .then((device) => {
                 this.info("Discovering services and characteristics ")
                 return device.discoverAllServicesAndCharacteristics()
           }).then((device) => {
                 this.info("Setting notifications")
            ``return this.Async_setupNotifications(device);
          })
          .then(() => {
            this.info("Listening...")
           return this.setupNotifications(device)

          }, (error) => {
            this.error(error.message)
          })
      }
    })`
  }


async Async_setupNotifications(device) {
 this.manager.characteristic  = device.characteristicsForService("0000180d-0000-1000-8000-00805f9b34fb");
const buf = Buffer.from(characteristic.value, "base64");
console.log(buf);
console.log (buf[1]);


this.manager.characteristic.isNotifying = true;
this.manager.characteristic.isReadable = true;


 if (error) {
   this.error(error.message)
             }
return ;
}

非常感谢到目前为止的帮助

假设您是从应用程序中执行console.log,您需要通过buffer.data[1]访问数据,它应该为您提供第二个值

如果
缓冲区
是一个全局变量,那么如何在React Native组件中呈现的简单示例如下:

import React from 'react';
import {View,Text} from 'react-native';

let buffer;
export default class YourComponent extends React.Component {

    Async_setupNotifications = async (device) => { // call this method from within your code
       this.manager.characteristic  = 
       device.characteristicsForService("0000180d-0000-1000-8000-00805f9b34fb");
       const buf = Buffer.from(characteristic.value, "base64");
       console.log(buf);
       console.log (buf[1]);
       this.setState({pulse: buf.data[1]}); // assign the needed data to state
    }
    render() {
       return(
          <View>
              <Text>{this.state.pulse}</Text>
          </View>
       )
    }
}
从“React”导入React;
从“react native”导入{View,Text};
让缓冲;
导出默认类YourComponent扩展React.Component{
Async\u setupNotifications=Async(device)=>{//从代码中调用此方法
this.manager.characteristic=
设备特性服务(“0000180d-0000-1000-8000-00805f9b34fb”);
const buf=Buffer.from(characteristic.value,“base64”);
控制台日志(buf);
console.log(buf[1]);
this.setState({pulse:buf.data[1]});//将所需的数据分配给state
}
render(){
返回(
{this.state.pulse}
)
}
}

假设您在应用程序中执行console.log,您需要通过buffer.data[1]访问数据,它应该给您第二个值

如果
缓冲区
是一个全局变量,那么如何在React Native组件中呈现的简单示例如下:

import React from 'react';
import {View,Text} from 'react-native';

let buffer;
export default class YourComponent extends React.Component {

    Async_setupNotifications = async (device) => { // call this method from within your code
       this.manager.characteristic  = 
       device.characteristicsForService("0000180d-0000-1000-8000-00805f9b34fb");
       const buf = Buffer.from(characteristic.value, "base64");
       console.log(buf);
       console.log (buf[1]);
       this.setState({pulse: buf.data[1]}); // assign the needed data to state
    }
    render() {
       return(
          <View>
              <Text>{this.state.pulse}</Text>
          </View>
       )
    }
}
从“React”导入React;
从“react native”导入{View,Text};
让缓冲;
导出默认类YourComponent扩展React.Component{
Async\u setupNotifications=Async(device)=>{//从代码中调用此方法
this.manager.characteristic=
设备特性服务(“0000180d-0000-1000-8000-00805f9b34fb”);
const buf=Buffer.from(characteristic.value,“base64”);
控制台日志(buf);
console.log(buf[1]);
this.setState({pulse:buf.data[1]});//将所需的数据分配给state
}
render(){
返回(
{this.state.pulse}
)
}
}

您是否尝试过类似的方法?答复.数据[1]?比如输入对象的数据字段,访问数组的第二位([1]),然后保存该值。您是否尝试过类似的方法?答复.数据[1]?比如输入对象的数据字段,访问数组的第二位([1]),然后保存这个值。问题是我使用缓冲区的函数是异步的,所以当我执行{buffer.data[1]}时,它不识别缓冲区,我尝试将它传递给其他变量,但我不知道如何做。我试着用setstate存储它,但没有成功,所以我想有一种方法可以从异步函数中去掉这个值,你介意分享一段相关的代码吗?很难推荐其他内容。请不要将问题编辑作为答案发布,问题上有“编辑”功能。我已经更新了我的答案让你开始。假设您的代码是正确的,这应该打印最后的读数。如果你想保留它们,你可能需要把所有的东西都保存为数组。问题是我使用缓冲区的函数是异步的,所以当我执行{buffer.data[1]}时,它无法识别缓冲区,我试着把它传递给其他变量,但我不知道怎么做。我试着用setstate存储它,但没有成功,所以我想有一种方法可以从异步函数中去掉这个值,你介意分享一段相关的代码吗?很难推荐其他内容。请不要将问题编辑作为答案发布,问题上有“编辑”功能。我已经更新了我的答案让你开始。假设您的代码是正确的,这应该打印最后的读数。如果您希望保留它们,可能需要将状态中的所有内容保存为数组。