background fetch not working with accelerometer in react native expo

364 Views Asked by At

im using below code to fetch the accelerometer values when my app is running in the background i have added the below code ouside of the app component

TaskManager.defineTask('firstTask', async ({ data, error }) => {
  if (error) {
    // Error occurred - check `error.message` for more details.
    return; 
  }
  if (data) {
    //below 3 lines of code is the accelerometer is for the accelerometer tracking
    Accelerometer.addListener(accelerometerData => {
      console.log(accelerometerData);
      Accelerometer.setUpdateInterval(10000);
     })
    console.log('locations')
  }
});

the below code is inside of the app component

   useEffect(() => {
    RegisterBackgroundTask();
   }, []);

  const RegisterBackgroundTask = async () => {
    try {
      await BackgroundFetch.registerTaskAsync('firstTask', {
        minimumInterval: 5, // seconds,
      })

      console.log("Task registered")
    } catch (err) {
      console.log("Task Register failed:", err)
    }
  }

when i console log something the background app works meaning when i close my app the console.log will display on the console but when i add the acceleromenter nothing happens

Any idea on what i should do

0

There are 0 best solutions below