mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-24 23:45:18 +00:00
- Add LZ4 message compression for 30-70% bandwidth reduction - Implement adaptive battery optimization with power modes - Optimize Bloom filter with bit-packed storage and SHA256 hashing - Create WiFi Direct integration plan for future implementation - Enable and update Bloom filter tests
6.1 KiB
6.1 KiB
WiFi Direct Integration Plan for BitChat
Overview
WiFi Direct enables peer-to-peer WiFi connections without requiring an access point, offering significantly higher bandwidth and range compared to Bluetooth Low Energy.
Key Specifications
- Range: 100-200 meters (vs BLE's 10-30m)
- Speed: 250+ Mbps (vs BLE's 1-3 Mbps)
- Power: Higher consumption than BLE
- Platform Support:
- iOS: MultipeerConnectivity framework
- Android: WiFi P2P API
- macOS: Network.framework with Bonjour
Alternative Transport Technologies
Ultrasonic Communication
- What: Uses sound waves above human hearing (>20kHz) to transmit data
- Range: 1-10 meters typically
- Speed: ~1-10 kbps
- Pros: Works through thin walls, no radio interference, very low power
- Cons: Limited range, sensitive to noise, low bandwidth
- Use case: Secret communication in meetings, data transfer when radio is jammed
LoRa (Long Range)
- What: Low-power, wide-area network protocol using sub-GHz frequencies
- Range: 2-15 km in rural areas, 2-5 km in urban
- Speed: 0.3-50 kbps
- Pros: Incredible range, very low power, penetrates buildings well
- Cons: Very low bandwidth, requires special hardware, regulated frequencies
- Use case: Disaster relief, rural communities, sensor networks
Architecture Design
Transport Protocol Interface
protocol TransportProtocol {
var transportType: TransportType { get }
var isAvailable: Bool { get }
var currentPeers: [PeerInfo] { get }
func startDiscovery()
func stopDiscovery()
func send(_ packet: BitchatPacket, to peer: PeerID?)
func setDelegate(_ delegate: TransportDelegate)
}
enum TransportType {
case bluetooth
case wifiDirect
case ultrasonic // future
case lora // future
}
// Transport Manager to coordinate multiple transports
class TransportManager {
private var transports: [TransportProtocol] = []
private var routingTable: [PeerID: TransportType] = [:]
func sendOptimal(_ packet: BitchatPacket, to peer: PeerID?) {
// Choose best transport based on:
// 1. Message size
// 2. Battery level
// 3. Available transports
// 4. Peer capabilities
}
}
Implementation Phases
Phase 1: Abstract Transport Layer
- Create
TransportProtocolinterface - Refactor
BluetoothMeshServiceto implement protocol - Create
TransportManagerto coordinate transports - Update
ChatViewModelto use transport abstraction
Phase 2: WiFi Direct Transport
- Create
WiFiDirectTransportclass - iOS: Use MultipeerConnectivity framework
- macOS: Use Network.framework with Bonjour
- Handle transport handoff (BLE → WiFi when available)
Phase 3: Intelligent Routing
- Implement bandwidth detection
- Create routing algorithm:
- Small messages (< 1KB): Use BLE (lower power)
- Large messages/files: Use WiFi Direct
- Emergency/broadcast: Use all transports
- Add transport negotiation protocol
Phase 4: Advanced Features
- File transfer with resumption
- Video/audio streaming support
- Hybrid mesh (some nodes BLE-only, some WiFi-capable)
- Transport bonding (use multiple simultaneously)
Key Considerations
Battery Impact
- WiFi Direct uses significantly more power than BLE
- Only activate when:
- Large file transfer needed
- User explicitly enables
- Device is charging
- Battery > 50%
Discovery Strategy
- Use BLE for initial discovery (low power)
- Exchange WiFi Direct capabilities
- Establish WiFi Direct only when needed
- Fall back to BLE if WiFi fails
Security
- Use same encryption (X25519 + AES-256-GCM)
- Pin WiFi Direct connections with BLE-exchanged keys
- Prevent WiFi Direct spoofing attacks
User Experience
- Automatic transport selection
- Visual indicator showing active transport
- Manual override option
- Seamless handoff between transports
Proposed File Structure
bitchat/
├── Transports/
│ ├── TransportProtocol.swift
│ ├── TransportManager.swift
│ ├── BluetoothTransport.swift (refactored from BluetoothMeshService)
│ ├── WiFiDirectTransport.swift (new)
│ └── TransportDelegate.swift
├── Services/
│ └── RoutingService.swift (intelligent message routing)
Benefits
- 10-100x faster file transfers
- Longer range for fixed installations
- Video chat capability
- Backwards compatible (BLE-only devices still work)
- Future-proof (easy to add more transports)
Implementation Notes
iOS MultipeerConnectivity Example
import MultipeerConnectivity
class WiFiDirectTransport: NSObject, TransportProtocol {
private let serviceType = "bitchat-wifi"
private var peerID: MCPeerID
private var session: MCSession
private var advertiser: MCNearbyServiceAdvertiser
private var browser: MCNearbyServiceBrowser
func startDiscovery() {
advertiser.startAdvertisingPeer()
browser.startBrowsingForPeers()
}
}
Message Size Routing Logic
func selectTransport(for message: Data) -> TransportType {
let size = message.count
let batteryLevel = BatteryOptimizer.shared.batteryLevel
if size > 10_000 && batteryLevel > 0.5 {
return .wifiDirect
} else if size < 1_000 || batteryLevel < 0.3 {
return .bluetooth
} else {
// Medium size, good battery - use faster if available
return wifiAvailable ? .wifiDirect : .bluetooth
}
}
Testing Strategy
- Unit Tests: Mock transport implementations
- Integration Tests: BLE + WiFi handoff scenarios
- Performance Tests: Throughput comparison
- Battery Tests: Power consumption analysis
- Field Tests: Real-world range and reliability
Future Considerations
- Transport Plugins: Allow third-party transport implementations
- SDN Integration: Software-defined networking for complex topologies
- QoS: Quality of Service for different message types
- Compression: Different algorithms per transport
- Multi-path: Send redundant copies over multiple transports