Skip to content
An Android library to assist with connectivity of your app.
Kotlin Java
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
app
connectivityhelper
gradle/wrapper
.gitignore
LICENSE
README.md
build.gradle
gradlew
gradlew.bat
settings.gradle

README.md

ConnectivityHelper

Functions to help with checking your app's online status

CircleCI Download

Installation

implementation 'io.flatcircle:connectivityhelper:{version}'

Usage

Function Description Example
NetUtil.isProbablyOnline(context) Does a basic check to see if the device has a connected network of some kind Example
NetUtil.ping(endpoint) Pings a given endpoint Example
NetUtil.getWifiInfo(context) Provides all wifi info, but requires location permission Example
NetUtil.getWifiInfoPartial(context) Provides all wifi info it can, but will omit ssid/bssid without location permission on Android 27+ Example

ConnectionMonitor

This library also comes with a ConnectionMonitor, which can be used to get active notifications about your app's connectivity status.

The ConnectionMonitor must be coupled with your Activity/Application's lifecycle like so:

class MainActivity : AppCompatActivity(), StateChangeListener {

    lateinit var netMonitor: ConnectionMonitor

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        netMonitor = ConnectionMonitor.Builder(applicationContext) // any context will do
                    .watchedConnections(ConnectionType.Cellular, ConnectionType.WiFi) // list of connections that will be watched by the monitor
                    .stateChangeListener(this) // optional interface for handling connectivity state changes
                    .timeBetweenPings(10000) // milliseconds between each ping to check online status
                    .endpoint("8.8.8.8") // endpoint for pinging against. Default is Google, but that's unreliable in some countries.
                    .build()

        val isConnectedToWifi = netMonitor.isConnectedTo(ConnectionType.WiFi) // Determines if the app is currently connected to the given network type
        val isConnectedToCellular = netMonitor.isConnectedTo(ConnectionType.Cellular) // Determines if the app is currently connected to the given network type

        val connectionState = netMonitor.connectionState // the current connectionState, which can be Offline, ProbablyOnline, or Online

        when (connectionState) {
            ConnectionState.Offline -> {} // The app is not connected to any network with internet capability
            ConnectionState.ProbablyOnline -> {} // The app is connected to a network which claims to have internet
            ConnectionState.Online -> {} // The app is able to successfully resolve a call to the endpoint, at least within the last [timeBetweenPings] milliseconds
        }
    }

    override fun stateChange(state: ConnectionState) {
        // function from StateChangeListener, which receives all state changes
    }

    override fun onDestroy() {
        netMonitor.clear() // netMonitor must be cleared for garbage collection
        super.onDestroy()
    }
You can’t perform that action at this time.