Unity NearbyConnections Google Play Services Implementation

# How can I use NearbyConnections (as provided by Google Play Services) in conjunction with Unity to send data between players?

## Summary
This postmortem documents the challenges and solutions faced while implementing Google Play Services' NearbyConnections API in a Unity project to enable peer-to-peer data sharing between Android devices for a "Streetpass"-style prototype. Despite initial optimism, outdated plugins, incomplete documentation, and platform-specific limitations led to significant delays. Lessons learned include stricter code reviews, prioritization of cross-team communication, and proactive third-party research.

---

## Root Cause
The core issue stemmed from **outdated/unmaintained Unity plugins** for NearbyConnections and conflicting implementation approaches between Android's native API and Unity's abstraction layer. Additionally, **ambiguous documentation** for Google Play Services services (e.g., NearbyMessages API versioning) caused confusion about required dependencies and setup steps. Team communication gaps further compounded delays as members pursued divergent solutions without centralized coordination.

---

## Why This Happens in Real Systems
- **Third-Party Dependency Rot**: Android's NearbyConnections API evolved during development, breaking compatibility with existing Unity plugins.  
- **Overreliance on Documentation**: Insufficient examples for production use cases (e.g., handling disconnections, data serialization).  
- **Toolchain Inflexibility**: Unity's older versions lacked native support for newer Kotlin/Java integration patterns.  
- **Team Knowledge Gaps**: Lack of Android-specific expertise among developers slowed debugging.

---

## Real-World Impact
- **Prototyping Delays**: 14+ hours spent debugging connection timeouts and packet fragmentation.  
- **Project Scope Reduction**: Simplified networking model shifted from multiplayer quest systems to pre-built Streetpass-style exchanges.  
- **Maintenance Debt**: Hardcoded IP whitelisting and platform checks complicate future expansions.

---

## Example or Code
```C#
// Unity C# script using Google Play Services Android Native Plugin (hypothetical example)
using GooglePlayServices.NearbyConnections;

public class StreetpassManager : MonoBehaviour
{
    private NearbyConnectionsCallback callback;
    private NearbyConnections nearByConnections;

    void Start()
    {
        var options = new NearbyConnectionsOptions();
        options.ServiceRequest = NearbyConnectionsService.NearbyConnections;

        nearByConnections.Connect(options, callback);
    }

    void Update()
    {
        nearByConnections.SetMessageListener(new ServerMessageCallback("quest_data"));
    }
}

Note: Actual implementation required bridging to Android’s NearbyMessagesApi via JNI/callbacks.


How Senior Engineers Fix It

  • Implement Modular Wrapper: Build a cross-platform networking manager decoupled from platform-specific code.
  • Enforce Code Reviews: Validate third-party plugin compatibility via unit tests.
  • Adopt CI/CD: Automate testing across device types to catch fragmentation issues early.
  • Use Protocol Buffers: Optimize data serialization for NearbyConnections’ bandwidth constraints.

Why Juniors Miss It

  • Assumed Unity plugins would handle low-level complexity, overlooking fragment lifecycle management.
  • Misinterpreted deprecated API guides as authoritative references.
  • Skipped logcat debugging due to unfamiliarity with Android thread graphs.
  • Forgot to handle device discovery timeouts as “infinite retry loops” seemed logically correct.

Leave a Comment