Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

how to maintain bluetooth device connection throughout the react native app

New member
Joined
Feb 7, 2023
Messages
27
I'm new to react native, ive created an app using react native, im working on bluetooth device that connects with app and gives me data.. everything is working fine. but i want the device to stay connected through out the app. but not at the single page.. since i need the device to give me data on specific button click on different pages.
is there a way to do it
thank you soo much.
i tried to connect in a single page it worked absolutely fine, but if navigated to separate page the device connection is lost and again have to connect to read data.
ive used react native ble-plx library.
 
New member
Joined
Feb 7, 2023
Messages
21
you should do this in root of your project that accessible to all over

create a new sperate file in root ./src

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

import this file in main root container of app like below

Code:
 <SafeAreaProvider>
      <NavigationContainer>
        <View>
          <AppRoot />
          <Bluetooth /> // connection file import
        </View>
      </NavigationContainer>
    </SafeAreaProvider>
 
Top