Accelerometer.isAvailableAsync() doesn't work ?

Hello,

In my App I use Accelerometer and I would like to use the function Accelerometer.isAvailableAsync().

My App is managed with Expo.
This is my code :

import { Accelerometer } from 'expo';

...

isAccelerometerAvailable(){

    Accelerometer.isAvailableAsync()
    .then(
      result => {
        this.setState({
          isAccelerometerAvailable: String(result)
        });
      },
      error => {
          this.setState({
            isAccelerometerAvailable: "Could not get isAvailable: " + error
        });
      }
    );
  }

I receive this error :

TypeError: _expo.Accelerometer.isAvailableAsync is not a function. (In '_expo.Accelerometer.isAvailableAsync()', '_expo.Accelerometer.isAvailableAsync' is undefined)]

Someone would have a solution ?

Hey @jubertoo,

Can you provide some more information as to how it “doesn’t work”? It’s impossible for us to help you out without concrete errors or detailed explanations of what’s occurring.

Cheers,
Adam

Thank you @adamjnav for this precision.
I edited my request

Hmmm. Can you share your app.json and package.json?

package.json

{
  "expo": {
    "name": "test",
    "icon": "./assets/icon.png",
    "version": "1.0.0",
    "slug": "test",
    "sdkVersion": "1.0.0",
    "ios": {
      "bundleIdentifier": "com.test.test"
    },
    "android": {
      "package": "com.test.test"
    }
  },
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "eject": "expo eject"
  },
  "dependencies": {
    "create-react-context": "^0.2.3",
    "expo": "^32.0.0",
    "react": "16.5.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
    "react-native-fast-image-expo": "0.0.5",
    "react-native-modal": "^9.0.0",
    "react-native-responsive-screen": "^1.2.2",
    "react-native-swipe-gestures": "^1.0.3",
    "react-navigation": "^3.7.1",
    "react-navigation-transitions": "^1.0.10",
    "react-redux": "^6.0.1",
    "redux": "^4.0.1"
  },
  "devDependencies": {
    "babel-preset-expo": "^5.0.0"
  },
  "private": true
}

app.json

{
  "expo": {
    "name": "test",
    "slug": "test",
    "privacy": "public",
    "sdkVersion": "32.0.0",
    "platforms": [
      "ios",
      "android"
    ],
    "version": "1.0.0",
    "orientation": "landscape",
    "icon": "./assets/icon.png",
    "splash": {
      "image": "./assets/splash.png",
      "resizeMode": "contain",
      "backgroundColor": "#ffffff"
    },
    "updates": {
      "fallbackToCacheTimeout": 0
    },
    "assetBundlePatterns": [
      "**/*"
    ],
    "ios": {
      "bundleIdentifier": "com.test.test",
      "supportsTablet": true
    },
    "android": {
      "package": "com.test.test"
    }
  }
}

Quelqu’un aurait une solution?

Hey @jubertoo,

Sorry for the delay here. Can you try creating a Snack that reproduces this issue? From your json files, it looks like things should be working properly.

Hey @adamjnav thank you for your answer.
Here is the Snack link : https://snack.expo.io/H1KKHhM3V
I have the same error…

Hey @jubertoo,

You need to use an async function. Change your isAccelerometerAvailable function to this:

isAccelerometerAvailable = async () => {
    let isAvailable = await Accelerometer.isAvailableAsync()
  }

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