我正在使用 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/