如何在整个 React Native 应用程序中保持蓝牙设备连接

问题描述 投票:0回答:1

我是本地人的新手, 我已经使用本机反应创建了一个应用程序,我正在使用与该应用程序连接并为我提供数据的蓝牙设备。一切正常。但我希望设备通过应用程序保持连接。但不是在一个页面上..因为我需要设备给我在不同页面上点击的特定按钮的数据。

有什么办法吗

非常感谢。

我尝试在单个页面上连接它工作得非常好,但是如果导航到一个单独的页面,设备连接就会丢失,并且必须再次连接才能读取数据。

我使用过 React Native ble-plx 库。

javascript android react-native bluetooth-lowenergy react-native-ble-plx
1个回答
0
投票

你应该在你的项目的根目录下这样做,让所有人都可以访问

在 root ./src 中创建一个新的 sperate 文件

const subscription = manager.onStateChange((state) => {
      if (state === 'PoweredOn') {
          this.scanAndConnect();
          subscription.remove();
      }
  }, true);
  return () => subscription.remove();
}, [manager]);

在应用程序的主根容器中导入此文件 像下面

 <SafeAreaProvider>
      <NavigationContainer>
        <View>
          <AppRoot />
          <Bluetooth /> // connection file import 
        </View>
      </NavigationContainer>
    </SafeAreaProvider>
© www.soinside.com 2019 - 2024. All rights reserved.