Can't get any sensor to work when app is backgrounded

Hello everyone

I’m running the SDK 39.0.0 and making a native app for Android/iOS using Expo.

So far everything has been going great, but I’ve stumbled across a problem:
I’m trying to get readings from a sensor (the accelerometer to be exact), which works perfectly fine, but it does not when the app is backgrounded.

I’ve already tried using the BackgroundFetch with TaskManager to accomplish this, but the main issue is that I need the data from the sensor at correct intervals (every 100ms), which is not possible with the BackgroundFetch API.

I’ve found a workaround in this article, which uses the WebView component to constantly run a function for every interval (100ms). This does actually work (in foreground and background), I’ve tried it with console.log, BUT the sensor listener Accelerometer.addListener() does not get called anymore when the app is backgrounded This happens for both the official method with BackgroundFetch AND the unofficial method with WebView. So my guess is that the sensor is just not working in background?

I hope someone can help me out with this because this functionality is of great importance for my final school project. Detaching from Expo is also not an option. Thanks in advance.

Here is the ‘workaround’ code that makes the background task possible:

import * as React from 'react';
import { WebView } from 'react-native-webview';

export default function BackgroundTask(props) {
  return (
        html: `<script>
          setInterval(()=>{window.ReactNativeWebView.postMessage("");}, ${props.interval})


Here is the code that implements the ‘workaround’ background-worker code (where the accelerometer is called and data is saved):

import { Accelerometer } from 'expo-sensors';
import React, { useState, useEffect } from 'react';
import BackgroundTask from '../background-workers/BackgroundTask';

export default function BackgroundTaskTest() {
  const [data, setData] = useState({});

  useEffect(() => {
    const accelSub = Accelerometer.addListener((accelerometerData) => {

    if (accelSub) {
      return function cleanup() {

  const updateAcc = () => console.log('Data:', data.x, data.y, data.z);
  /*This returned component does basically return no actual View, it just
  constantly repeats the updateAcc() function (foreground and background)*/
  return <BackgroundTask interval={100} function={updateAcc} />;

Keep in mind that the Accelerometer does not do anything in both the official method (with BackgroundFetch) and the unofficial method (with the WebView method shown above), while a simple console.log() DOES work in both methods. So my guess is that the sensor is just not working in background?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.