android - React Native - 模块生命周期 - 在 "Reload"上处置资源

标签 android bluetooth react-native

我正在使用 React Native 模块 ( https://github.com/rusel1989/react-native-bluetooth-serial ) 与 Arduino 进行蓝牙通信。

一切正常。但是,当我按下“重新加载”或由于启用实时重新加载而重新加载应用程序时,不会调用模块的 onDestroy 方法。因此,套接字(和流)没有正确处理。

重新加载完成后,我无法再打开蓝牙 socket 。它需要我禁用和启用蓝牙,或重新启动应用程序。

有没有我可以实现的 ant 回调或方法,可以在我重新加载我的应用程序时正确处理这些套接字?

最佳答案

好的,在花时间研究 react-native 代码后,我找到了这个问题的答案:

在 iOS 上:

您必须在 RCTBridgeModule 实现中实现一个名为 invalidate 的方法:

只要上下文被销毁(应用程序重新加载),它就会运行,它看起来像这样:

- (void)invalidate
{
    // disconnect bluetooth here
}

这是 an example我是如何在 iOS 上做到这一点的。

在 Android 上:

你必须在你的 ReactContextBaseJavaModule 中实现 onCatalystInstanceDestroy 方法,它看起来像这样:

@Override
public void onCatalystInstanceDestroy() {
    // disconnect bluetooth here
}

这是 an example我是如何在 Android 上做到这一点的。

关于android - React Native - 模块生命周期 - 在 "Reload"上处置资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40976910/

相关文章:

java - 按下确定按钮后,Android 相机 Activity 不会返回到我的应用程序

java - 使用 TextWatcher 的 EditText 动态掩码(用于血压单位)

ios - 点击按钮打开蓝牙设置

javascript - 如何在 React Native 中使用 axios 将图像上传到服务器?

java - 从设备访问数据

c# - 如何在不使用设置应用程序的情况下在 Windows 10 平板电脑应用程序中发现未配对的蓝牙设备

java - 蓝牙 COM 端口的 RXTX 问题

react-native - React Native 白屏问题

android - 在 Android 上离线运行 React Native 应用程序

android - 如何等到蓝牙在android中打开