|
| 1 | + |
| 2 | +# P2P Bluetooth LE Synchronization Documentation |
| 3 | + |
| 4 | +## 1. Overview |
| 5 | + |
| 6 | +This document provides a detailed explanation of the P2P synchronization feature using Bluetooth Low Energy (BLE). The feature allows Couchbase Lite enabled devices to discover each other and synchronize data directly without the need for a central server. |
| 7 | + |
| 8 | +## 2. Architecture |
| 9 | + |
| 10 | +The P2P synchronization feature is built on a hybrid architecture that leverages both Java and C++ code. |
| 11 | + |
| 12 | +* **Java Layer**: The Java layer is responsible for all platform-specific Bluetooth LE operations. This includes scanning for nearby devices, advertising the device's presence, and managing Bluetooth connections. By delegating these tasks to the Java layer, we ensure that the application can take advantage of the latest Android Bluetooth APIs and best practices. |
| 13 | + |
| 14 | +* **C++ Layer**: The C++ layer contains the core logic for peer discovery and data synchronization. It is responsible for managing the state of discovered peers, handling metadata exchange, and orchestrating the overall synchronization process. The C++ layer communicates with the Java layer through the Java Native Interface (JNI). |
| 15 | + |
| 16 | +This separation of concerns allows for a clean and maintainable codebase. The C++ layer remains platform-agnostic, while the Java layer handles the intricacies of the Android Bluetooth stack. |
| 17 | + |
| 18 | +## 3. C++ Class Descriptions |
| 19 | + |
| 20 | +### 3.1. `C4BLEProvider` |
| 21 | + |
| 22 | +The `C4BLEProvider` class is the heart of the peer discovery process. It is a C++ class that runs in the native layer and is responsible for the following: |
| 23 | + |
| 24 | +* **Starting and Stopping Peer Discovery**: The `C4BLEProvider` can be instructed to start or stop scanning for nearby peers. When scanning is active, the provider will notify the application of any discovered peers. |
| 25 | +* **Publishing the Device's Presence**: The `C4BLEProvider` can also be used to advertise the device's presence to other peers. This allows other devices to discover and connect to it. |
| 26 | +* **Managing Discovered Peers**: The `C4BLEProvider` maintains a list of all discovered peers and their current status (online or offline). |
| 27 | +* **Forwarding Calls to the Java Layer**: The `C4BLEProvider` uses JNI to forward calls to the Java layer for all Bluetooth-specific operations. |
| 28 | + |
| 29 | +### 3.2. `C4Peer` |
| 30 | + |
| 31 | +The `C4Peer` class represents a discovered peer in the network. It is a C++ class that contains the following information about a peer: |
| 32 | + |
| 33 | +* **Peer ID**: A unique identifier for the peer. |
| 34 | +* **Online Status**: A boolean flag that indicates whether the peer is currently online and reachable. |
| 35 | +* **Metadata**: A collection of key-value pairs that can be used to store additional information about the peer. |
| 36 | + |
| 37 | +The `C4Peer` class provides methods for getting and setting the peer's metadata, as well as for monitoring changes to the metadata. |
| 38 | + |
| 39 | +### 3.3. `MetadataHelper` |
| 40 | + |
| 41 | +The `MetadataHelper` is a C++ utility class that provides functions for converting between Java `Map` objects and the `C4Peer::Metadata` type. This is necessary because the metadata is exchanged between the Java and C++ layers as part of the peer discovery and synchronization process. |
| 42 | + |
| 43 | +## 4. Java Class Descriptions |
| 44 | + |
| 45 | +### 4.1. `BluetoothProvider` |
| 46 | + |
| 47 | +The `BluetoothProvider` class is the main entry point for the Java-side of the P2P implementation. It acts as a bridge between the native C++ code and the Java-based `BleService`. It is responsible for: |
| 48 | + |
| 49 | +* Creating and managing `BleService` instances. |
| 50 | +* Forwarding calls from the native layer to the `BleService` for starting/stopping browsing and publishing. |
| 51 | +* Handling callbacks from the `BleService` and forwarding them to the native layer. |
| 52 | + |
| 53 | +### 4.2. `BluetoothPeer` |
| 54 | + |
| 55 | +The `BluetoothPeer` class is the Java representation of a discovered peer. It mirrors the C++ `C4Peer` class and provides a high-level API for interacting with a peer. It is responsible for: |
| 56 | + |
| 57 | +* Storing the peer's ID, online status, and metadata. |
| 58 | +* Providing methods for monitoring metadata changes and resolving the peer's URL. |
| 59 | + |
| 60 | +### 4.3. `BleService` |
| 61 | + |
| 62 | +The `BleService` class is the core of the Java-side implementation. It manages all Bluetooth LE operations, including: |
| 63 | + |
| 64 | +* **Scanning**: It starts and stops scanning for nearby devices that are advertising the Couchbase Lite P2P service. |
| 65 | +* **Device Management**: It maintains a list of discovered devices (`CblBleDevice`) and manages their connection state. |
| 66 | +* **Publishing**: It coordinates with the `BlePublisher` to advertise the device's presence. |
| 67 | +* **L2CAP Connections**: It initiates L2CAP connections to discovered peers. |
| 68 | + |
| 69 | +### 4.4. `BlePublisher` |
| 70 | + |
| 71 | +The `BlePublisher` class is responsible for advertising the device's presence to other peers. It handles the complexities of BLE advertising, including: |
| 72 | + |
| 73 | +* Starting and stopping advertising with the appropriate settings. |
| 74 | +* Managing the advertising lifecycle and handling failures. |
| 75 | +* Working in conjunction with the `BleGattServer` to host the Couchbase Lite service. |
| 76 | + |
| 77 | +### 4.5. `CblBleDevice` |
| 78 | + |
| 79 | +The `CblBleDevice` class represents a remote Bluetooth LE device that has been discovered by the `BleService`. It is responsible for: |
| 80 | + |
| 81 | +* Managing the GATT connection to the remote device. |
| 82 | +* Discovering the Couchbase Lite P2P service and its characteristics. |
| 83 | +* Reading the peer's ID, port, and metadata from the GATT characteristics. |
| 84 | +* Initiating an L2CAP connection to the remote device. |
| 85 | + |
| 86 | +### 4.6. `BleGattServer` |
| 87 | + |
| 88 | +The `BleGattServer` class is responsible for creating and managing the GATT server that hosts the Couchbase Lite P2P service. Its key responsibilities include: |
| 89 | + |
| 90 | +* Creating the GATT service with the required characteristics (ID, port, metadata). |
| 91 | +* Handling read requests for the characteristics from remote devices. |
| 92 | +* Optionally starting an L2CAP server to listen for incoming connections. |
| 93 | + |
| 94 | +### 4.7. `BleL2CAPConnection` |
| 95 | + |
| 96 | +The `BleL2CAPConnection` class is a wrapper around a BluetoothSocket that provides a simple interface for sending and receiving data over an L2CAP connection. It handles: |
| 97 | + |
| 98 | +* Reading data from the socket in a background thread. |
| 99 | +* Writing data to the socket asynchronously. |
| 100 | +* Notifying a listener of incoming data and connection closure. |
| 101 | + |
| 102 | +### 4.8. `BleP2pConstants` |
| 103 | + |
| 104 | +This class defines the UUIDs for the Couchbase Lite P2P service and its characteristics, as well as other constants used by the BLE implementation. |
| 105 | + |
| 106 | +### 4.9. Listeners and Handles |
| 107 | + |
| 108 | +* **`BlePublisherListener`**: An interface for receiving lifecycle events from the `BlePublisher`. |
| 109 | +* **`BlePublisherHandle`**: A handle that allows for stopping an active advertising session. |
| 110 | +* **`BleGattInboundListener`**: An interface for receiving events related to inbound L2CAP connections. |
| 111 | + |
| 112 | +## 5. Flows |
| 113 | + |
| 114 | +### 5.1. Peer Discovery |
| 115 | + |
| 116 | +The peer discovery process is initiated by the application, which calls the `startBrowsing()` method on the `C4BLEProvider` object. The `C4BLEProvider` then forwards this call to the Java layer, which begins scanning for nearby Bluetooth LE devices. |
| 117 | + |
| 118 | +When a new device is discovered, the Java layer notifies the `C4BLEProvider`, which creates a new `C4Peer` object to represent the device. The `C4BLEProvider` then adds the new `C4Peer` to its list of discovered peers and notifies the application. |
| 119 | + |
| 120 | +### 5.2. Publishing |
| 121 | + |
| 122 | +To make itself discoverable to other devices, the application calls the `startPublishing()` method on the `C4BLEProvider` object. The `C4BLEProvider` then forwards this call to the Java layer, which begins advertising the device's presence. |
| 123 | + |
| 124 | +The advertisement packet contains a service UUID that is specific to the application, as well as the device's display name and other relevant information. |
| 125 | + |
| 126 | +### 5.3. Metadata Exchange |
| 127 | + |
| 128 | +The P2P synchronization feature allows for the exchange of metadata between peers. This metadata can be used to store any application-specific information, such as the user's name or the device's capabilities. |
| 129 | + |
| 130 | +The metadata is exchanged as part of the peer discovery process. When a device discovers a new peer, it can request the peer's metadata. The metadata is then sent over the Bluetooth connection and stored in the `C4Peer` object. |
| 131 | + |
| 132 | +The application can also monitor changes to a peer's metadata. When the metadata changes, the `C4BLEProvider` will notify the application, which can then take appropriate action. |
0 commit comments