gpt4 book ai didi

javascript - 如何从缓冲区渲染数据? react native Javascript

转载 作者:行者123 更新时间:2023-11-30 04:57:45 25 4
gpt4 key购买 nike

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

此时我设法通过蓝牙连接它并将信息存储在缓冲区中,我可以通过 console.log(buffer) 看到该缓冲区,如下图所示。

enter image description here

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

编辑:我特别想要那个缓冲区的第二个值(通常是 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] 访问数据,它应该会为您提供第二个值。

如果 buffer 是一个全局变量,如何在 React Native Component 中渲染的简单示例:

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>
)
}
}

关于javascript - 如何从缓冲区渲染数据? react native Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58836462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com