【问题标题】:How to link BluetoothSerial setDataAvailableCallback() to update text display如何链接 BluetoothSerial setDataAvailableCallback() 以更新文本显示
【发布时间】:2017-04-13 02:17:12
【问题描述】:

我对@9​​87654325@ 很陌生。我正在使用 https://github.com/rusel1989/react-native-bluetooth-serial 将我的 Android 应用程序连接到 BT 设备。这个 GitHub repo 有一个例子,它可以工作。我能够从应用程序向设备发送命令。

现在我希望我的设备将数据发送回应用程序。将其视为物联网数据,每隔几秒发送几个字节。我知道可以使用setDataAvailableCallback()。我的问题是,如果我在 JSX 中创建一个 <Text /> 元素,我如何将它与回调联系起来。谢谢。

API:https://github.com/derektu/react-native-bluetooth-serial

【问题讨论】:

标签: callback react-native react-jsx jsx


【解决方案1】:

我通过尝试对我的示例进行一些更改来解决这个问题。在render() 中,我包含以下内容:<Text>{this.state.myText}</Text>

在该类的构造函数中,我向this.state 添加了一个新字段,该字段最初设置为空字符串:

this.state = {
      ...
      myText: ''
    }

在同一个构造函数中,我以 100 毫秒的周期设置了这个轮询:

setInterval(() => {
        BluetoothSerial.read()
        .then((res) => {
          this.setState({ myText: res })
        })
      }, 1000);

我将增强它以仅在字节可用时读取。另外,我的安装没有setDataAvailableCallback(),所以我不能使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    相关资源
    最近更新 更多