This document describes the BLEService component, which implements the Bluetooth Low Energy mesh transport layer for BitChat. BLEService provides local, offline peer-to-peer communication through a self-organizing mesh network that operates without internet connectivity or central infrastructure.
This page covers:
BLEFragmentAssemblyBuffer.Sources: bitchat/Services/BLE/BLEService.swift10-14 bitchat/Services/BLE/BLEService.swift152-162
BLEService operates as both a BLE Central (scanner/connector) and BLE Peripheral (advertiser), enabling bidirectional mesh connectivity. The service manages discovery, connection lifecycle, and packet routing in a unified component.
The following diagram maps the high-level system responsibilities to the specific code entities within BLEService.swift.
Title: BLEService Architecture and Code Entities
Key Design Principles:
BLELinkStateStore with strict queue ownership enforcement to prevent race conditions bitchat/Services/BLE/BLEService.swift38-39MessageDeduplicator bitchat/Services/BLE/BLEService.swift60-61RelayController (referenced via logic flow) to dynamically adjust packet TTL based on network density (node degree) bitchat/Services/TransportConfig.swift10-18Sources: bitchat/Services/BLE/BLEService.swift13-110 bitchat/Services/TransportConfig.swift5-18
BLEService uses distinct UUIDs for test and production networks to prevent cross-contamination:
| Constant | Test UUID | Production UUID | Purpose |
|---|---|---|---|
serviceUUID | F47B5E2D-4A9E-4C5A-9B3F-8E1D2C3A4B5A | F47B5E2D-4A9E-4C5A-9B3F-8E1D2C3A4B5C | Service advertisement bitchat/Services/BLE/BLEService.swift18-21 |
characteristicUUID | A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D | Same | Message transfer bitchat/Services/BLE/BLEService.swift22 |
centralRestorationID | chat.bitchat.ble.central | Same | iOS state restoration bitchat/Services/BLE/BLEService.swift23 |
peripheralRestorationID | chat.bitchat.ble.peripheral | Same | iOS state restoration bitchat/Services/BLE/BLEService.swift24 |
Configuration Parameters:
TransportConfig.bleDefaultFragmentSize (469 bytes) bitchat/Services/TransportConfig.swift7TransportConfig.messageTTLDefault (7) bitchat/Services/TransportConfig.swift8Sources: bitchat/Services/BLE/BLEService.swift18-34 bitchat/Services/TransportConfig.swift7-10
To maintain a reliable mesh, BitChat uses a sophisticated link store and fanout selector to manage concurrent BLE links.
The BLELinkStateStore (referenced as linkStateStore) centralizes all BLE link state, including peripheral connections (we-as-central) and central subscriptions (they-as-central) bitchat/Services/BLE/BLEService.swift38
noiseAuthenticatedLinkOwners to ensure a session is bound to a specific physical ingress link bitchat/Services/BLE/BLEService.swift44The BLERecentPeripheralCache remembers recently seen bitchat peripherals to arm background connections bitchat/Services/BLE/BLERecentPeripheralCache.swift3-7
limit bitchat/Services/BLE/BLERecentPeripheralCache.swift39-43Title: Discovery and Connection Flow
Sources: bitchat/Services/BLE/BLEService.swift38-52 bitchat/Services/BLE/BLERecentPeripheralCache.swift3-53
BitChat uses a "flood-fill" gossip protocol with intelligent suppression to prevent network saturation.
The service uses RelayController logic to handle packet forwarding bitchat/Services/TransportConfig.swift14-18
bleHighDegreeThreshold), TTL is clamped harder to contain floods bitchat/Services/BLE/BLEService.swift33 bitchat/Services/TransportConfig.swift17The MessageDeduplicator ensures that a packet is only processed or relayed once. Even if received from multiple neighbors, the hash-based check suppresses subsequent copies bitchat/Services/BLE/BLEService.swift60-61 Verified in tests to ensure duplicate packets are ignored bitchatTests/BLEServiceCoreTests.swift17-49
Sources: bitchat/Services/BLE/BLEService.swift33-61 bitchat/Services/TransportConfig.swift5-18 bitchatTests/BLEServiceCoreTests.swift17-49
BLEService integrates with the CourierStore to carry messages for offline peers.
To enable forward-secret offline messaging, peers gossip PrekeyBundles.
usedIDs to ensure a prekey is never reused across different messages bitchat/Services/Prekeys/PrekeyBundleStore.swift35-36BLEService enforces a trust-based policy for accepting store-and-forward deposits.
Sources: bitchat/Services/Prekeys/PrekeyBundleStore.swift13-142 bitchat/Services/BLE/BLEService.swift63-80
To support background reconnection, BLERecentPeripheralCache tracks recently seen peripherals.
bleRecentPeripheralCacheCap) bitchat/Services/BLE/BLERecentPeripheralCache.swift27-34bleRecentPeripheralMaxAgeSeconds (900s) are pruned bitchat/Services/BLE/BLERecentPeripheralCache.swift44-45Large messages are reassembled using BLEFragmentAssemblyBuffer.
bleMaxInFlightAssemblies (128) to protect memory bitchat/Services/BLE/BLEService.swift32bleAssemblerStallResetMs (250ms) bitchat/Services/TransportConfig.swift69Sources: bitchat/Services/BLE/BLERecentPeripheralCache.swift7-53 bitchat/Services/TransportConfig.swift9-72 bitchat/Services/BLE/BLEService.swift32-34
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.