forked from google/nearby
-
Notifications
You must be signed in to change notification settings - Fork 1
Discovery and Advertising
kidfromjupiter edited this page Jan 23, 2026
·
1 revision
This page explains how advertising and discovery are implemented and wired into the Core API.
API entry points (Core)
-
Core::StartAdvertising(...)/Core::StopAdvertising(...) -
Core::StartDiscovery(...)/Core::StopDiscovery(...) - V3 variants:
StartAdvertisingV3,StartDiscoveryV3which accept richerv3::AdvertisingOptionsandv3::DiscoveryOptionstypes.
Routing and responsibilities
- Core delegates to
ServiceControllerRouter(connections/implementation/service_controller_router.*). - Router serializes requests on a single-threaded executor and forwards them to a
ServiceControllerimplementation (oftenOfflineServiceController). - The ServiceController starts/stops PCP handlers which manage medium lifecycle (advertising/scanning/accept loops) and forward endpoint events to EndpointManager.
Advertising / Discovery options
-
AdvertisingOptionsandDiscoveryOptions(and their V3 counterparts) control:- Strategy (how endpoints are grouped/connected)
- Enabled/allowed mediums (bluetooth, ble, web_rtc, wifi_lan, wifi_hotspot, wifi_direct, awdl)
- auto_upgrade_bandwidth
- power level (low/high)
- enable_bluetooth_listening, enable_webrtc_listening
- use_stable_endpoint_id / force_new_endpoint_id
- fast_advertisement_service_uuid for BLE fast/extended
V3 -> V1 mapping
-
StartAdvertisingV3andStartDiscoveryV3convert V3 options into the legacyAdvertisingOptions/DiscoveryOptionsthen call the old StartAdvertising/StartDiscovery paths. SeeCore::StartAdvertisingV3andCore::StartDiscoveryV3inconnections/core.cc.
Out-of-band discovery and injection
-
Core::InjectEndpoint(...)can be used to inject an endpoint into discovery whenis_out_of_band_connection == true.- Meaning: the endpoint was discovered by an out-of-band mechanism (not by scanning medium advertisements) and is being injected into the discovery callback path using externally-provided metadata (
OutOfBandConnectionMetadata). Common out-of-band sources include NFC taps, QR codes, cloud pairing, or manual entry of a Bluetooth MAC or service credentials. - Usage: callers set
is_out_of_band_connectionin DiscoveryOptions/ConnectionOptions when they expect to inject endpoints rather than rely on medium scanning.Core::InjectEndpoint(service_id, metadata, callback)delivers the discovery callback (DiscoveryListener::endpoint_found_cb) for the provided metadata so the app can act as if the endpoint was discovered normally.
- Meaning: the endpoint was discovered by an out-of-band mechanism (not by scanning medium advertisements) and is being injected into the discovery callback path using externally-provided metadata (
- The router currently limits injection to Bluetooth in
ServiceControllerRouter::InjectEndpoint(it validates thatmetadata.medium == Medium::BLUETOOTHand thatremote_bluetooth_mac_addresshas length 6). The router also checks thatmetadata.endpoint_idandmetadata.endpoint_infoare well-formed and that the client is actively discovering (client->IsDiscovering()); on success it callsGetServiceController()->InjectEndpoint(...).
-
BLE: advertising (legacy/extended) and scanning via platform BLE APIs.
- Note: Android advertises BLE packets using resolvable LE random addresses (one-per-advertisement) which allows advertising both Fast and Extended advertisements concurrently with different random addresses. Linux does not generally support per-advertisement LE random addresses and typically advertises from the same adapter MAC; registering both legacy (Fast) and extended adverts on the same MAC causes them to be interleaved intermittently. This can cause other devices to miss one or both adverts if their scanners assume a stable advertising address, so the Linux implementation prefers BLE Extended advertising instead of running both adverts on the same MAC.
-
Wi‑Fi LAN: uses mDNS / NsdServiceInfo to advertise and discover services.
-
Other mediums have their own discovery/advertising primitives (see
Mediums.md).
References in code
-
connections/core.h,connections/core.cc connections/implementation/service_controller_router.*-
connections/implementation/mediums/*(per-medium advertising & scanning code)