DEV Community

Cover image for Building a parenting app with UDP
Slavik
Slavik

Posted on

Building a parenting app with UDP

The Problem That Started It All

When I needed a screen time limiting app for my child's Fire HD device, Amazon's built-in parental controls just didn't cut it for my specific use case. Like many developers faced with a problem, I decided to build my own solution. What started as a simple hardcoded timer has evolved into something much more interesting - a real-time parent control system using UDP communication over the local network.

From Simple Beginnings

The first version was beautifully simple: a hardcoded time limit with two buttons for the child to request extensions - one minute or five minutes. I implemented this because, let's be honest, kids always ask for "just a little bit more" time at the end!

The app reliably did what it needed to do: block the tablet with an overlay activity once the time credit was reached. Simple, effective, and it worked.

The Evolution: Adding Remote Parent Control

When I decided to open-source the app, I started polishing it and implementing features I'd always thought about but never had the time (or motivation) to build. The most interesting addition? A companion parent app that can communicate with the child's device over the local network.

Initially, I had implemented admin settings directly in the child app, but this approach had serious drawbacks:

  • Cumbersome to use: Parents had to physically access the child's device
  • Disruptive experience: It interrupted the child's interaction with the device
  • Impractical: Imagine trying to check time limits or grant extensions from across the room!

Enter UDP: The Perfect Protocol for Local Network Communication

This is where UDP (User Datagram Protocol) comes into play. But first, let's understand what UDP actually is:

What is UDP?

UDP is a communication protocol that's like sending postcards between devices:

  • Fast and lightweight: No handshaking or connection establishment needed
  • Broadcast capable: One device can send messages to all devices on the network
  • Simple: Perfect for local network communication where speed matters more than guaranteed delivery
  • Fire-and-forget: Send a message and move on - ideal for real-time controls

Unlike TCP (which is like a phone call - establishing a connection and ensuring every word is heard), UDP is more like shouting across a room - quick, direct, and perfect for commands like "check time remaining" or "extend time by 5 minutes."

Why UDP is Perfect for Parent-Child Device Control

  1. Discovery: The parent app can broadcast "Is there a child device here?" and any child devices respond
  2. Real-time commands: Instant blocking, time extensions, or status checks
  3. No complex setup: No pairing, no account creation - just connect to the same WiFi
  4. Local network only: Commands stay within your home network for privacy

The Technical Implementation

Here's how the system works:

1. Device Discovery

Parent App → Broadcasts: "CST_PARENT_DISCOVERY" on port 8888
Child Device → Responds: "CST_CHILD_RESPONSE" with device info
Enter fullscreen mode Exit fullscreen mode

2. Secure Command Communication

All commands are encrypted using AES-CBC with device ID-based keys:

Parent App → Sends: "CST_CMD:[encrypted_command]"
Child Device → Responds: "CST_RESP:[encrypted_response]"
Enter fullscreen mode Exit fullscreen mode

3. Available Commands

  • GET_TIME_LEFT: Check remaining screen time
  • LOCK_DEVICE: Immediately block the device
  • EXTEND_TIME:30: Add 30 minutes to the time limit

4. Real-World Usage Flow

  1. Parent opens the companion app on their phone/tablet
  2. App automatically discovers child devices on the network
  3. Parent can see real-time status: time remaining, current state (active/blocked)
  4. Instant control: Block immediately or grant time extensions
  5. No disruption to the child's experience

The Security Layer

Since this involves controlling a child's device, security is paramount:

  • AES-CBC Encryption: All commands are encrypted using device-specific keys
  • Local network only: Commands never leave your home WiFi
  • Device ID-based keys: Each device has unique encryption keys
  • No internet required: Everything works offline

Code Deep Dive: The Discovery Service

Here's a simplified version of how the child device listens for parent commands:

public class ParentDiscoveryService extends Service {
    private static final int DISCOVERY_PORT = 8888;
    private static final String DISCOVERY_REQUEST = "CST_PARENT_DISCOVERY";
    private static final String DISCOVERY_RESPONSE = "CST_CHILD_RESPONSE";

    private void handleIncomingMessage(String message, InetAddress sender, int port) {
        if (DISCOVERY_REQUEST.equals(message)) {
            // Respond to discovery
            sendResponse(DISCOVERY_RESPONSE, sender, port);
        } else if (message.startsWith("CST_CMD:")) {
            // Handle encrypted command
            String encrypted = message.substring(8);
            String decrypted = securityManager.decryptMessage(encrypted);
            String response = processCommand(decrypted);
            // Send encrypted response back
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Lessons Learned

Building this system taught me several valuable lessons:

  1. Simple protocols work best: UDP's simplicity made implementation straightforward
  2. Security can't be an afterthought: Encryption was essential for a family app
  3. User experience drives architecture: The need for non-disruptive parent control shaped the entire design
  4. Local-first is powerful: No internet dependency makes the system more reliable

The Technical Challenges

Of course, it wasn't all smooth sailing:

Challenge 1: Service Persistence

Android aggressively kills background services. The solution? Multiple layers of protection:

  • Foreground services with proper notification channels
  • AlarmManager for restart scheduling
  • WorkManager for periodic health checks

Challenge 2: Network Discovery

UDP broadcast doesn't always work reliably across all WiFi configurations. The solution:

  • Retry logic with exponential backoff
  • Multiple discovery attempts
  • Graceful degradation

Challenge 3: Encryption Key Management

Securely sharing encryption keys between devices without user complexity:

  • Device ID-based key derivation
  • SHA-256 hashing for consistent keys
  • No manual key exchange required

What's Next?

The UDP communication system opens up exciting possibilities:

  • Multi-device management: Control multiple child devices from one parent app
  • Usage analytics: Real-time monitoring and historical data
  • Family coordination: Multiple parent devices controlling the same child device

Try It Yourself

The project is open source and available on GitHub. The UDP communication system demonstrates how simple protocols can solve complex real-world problems elegantly.

Key Takeaways

  1. Sometimes simple is better: UDP's simplicity made it perfect for this use case
  2. Local network communication is underutilized: Many problems can be solved without cloud services
  3. User experience should drive technical decisions: The need for non-disruptive control shaped the entire architecture
  4. Security matters in family apps: Encryption is essential, even for local communication
  5. Android background services require careful handling: Multiple protection layers are necessary

Have you built similar local network communication systems? What protocols and patterns have worked well for you? Share your experiences in the comments!

Technical Resources

Top comments (0)