React Native course · Module 11: Device and sensors

Other Device APIs

2 min read ·
In this lesson5

expo-battery

npx expo install expo-battery
import * as Battery from 'expo-battery';

async function getBatteryInfo() {
  const level = await Battery.getBatteryLevelAsync(); // 0-1
  const state = await Battery.getBatteryStateAsync();
  // UNKNOWN, UNPLUGGED, CHARGING, FULL
  const lowPower = await Battery.isLowPowerModeEnabledAsync();

  return { level: Math.round(level * 100), state, lowPower };
}

// Listen for changes
Battery.addBatteryLevelListener(({ batteryLevel }) => {
  console.log('Battery level:', batteryLevel);
});

expo-brightness

npx expo install expo-brightness
import * as Brightness from 'expo-brightness';

// Get brightness
const brightness = await Brightness.getBrightnessAsync();

// Set brightness (0-1)
await Brightness.setBrightnessAsync(0.8);

// System brightness
const systemBrightness = await Brightness.getSystemBrightnessAsync();

expo-network

npx expo install expo-network
import * as Network from 'expo-network';

const networkState = await Network.getNetworkStateAsync();
console.log('Connected:', networkState.isConnected);
console.log('Type:', networkState.type); // WIFI, CELLULAR, etc.
console.log('Internet:', networkState.isInternetReachable);

// IP Address
const ip = await Network.getIpAddressAsync();

expo-clipboard

npx expo install expo-clipboard
import * as Clipboard from 'expo-clipboard';

// Copy
await Clipboard.setStringAsync('Text to copy');

// Paste
const text = await Clipboard.getStringAsync();

// Check if there is text
const hasText = await Clipboard.hasStringAsync();

expo-web-browser

npx expo install expo-web-browser
import * as WebBrowser from 'expo-web-browser';

// Open page in in-app browser
await WebBrowser.openBrowserAsync('https://atlantis.app');

// Open with callback (OAuth)
const result = await WebBrowser.openAuthSessionAsync(
  'https://auth.atlantis.app/login',
  'atlantis://callback'
);
Code for this lesson: App.tsx
import React, { useState, useEffect } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import * as Battery from 'expo-battery';
import * as Network from 'expo-network';

export default function App() {
  const [batteryLevel, setBatteryLevel] = useState(0);
  const [networkType, setNetworkType] = useState('');

  useEffect(() => {
    (async () => {
      // TODO: get the battery level (Battery.getBatteryLevelAsync returns 0-1)
      //   convert to a percentage (level * 100) and store it with setBatteryLevel
      const level = 0;
      setBatteryLevel(Math.round(level * 100));

      // TODO: get the network state with Network.getNetworkStateAsync()
      //   and store networkState.type with setNetworkType
      const networkState = { type: '' };
      // setNetworkType(...)
    })();
  }, []);

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Device Panel</Text>
      <Text style={styles.row}>Battery: {batteryLevel}%</Text>
      <Text style={styles.row}>Network: {networkType || 'unknown'}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingHorizontal: 24,
    backgroundColor: '#154360',
  },
  title: {
    fontSize: 22,
    color: '#fff',
    marginBottom: 16,
  },
  row: {
    fontSize: 18,
    color: '#aed6f1',
    marginBottom: 10,
  },
});

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. How do you install expo-battery?

  2. 2. In what range does getBatteryLevelAsync return the battery level?

These are 2 of 9 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Click in order

    Click the network check elements:

  • Horizontal ordering

    Arrange the browser opening call:

  • Code editor

    Complete the code using various APIs.

  1. Questionexpo-battery
  2. QuestionBattery level range
  3. QuestionBatteryState
  4. Click in orderNetwork state
  5. QuestionNetwork.type
  6. QuestionisInternetReachable
  7. QuestionClipboard.setStringAsync
  8. QuestionClipboard.getStringAsync
  9. Questionexpo-brightness
  10. Put in orderWebBrowser.openBrowserAsync
  11. QuestionopenAuthSessionAsync
  12. Coding taskDevice dashboard
  13. Project to submitProject: Sensors
  14. VideoModule summary

Useful articles