1import * as Cellular from 'expo-cellular';
2import { CellularGeneration } from 'expo-cellular';
3import * as React from 'react';
4import { useState } from 'react';
5import { ScrollView } from 'react-native';
6
7import Button from '../components/Button';
8import MonoText from '../components/MonoText';
9
10type CellularInfo = {
11  allowsVoip: boolean | null;
12  carrier: string | null;
13  isoCountryCode: string | null;
14  mobileCountryCode: string | null;
15  mobileNetworkCode: string | null;
16  generation: CellularGeneration;
17};
18
19export default function CellularScreen() {
20  const [cellularInfo, setCellularInfo] = useState<CellularInfo>();
21
22  const _getCellularInfo = async () => {
23    try {
24      const generation = await Cellular.getCellularGenerationAsync();
25      setCellularInfo({
26        allowsVoip: await Cellular.allowsVoipAsync(),
27        carrier: await Cellular.getCarrierNameAsync(),
28        isoCountryCode: await Cellular.getIsoCountryCodeAsync(),
29        mobileCountryCode: await Cellular.getMobileCountryCodeAsync(),
30        mobileNetworkCode: await Cellular.getMobileNetworkCodeAsync(),
31        generation,
32      });
33    } catch (error) {
34      alert(error.message);
35    }
36  };
37
38  return (
39    <ScrollView style={{ padding: 10 }}>
40      <Button onPress={_getCellularInfo} title="Get cellular information" style={{ padding: 10 }} />
41      {cellularInfo ? (
42        <MonoText>
43          {JSON.stringify(
44            {
45              ...cellularInfo,
46              generationName: generationMap[cellularInfo.generation ?? 0],
47            },
48            null,
49            2
50          )}
51        </MonoText>
52      ) : null}
53    </ScrollView>
54  );
55}
56
57const generationMap = {
58  [Cellular.CellularGeneration.UNKNOWN]: 'unknown',
59  [Cellular.CellularGeneration.CELLULAR_2G]: '2G',
60  [Cellular.CellularGeneration.CELLULAR_3G]: '3G',
61  [Cellular.CellularGeneration.CELLULAR_4G]: '4G',
62  [Cellular.CellularGeneration.CELLULAR_5G]: '5G',
63};
64