iOS chat between two iPhones over Bluetooth LE, built for flight mode
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-11-11 20:55:59 +00:00
assets Initial commit 2026-11-11 19:27:13 +00:00
SkyChat Fix ChatStore: rename message(withID:) declaration to storedMessage(withID:) 2026-11-11 20:55:59 +00:00
SkyChat.xcodeproj Add chat over L2CAP with iMessage-style UI and iPad support (step 2) 2026-11-11 20:51:13 +00:00
.gitignore Add SkyChat iOS app: Bluetooth LE discovery and hello (step 1) 2026-11-11 19:43:07 +00:00
README.md Add chat over L2CAP with iMessage-style UI and iPad support (step 2) 2026-11-11 20:51:13 +00:00

SkyChat

Chat between iPhones and iPads over Bluetooth LE, with no Wi-Fi, cellular or shared network. It's built for flights: turn on flight mode, switch Bluetooth back on, and talk to the person a few seats away.

Status

Step 2: chat. Devices running SkyChat find each other and open an L2CAP channel. You get a Messages-style chat with bubbles, a typing indicator, Delivered/Read receipts, and history stored with SwiftData. Messages written while the other device is out of range are sent when it's back. A Bluetooth log (under Settings) shows what's happening on each side.

Step What Status
1 Discovery, role selection, handshake, hello over GATT ✅
2 L2CAP channel + length-prefixed framing, chat UI, SwiftData history, iPad ✅
3 Acks, outbox/retry, auto-reconnect polish (basic receipts + resend done in 2) ⏳
4 QR pairing + end-to-end encryption (CryptoKit X25519 / ChaChaPoly) ⏳
5 Background modes, state restoration, local notifications ⏳
6 Optional: photos, Wi-Fi peer-to-peer upgrade, mesh relay ⏳

Building

Requirements: Xcode 16 or newer and two physical iPhones or iPads on iOS 17 or later. The Simulator has no Bluetooth.

  1. Open SkyChat.xcodeproj.
  2. Select the SkyChat target → Signing & Capabilities, pick your team. If tech.bendiksen.skychat is taken for your team, change the bundle ID.
  3. Run on both devices and allow Bluetooth when prompted.
  4. Optionally tap the person icon to set a display name.
  5. Within a few seconds each device lists the other, with a green dot on its avatar. Tap it and start typing.

On iPad the conversation list and the chat sit side by side. On iPhone they collapse into one navigation stack, as in Messages.

If you ran step 1 before, reinstall on both devices: the wire protocol is now v2.

The project uses folder-synced groups. Any file you add under SkyChat/ is part of the app target automatically, with no project-file edits needed.

Testing in flight mode

Turn on flight mode, then open Control Center and tap Bluetooth so it's on again. iOS remembers this, so next time flight mode leaves Bluetooth on. If Bluetooth is off, SkyChat shows a banner explaining this.

How it works

Every phone runs both Bluetooth LE roles at once:

  • Peripheral: advertises the SkyChat service and serves its characteristics.
  • Central: scans for that service and connects to the phones it finds.

Two phones would otherwise connect to each other twice. Instead, each phone has a persistent random SkyChat ID, and the phone whose ID sorts lower keeps its outgoing connection while the other backs off. Both phones apply the same rule, so each pair ends up with exactly one link.

GATT layout

SkyChat service  8E4B1F2A-6C3D-4E7A-9B10-5D2F7C8A0001
├── identity  …0002  read    {"v":2,"id":"…","name":"…","psm":193}
├── inbox     …0003  write   WireMessage JSON, central → peripheral
└── outbox    …0004  notify  WireMessage JSON, peripheral → central

L2CAP channel (PSM picked by iOS)
    [4-byte big-endian length][WireMessage JSON]   both directions
  1. Discover the service, connect, discover its characteristics.
  2. Read identity. If our ID sorts higher, disconnect and let the other phone connect to us.
  3. Subscribe to outbox.
  4. Write a handshake (our ID and name) to inbox.
  5. The peripheral answers with its own handshake, carrying its L2CAP PSM.
  6. Open an L2CAP channel to that PSM. From then on every message uses the channel. If it can't be opened, or closes, messages fall back to GATT.

If a phone goes out of range, the central re-issues connect. iOS connection requests never time out, so the link comes back by itself when the phones are close again.

Messages

WireMessage is JSON with one-letter keys (k kind, i id, f from, n name, t text, s sent-at, p PSM, r referenced ids, a typing active). Kinds:

Kind Meaning
handshake ID, name and (from the peripheral) the L2CAP PSM
chat A message. Its i is the same on both devices
delivered / read Receipts for the chat ids in r
typing Started (a: true) or stopped typing. Expires after 6 s without a refresh

Over GATT each message must fit in one write (~512 B) or notification (~180 B). Over L2CAP frames can be up to 1 MiB.

Delivery

Messages are saved before they're sent. Whenever a link to a peer comes up or improves (L2CAP opens), everything still pending or sent (no receipt yet) goes out again, oldest first. The receiver drops duplicates by id and repeats its receipts, so a lost receipt heals itself on the next resend.

Layout

SkyChat/
├── App/SkyChatApp.swift             App entry, owns transport, chat store, SwiftData container
├── Bluetooth/
│   ├── BLEConstants.swift           UUIDs and the role rule
│   ├── BluetoothTransport.swift     Central + peripheral, link setup, send/receive
│   └── L2CAPConnection.swift        Length-prefixed framing over an L2CAP channel
├── Chat/ChatStore.swift             Transport ↔ history: send, resend, receipts, typing
├── Model/
│   ├── ChatModels.swift             SwiftData Conversation + ChatMessage
│   ├── LocalIdentity.swift          Persistent SkyChat ID + display name
│   ├── Peer.swift                   Nearby device and log entry models
│   └── WireMessage.swift            Wire format + codec
└── Views/                           SwiftUI screens (list, chat, bubbles, composer)