Get info if the wifi and mobile data are on at the same time in React Native

I creating a application that shows the device info, i need to show if the wifi and mobile data are on, but even with the two on, only shows the wifi is on. There is any way to show the two at the same time?? I’m using the expo with react-native-device-info and using @react-native-community/netinfo to get the network info.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { View, Text } from 'react-native'
import React, { useEffect, useState } from 'react'
import * as Location from 'expo-location';
import NetInfo from '@react-native-community/netinfo';
import { useBatteryLevel } from 'expo-battery';
import * as Device from 'expo-device';
import * as Network from 'expo-network';
import * as Cellular from 'expo-cellular';
import Background from '@/components/Background';
import { FontAwesome, FontAwesome5, FontAwesome6, Ionicons, MaterialCommunityIcons, MaterialIcons } from '@expo/vector-icons';
// import { getTotalDiskCapacity, getFreeDiskStorage } from "react-native-device-info"
import DeviceInfo from 'react-native-device-info';
import 'expo-dev-client';
export default function Diagnostico() {
const batteryLevel = useBatteryLevel();
const [isWifiOn, setIsWifiOn] = useState<boolean>(false)
const [isMobileDataOn, setIsMobileDataOn] = useState<boolean>(false)
const [isAirplaneModeOn, setIsAirplaneModeOn] = useState<boolean>(false)
const [isGPSEnabled, setIsGPSEnabled] = useState<boolean>(false)
const [memoryUsed, setMemoryUsed] = useState<number>(0)
useEffect(() => {
const load = async () => {
const permissionForeground = await Location.requestForegroundPermissionsAsync();
const permissionBackground = await Location.requestForegroundPermissionsAsync();
if (permissionForeground.granted || permissionBackground.granted) {
// const isGPSEnabled = await Location.hasServicesEnabledAsync();
setIsGPSEnabled(await Location.hasServicesEnabledAsync())
} else {
console.log('Location permission denied');
}
console.log(`RAM: ${Device?.totalMemory! / 1e+9} bytes`);
// setMemoryUsed(Device?.totalMemory! / 1e+9)
// console.log(`Available Memory: ${Device?.availableMemory} bytes`);
const netInfo = await NetInfo.fetch();
setIsWifiOn(netInfo.isWifiEnabled!)
setIsAirplaneModeOn(await DeviceInfo.isAirplaneMode())
const totalMemory = await DeviceInfo.getTotalMemory();
const usedMemory = await DeviceInfo.getUsedMemory();
const memoryUsedPercentage = (usedMemory / totalMemory) * 100;
setMemoryUsed(memoryUsedPercentage);
}
load()
// Cleanup subscription on unmount
}, [])
useEffect(() => {
const checkConnections = async () => {
// Verifica o estado do Wi-Fi e dos dados móveis simultaneamente
const state = await NetInfo.fetch();
// Verifica se o Wi-Fi está conectado
const isWifiConnected = state.type === 'wifi' && state.isConnected;
setIsWifiOn(isWifiConnected);
// Verifica se os dados móveis estão disponíveis mesmo com o Wi-Fi ligado
const isCellularAvailable = state.details !== null && state.details.isConnectionExpensive;
setIsMobileDataOn(isCellularAvailable || (state.type === 'cellular' && state.isConnected));
};
// Inicializa a verificação das conexões
checkConnections();
// Monitora mudanças na conexão
const unsubscribe = NetInfo.addEventListener(() => {
checkConnections();
});
return () => {
unsubscribe();
};
}, []);
// let deviceId = DeviceInfo.getDeviceId();
return (
<View className='flex-1'>
<Background>
<View className='flex-row justify-between items-center'>
<Ionicons className='w-[15%]' name="cellular" size={18} color="black" />
<Text className={'text-white text-lg font-bold flex-1'}>Dados Moveis: </Text>
<Text className={`text-lg font-bold ${isMobileDataOn ? 'text-green-pastel' : 'text-red-soft'}`}>{isMobileDataOn ? "Ligado" : "Desligado"}</Text>
</View>
<View className='flex-row justify-between items-center'>
<FontAwesome className='w-[15%]' name="wifi" size={18} color="black" />
<Text className={'text-white text-lg font-bold flex-1'}>Wifi: </Text>
<Text className={`text-lg font-bold ${isWifiOn ? 'text-green-pastel' : 'text-red-soft'}`}>{isWifiOn ? "Ligado" : "Desligado"}</Text>
</View>
<View className='flex-row justify-between items-center'>
<MaterialIcons className='w-[15%]' name="airplanemode-active" size={18} color="black" />
<Text className={'text-white text-lg font-bold flex-1'}>Modo avião: </Text>
<Text className={`text-lg font-bold ${isAirplaneModeOn ? 'text-green-pastel' : 'text-red-soft'}`}>{isAirplaneModeOn ? "Ligado" : "Desligado"}</Text>
</View>
<View className='flex-row justify-between items-center'>
<FontAwesome6 className='w-[15%]' name="map-location-dot" size={18} color="black" />
<Text className={'text-white text-lg font-bold flex-1'}>GPS: </Text>
<Text className={`text-lg font-bold ${isGPSEnabled ? 'text-green-pastel' : 'text-red-soft'}`}>{isGPSEnabled ? "Ligado" : "Desligado"}</Text>
</View>
<View className='flex-row justify-between items-center'>
<FontAwesome className='w-[15%]' name="battery-3" size={18} color="black" />
<Text className={'text-white text-lg font-bold flex-1'}>Current Battery Level: </Text>
<Text className={`text-lg font-bold ${batteryLevel * 100 > 50 ? 'text-green-pastel' : 'text-red-soft'}`}>{(batteryLevel * 100).toFixed(0)}%</Text>
</View>
<View className='flex-row justify-between items-center'>
{/* <MaterialCommunityIcons name="memory" size={18} color="black" /> */}
<FontAwesome5 className='w-[15%]' name="memory" size={18} color="black" />
<Text className={'text-white text-lg font-bold flex-1'}>Memória usada: </Text>
<Text className={`text-lg font-bold ${memoryUsed < 50 ? 'text-green-pastel' : 'text-red-soft'}`}>{memoryUsed.toFixed(0)}%</Text>
</View>
</Background>
</View>
)
}
</code>
<code>import { View, Text } from 'react-native' import React, { useEffect, useState } from 'react' import * as Location from 'expo-location'; import NetInfo from '@react-native-community/netinfo'; import { useBatteryLevel } from 'expo-battery'; import * as Device from 'expo-device'; import * as Network from 'expo-network'; import * as Cellular from 'expo-cellular'; import Background from '@/components/Background'; import { FontAwesome, FontAwesome5, FontAwesome6, Ionicons, MaterialCommunityIcons, MaterialIcons } from '@expo/vector-icons'; // import { getTotalDiskCapacity, getFreeDiskStorage } from "react-native-device-info" import DeviceInfo from 'react-native-device-info'; import 'expo-dev-client'; export default function Diagnostico() { const batteryLevel = useBatteryLevel(); const [isWifiOn, setIsWifiOn] = useState<boolean>(false) const [isMobileDataOn, setIsMobileDataOn] = useState<boolean>(false) const [isAirplaneModeOn, setIsAirplaneModeOn] = useState<boolean>(false) const [isGPSEnabled, setIsGPSEnabled] = useState<boolean>(false) const [memoryUsed, setMemoryUsed] = useState<number>(0) useEffect(() => { const load = async () => { const permissionForeground = await Location.requestForegroundPermissionsAsync(); const permissionBackground = await Location.requestForegroundPermissionsAsync(); if (permissionForeground.granted || permissionBackground.granted) { // const isGPSEnabled = await Location.hasServicesEnabledAsync(); setIsGPSEnabled(await Location.hasServicesEnabledAsync()) } else { console.log('Location permission denied'); } console.log(`RAM: ${Device?.totalMemory! / 1e+9} bytes`); // setMemoryUsed(Device?.totalMemory! / 1e+9) // console.log(`Available Memory: ${Device?.availableMemory} bytes`); const netInfo = await NetInfo.fetch(); setIsWifiOn(netInfo.isWifiEnabled!) setIsAirplaneModeOn(await DeviceInfo.isAirplaneMode()) const totalMemory = await DeviceInfo.getTotalMemory(); const usedMemory = await DeviceInfo.getUsedMemory(); const memoryUsedPercentage = (usedMemory / totalMemory) * 100; setMemoryUsed(memoryUsedPercentage); } load() // Cleanup subscription on unmount }, []) useEffect(() => { const checkConnections = async () => { // Verifica o estado do Wi-Fi e dos dados móveis simultaneamente const state = await NetInfo.fetch(); // Verifica se o Wi-Fi está conectado const isWifiConnected = state.type === 'wifi' && state.isConnected; setIsWifiOn(isWifiConnected); // Verifica se os dados móveis estão disponíveis mesmo com o Wi-Fi ligado const isCellularAvailable = state.details !== null && state.details.isConnectionExpensive; setIsMobileDataOn(isCellularAvailable || (state.type === 'cellular' && state.isConnected)); }; // Inicializa a verificação das conexões checkConnections(); // Monitora mudanças na conexão const unsubscribe = NetInfo.addEventListener(() => { checkConnections(); }); return () => { unsubscribe(); }; }, []); // let deviceId = DeviceInfo.getDeviceId(); return ( <View className='flex-1'> <Background> <View className='flex-row justify-between items-center'> <Ionicons className='w-[15%]' name="cellular" size={18} color="black" /> <Text className={'text-white text-lg font-bold flex-1'}>Dados Moveis: </Text> <Text className={`text-lg font-bold ${isMobileDataOn ? 'text-green-pastel' : 'text-red-soft'}`}>{isMobileDataOn ? "Ligado" : "Desligado"}</Text> </View> <View className='flex-row justify-between items-center'> <FontAwesome className='w-[15%]' name="wifi" size={18} color="black" /> <Text className={'text-white text-lg font-bold flex-1'}>Wifi: </Text> <Text className={`text-lg font-bold ${isWifiOn ? 'text-green-pastel' : 'text-red-soft'}`}>{isWifiOn ? "Ligado" : "Desligado"}</Text> </View> <View className='flex-row justify-between items-center'> <MaterialIcons className='w-[15%]' name="airplanemode-active" size={18} color="black" /> <Text className={'text-white text-lg font-bold flex-1'}>Modo avião: </Text> <Text className={`text-lg font-bold ${isAirplaneModeOn ? 'text-green-pastel' : 'text-red-soft'}`}>{isAirplaneModeOn ? "Ligado" : "Desligado"}</Text> </View> <View className='flex-row justify-between items-center'> <FontAwesome6 className='w-[15%]' name="map-location-dot" size={18} color="black" /> <Text className={'text-white text-lg font-bold flex-1'}>GPS: </Text> <Text className={`text-lg font-bold ${isGPSEnabled ? 'text-green-pastel' : 'text-red-soft'}`}>{isGPSEnabled ? "Ligado" : "Desligado"}</Text> </View> <View className='flex-row justify-between items-center'> <FontAwesome className='w-[15%]' name="battery-3" size={18} color="black" /> <Text className={'text-white text-lg font-bold flex-1'}>Current Battery Level: </Text> <Text className={`text-lg font-bold ${batteryLevel * 100 > 50 ? 'text-green-pastel' : 'text-red-soft'}`}>{(batteryLevel * 100).toFixed(0)}%</Text> </View> <View className='flex-row justify-between items-center'> {/* <MaterialCommunityIcons name="memory" size={18} color="black" /> */} <FontAwesome5 className='w-[15%]' name="memory" size={18} color="black" /> <Text className={'text-white text-lg font-bold flex-1'}>Memória usada: </Text> <Text className={`text-lg font-bold ${memoryUsed < 50 ? 'text-green-pastel' : 'text-red-soft'}`}>{memoryUsed.toFixed(0)}%</Text> </View> </Background> </View> ) } </code>
import { View, Text } from 'react-native'
import React, { useEffect, useState } from 'react'
import * as Location from 'expo-location';
import NetInfo from '@react-native-community/netinfo';
import { useBatteryLevel } from 'expo-battery';
import * as Device from 'expo-device';
import * as Network from 'expo-network';
import * as Cellular from 'expo-cellular';
import Background from '@/components/Background';
import { FontAwesome, FontAwesome5, FontAwesome6, Ionicons, MaterialCommunityIcons, MaterialIcons } from '@expo/vector-icons';
// import { getTotalDiskCapacity, getFreeDiskStorage } from "react-native-device-info"
import DeviceInfo from 'react-native-device-info';
import 'expo-dev-client';


