Reputation: 1131
i am using react-native-netinfo in my app. In iOS it's working fine. But in android always returns true. It doesn't have internet connection (WiFi or Mobile data). It still returns true. Please give me any suggestions.
Versions:-
react-native:- 0.61.4,
@react-native-community/netinfo:- 4.6.1
Here is my code:-
componentDidMount() {
NetInfo.isConnected.addEventListener(
'connectionChange',
this.handleConnectivityChange,
);
}
handleConnectivityChange = isConnected => {
console.log('uyeuiyiuyiu', isConnected);
this.setState({
connectionStatus: isConnected,
});
};
componentWillUnmount() {
NetInfo.isConnected.removeEventListener(
'connectionChange',
this.handleConnectivityChange,
);
}
Upvotes: 4
Views: 4689
Reputation: 538
1- Clean gradlew, run command in your android folder:
2- Import and use this component:
import React, {Component} from 'react';
import {StyleSheet, Text, View} from 'react-native';
import NetInfo from '@ react-native-community / netinfo';
class ConnectionInfo extends Component {
NetInfoSubcription = null;
constructor (props) {
super (props);
this.state = {
connection_status: false,
connection_type: null,
connection_net_reachable: false,
connection_wifi_enabled: false,
connection_details: null,
}
}
componentDidMount () {
this.NetInfoSubscribtion = NetInfo.addEventListener (
this._handleConnectivityChange,
);
}
componentWillUnmount () {
this.NetInfoSubscribtion && this.NetInfoSubscribtion ();
}
_handleConnectivityChange = (state) => {
this.setState ({
connection_status: state.isConnected,
connection_type: state.type,
connection_net_reachable: state.isInternetReachable,
connection_wifi_enabled: state.isWifiEnabled,
connection_details: state.details,
})
}
render () {
return (
<View>
<Text>
Connection Status: {this.state.connection_status? 'Connected': 'Disconnected'}
</Text>
<Text>
Connection Type: {this.state.connection_type}
</Text>
<Text>
Internet Reachable: {this.state.connection_net_reachable? 'YES': 'NO'}
</Text>
<Text>
Wifi Enabled: {this.state.connection_wifi_enabled? 'YES': 'NO'}
</Text>
<Text>
Connection Details: {'\ n'}
{this.state.connection_type == 'wifi'?
(this.state.connection_details.isConnectionExpensive? 'Connection Expensive: YES': 'Connection Expensive: NO')
+ '\ n'
+ 'ssid:' + this.state.connection_details.ssid
+ '\ n'
+ 'bssid:' + this.state.connection_details.bssid
+ '\ n'
+ 'strength:' + this.state.connection_details.strength
+ '\ n'
+ 'ipAddress:' + this.state.connection_details.ipAddress
+ '\ n'
+ 'subnet:' + this.state.connection_details.subnet
+ '\ n'
+ 'frequency:' + this.state.connection_details.frequency
:
this.state.connection_type == 'cellular'?
(this.state.connection_details.isConnectionExpensive? 'Connection Expensive: YES': 'Connection Expensive: NO')
+ '\ n'
+ 'cellularGeneration:' + this.state.connection_details.cellularGeneration
+ '\ n'
+ 'carrier:' + this.state.connection_details.carrier
:
'---'
}
</Text>
</View>
);
}
}
export default ConnectionInfo;
3- Reconstruct the project from Android Studio from:
Android Studio> File> Invalidate cache and restart
4- Put the airplane mode.
Upvotes: 0
Reputation: 3610
you use the new library, but the API you use is Deprecated. you can see this in this link.
for android, you have to add the internet permission at the android native AndroidMainFest.xml
// the app can link the network
<uses-permission
android:name="android.permission.INTERNET" />
// get the network state
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
// get the wifi state
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE" />
if you want to know the network is connected, you can use the following code
NetInfo.fetch().then(state => {
if(state. isConnected){
}
});
if you want to know when user change the network, whether it is connected, you can use the following code
NetInfo.addEventListener(state => {
if(state. isConnected){
}
});
Upvotes: 2