Introducing a new way to review your PRs.

Read the announcement

capcom6 / android-sms-gateway

The SMS Gateway for Android™ app enables sending and receiving SMS messages through an API that can be accessed directly on the device or via a cloud server when direct device access is not possible.

View on GitHub

20 documents · Updated Nov 20, 2026

docs / connection-and-device-health

Connection and device health

Connection and device health give the gateway a current view of whether the phone can reach the Internet and whether it can keep serving as an SMS gateway. The system combines network signals, local and public address data, battery state, and message checks into health reports that support the home screen, the local health endpoint, cloud polling, and health webhooks. See also Cloud gateway integration and Gateway command workers.

Connection signals

Internet and transport signals

The connection service turns Android network changes into two related signals: Internet availability and network type. Internet is considered available when the active network reports Internet access and, on supported Android versions, passes system validation. The service exposes this state for the home screen and records changes in the app log.

The transport signal describes how the phone reaches the network. It can report Wi-Fi, Ethernet, or cellular transport. When cellular transport is active, a second signal groups the radio into 2G, 3G, 4G, or 5G. Missing network data produces an unknown or empty result instead of claiming a usable path.

References: app/…/ConnectionService.kt• L21-67 app/…/ConnectionService.kt• L110-179 app/…/TransportType.kt• L3-10 app/…/CellularNetworkType.kt• L3-10

Address discovery

Address discovery supplies the local address that can identify the phone on its current network and the public address seen by the gateway. The local provider first uses Wi-Fi information, then checks network interfaces when that data is not available. The public provider asks the configured gateway for the device’s external address.

The local server starts this work in the background and emits both results together. The home screen then shows a copyable local or public address when one is available. A missing result is shown as “not found”; it does not make the other address unavailable.

References: app/…/IPProvider.kt• L3-5 app/…/LocalIPProvider.kt• L9-53 app/…/PublicIPProvider.kt• L6-16 app/…/LocalServerService.kt• L31-43 app/…/IPReceivedEvent.kt• L5-11 app/…/HomeFragment.kt• L232-278

Health model

Health aggregation

The health service creates one result from message, connection, and battery checks. Each check has a status, an observed value, a unit, and a description. The overall result is FAIL when any check fails, WARN when no check fails but one warns, and PASS when all checks pass.

The same health shape is wrapped with the app version and release ID for transport outside the health module. The local server exposes it at GET /health, using an HTTP error only for an overall failure. A warning remains a successful HTTP response so callers can inspect the details.

References: app/…/HealthService.kt• L9-32 app/…/HealthResult.kt• L3-6 app/…/CheckResult.kt• L3-8 app/…/Status.kt• L5-13 app/…/HealthResponse.kt• L8-15 app/…/WebService.kt• L139-150

Battery state

Battery monitoring adds the phone’s charge level and charging state to each health result. A level below 25 percent warns that the gateway may have less time to remain available. A level below 10 percent fails the battery check. Charging is reported separately with flags for charging, USB power, and AC power.

The settings screen also offers a request to exclude the app from Android battery optimization. This can help long-running status and gateway work continue, but the setting warns that it can affect battery life. The option is unavailable on Android versions that do not support it.

References: app/…/BatteryMonitor.kt• L10-61 app/…/SettingsFragment.kt• L72-109 app/…/root_preferences.xml• L61-68

Ping schedule

Health report delivery

Ping is an optional periodic health report. A positive interval enables the service, and the service then runs as a foreground task so Android is less likely to stop it while the phone is idle. Each cycle takes a fresh health snapshot and publishes a PingEvent on the app event bus.

The event drives two gateway-facing outcomes:

  1. The gateway event receiver starts a message pull. This lets a reachable phone check for cloud work on the same cadence as its health report.
  2. The webhook event receiver emits the system:ping webhook with the full health response. External systems can use the status and check values to track the device.

The service uses wake and Wi-Fi locks while it runs. This supports timely reports but increases the cost of enabling a short interval, which is why the setting is described as online status at the cost of battery life.

The following flow shows how one scheduled ping reaches the local and gateway-facing consumers.

flowchart LR
    S[Positive ping interval] --> P[Periodic health snapshot]
    P --> H[HealthService]
    H --> R[HealthResponse]
    R --> E[PingEvent]
    E --> G[Gateway event receiver]
    G --> W[Message pull work]
    E --> B[Webhook event receiver]
    B --> K[system:ping webhook]
    H --> L[Local GET /health]

References: app/…/PingSettings.kt• L8-20 app/…/PingService.kt• L6-16 app/…/PingForegroundService.kt• L27-74 app/…/PingEvent.kt• L6-11 app/…/EventsReceiver.kt• L54-63 app/…/EventsReceiver.kt• L17-29 app/…/root_preferences.xml• L40-47

Gateway experience

Home connection status

The home screen presents Internet availability as a direct status for the remote-server path. It shows the status only when remote use is selected, so local-server users are not given a cloud connection message that does not apply to their mode.

Service lifecycle

The app orchestrator starts the gateway, local server, and optional ping service as part of the app’s running state. If Android prevents a foreground service from starting in the background, the orchestrator logs a warning instead of treating the whole app start as successful. This matters because the gateway can be configured while the periodic health path is not active.

References: app/…/HomeFragment.kt• L296-305 app/…/OrchestratorService.kt• L29-59 app/…/OrchestratorService.kt• L69-79

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the check names and units stable across HealthService, HealthResponse, the local /health response, and the system:ping webhook. Consumers may use names such as connection:status and battery:level as keys.
  • Keep ping interval validation aligned between the settings UI, imported settings, and foreground service startup. A non-positive or missing interval disables periodic reports.
  • If the PingEvent shape changes, update both gateway event handling and webhook event handling. They use the same event for different downstream actions.
  • If address lookup changes, preserve the paired local/public result event and the home screen’s separate missing-address states.
  • If connection status semantics change, review the remote-only home status and the connection health check together.

Important failure modes

TriggerConsequenceGuard
Android reports no validated Internet networkConnection health fails on supported Android versions and the remote status shows unavailableKeep the connection callback active and inspect transport and health details separately
Local interface lookup finds no usable IPv4 addressThe local address is shown as not found even if the server can startTreat the local address as display or reachability information, not as proof that the local server is stopped
Gateway lookup cannot return the external addressThe public address is shown as not foundKeep the public lookup failure independent from local address display
Battery level falls below 25 percent or 10 percentThe aggregate health becomes warning or failureUse charging state and the battery optimization setting when a long-running gateway is needed
Ping interval is missing, invalid, or non-positiveThe ping service does not start or has no work intervalValidate the stored interval before enabling the service
Foreground service start is blocked in the backgroundLocal server and ping startup can be skipped while other app services continueCheck the app log and bring the app to the foreground before relying on periodic reports

Key files

FileWhy to read it
app/…/ConnectionService.ktDefines Internet state, transport type, cellular grouping, and connection health checks.
app/…/HealthService.ktShows how device, connection, and message checks become one health result.
app/…/BatteryMonitor.ktDefines battery thresholds and charging values used in health reports.
app/…/PingForegroundService.ktShows the periodic health report lifecycle and its battery trade-off.
app/…/EventsReceiver.ktShows how a ping prompts cloud message polling.
app/…/EventsReceiver.ktShows how a ping becomes the system:ping webhook.
app/…/WebService.ktShows the local health endpoint and its HTTP status mapping.
app/…/LocalServerService.ktShows when local and public address discovery runs and how results reach the UI.
app/…/HomeFragment.ktShows how the home screen presents remote connection and address state.