export default function Diagnostico() {
    const batteryLevel = useBatteryLevel();
    const [isWifiOn, setIsWifiOn] = useState<boolean>(false)
    const [isMobileDataOn, setIsMobileDataOn] = useState<boolean>(false)
    const [isAirplaneModeOn, setIsAirplaneModeOn] = useState<boolean>(false)
    const [isGPSEnabled, setIsGPSEnabled] = useState<boolean>(false)
    const [memoryUsed, setMemoryUsed] = useState<number>(0)

  
    useEffect(() => {



        const load = async () => {
            const permissionForeground = await Location.requestForegroundPermissionsAsync();
            const permissionBackground = await Location.requestForegroundPermissionsAsync();

            if (permissionForeground.granted || permissionBackground.granted) {
                // const isGPSEnabled = await Location.hasServicesEnabledAsync();
                setIsGPSEnabled(await Location.hasServicesEnabledAsync())
            } else {
                console.log('Location permission denied');
            }

            console.log(`RAM: ${Device?.totalMemory! / 1e+9} bytes`);
            // setMemoryUsed(Device?.totalMemory! / 1e+9)
            // console.log(`Available Memory: ${Device?.availableMemory} bytes`);


            const netInfo = await NetInfo.fetch();
            setIsWifiOn(netInfo.isWifiEnabled!)

            setIsAirplaneModeOn(await DeviceInfo.isAirplaneMode())

            const totalMemory = await DeviceInfo.getTotalMemory();
            const usedMemory = await DeviceInfo.getUsedMemory();
            const memoryUsedPercentage = (usedMemory / totalMemory) * 100;

            setMemoryUsed(memoryUsedPercentage);
        }
        load()

        // Cleanup subscription on unmount

    }, [])

    useEffect(() => {
        const checkConnections = async () => {
            // Verifica o estado do Wi-Fi e dos dados móveis simultaneamente
            const state = await NetInfo.fetch();

            // Verifica se o Wi-Fi está conectado
            const isWifiConnected = state.type === 'wifi' && state.isConnected;
            setIsWifiOn(isWifiConnected);

            // Verifica se os dados móveis estão disponíveis mesmo com o Wi-Fi ligado
            const isCellularAvailable = state.details !== null && state.details.isConnectionExpensive;
            setIsMobileDataOn(isCellularAvailable || (state.type === 'cellular' && state.isConnected));
        };

        // Inicializa a verificação das conexões
        checkConnections();

        // Monitora mudanças na conexão
        const unsubscribe = NetInfo.addEventListener(() => {
            checkConnections();
        });

        return () => {
            unsubscribe();
        };
    }, []);

    // let deviceId = DeviceInfo.getDeviceId();

    return (
        <View className='flex-1'>
            <Background>
                <View className='flex-row justify-between items-center'>
                    <Ionicons className='w-[15%]' name="cellular" size={18} color="black" />
                    <Text className={'text-white text-lg font-bold flex-1'}>Dados Moveis: </Text>
                    <Text className={`text-lg font-bold ${isMobileDataOn ? 'text-green-pastel' : 'text-red-soft'}`}>{isMobileDataOn ? "Ligado" : "Desligado"}</Text>
                </View>
                <View className='flex-row justify-between items-center'>
                    <FontAwesome className='w-[15%]' name="wifi" size={18} color="black" />
                    <Text className={'text-white text-lg font-bold flex-1'}>Wifi: </Text>
                    <Text className={`text-lg font-bold ${isWifiOn ? 'text-green-pastel' : 'text-red-soft'}`}>{isWifiOn ? "Ligado" : "Desligado"}</Text>
                </View>
                <View className='flex-row justify-between items-center'>
                    <MaterialIcons className='w-[15%]' name="airplanemode-active" size={18} color="black" />
                    <Text className={'text-white text-lg font-bold flex-1'}>Modo avião: </Text>
                    <Text className={`text-lg font-bold ${isAirplaneModeOn ? 'text-green-pastel' : 'text-red-soft'}`}>{isAirplaneModeOn ? "Ligado" : "Desligado"}</Text>
                </View>
                <View className='flex-row justify-between items-center'>
                    <FontAwesome6 className='w-[15%]' name="map-location-dot" size={18} color="black" />
                    <Text className={'text-white text-lg font-bold flex-1'}>GPS: </Text>
                    <Text className={`text-lg font-bold ${isGPSEnabled ? 'text-green-pastel' : 'text-red-soft'}`}>{isGPSEnabled ? "Ligado" : "Desligado"}</Text>
                </View>
                <View className='flex-row justify-between items-center'>
                    <FontAwesome className='w-[15%]' name="battery-3" size={18} color="black" />
                    <Text className={'text-white text-lg font-bold flex-1'}>Current Battery Level: </Text>
                    <Text className={`text-lg font-bold ${batteryLevel * 100 > 50 ? 'text-green-pastel' : 'text-red-soft'}`}>{(batteryLevel * 100).toFixed(0)}%</Text>
                </View>
                <View className='flex-row justify-between items-center'>
                    {/* <MaterialCommunityIcons name="memory" size={18} color="black" /> */}
                    <FontAwesome5 className='w-[15%]' name="memory" size={18} color="black" />
                    <Text className={'text-white text-lg font-bold flex-1'}>Memória usada: </Text>
                    <Text className={`text-lg font-bold ${memoryUsed < 50 ? 'text-green-pastel' : 'text-red-soft'}`}>{memoryUsed.toFixed(0)}%</Text>
                </View>
            </Background>
        </View>
    )
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật