GitHub - jensb1/handsfree: Turn a Mac into a Bluetooth car kit for your phone: ESP32 hands-free unit + SwiftUI app with per-side call recording

GitHub

5 min read Original article ↗

Turn a Mac into a Bluetooth car kit for your phone. Calls on your iPhone (or any phone) go through the Mac's microphone and speakers (or AirPods). Each side of the conversation is recorded on its own track, so it can be transcribed with exact speaker labels.

iPhone ──Bluetooth HFP──▶ ESP32 (the car kit) ──USB serial──▶ Handsfree.app on the Mac
          call control                          events + 8 kHz PCM        mic, speakers, recording,
          + call audio (SCO)                    in both directions        contacts, transcription

Why there is an ESP32 in the middle

A car kit is the hands-free side of the Bluetooth Hands-Free Profile (HFP). macOS still has an API for that role (IOBluetoothHandsFreeDevice). On macOS 26 it handles call control: dialing, answering, caller ID, signal and battery. But it refuses to open the call-audio (SCO) link, with kIOReturnUnsupported. Driving a USB Bluetooth dongle directly from user space (BTstack over libusb) was tried as well, and it panicked the kernel in Apple's USB host driver.

The original ESP32 has a Classic Bluetooth radio and Espressif's stack implements the HFP client, including routing call audio to the host. So the ESP32 is the car kit, and the Mac only sees a plain USB serial port.

Only the original ESP32 works (ESP32-WROOM-32/32E, WROVER, PICO-D4). The ESP32-S2, S3, C3, C5, C6, H2 and P4 have no Classic Bluetooth and cannot carry phone calls.

Hardware

  • Any original-ESP32 dev board with a USB-serial chip (CP2102 or CH340), e.g. an ESP32-WROOM-32E DevKitC. Tested with an ESP32-D0WD-V3 and a CP2102.
  • A USB cable to the Mac. Nothing else: no wiring, no Wi-Fi.

Firmware (esp32/)

Built with ESP-IDF 5.5.

cd esp32
. $IDF_PATH/export.sh
idf.py set-target esp32
idf.py build
idf.py -p /dev/cu.usbserial-XXXX -b 115200 flash

Flash at 115200 baud: with macOS's built-in CP210x driver, esptool's switch to a higher baud rate in the middle of a session corrupts data. The firmware's own link runs at a fixed 921600 baud without problems.

What it does:

  • Shows up to the phone as "Handsfree", a hands-free audio device. It is discoverable until a phone is bonded, then only connectable. It reconnects to the bonded phone every 10 s when the link is down.
  • Uses Just Works pairing: the phone shows a plain "Pair" prompt.
  • Runs the HFP client with call audio over HCI and an external codec. Wide-band speech is off, so the controller hands over plain 8 kHz 16-bit PCM in 7.5 ms frames, which the firmware relays unchanged.
  • Answers every audio frame from the phone with one frame of the Mac's microphone. The uplink therefore runs on the phone's audio clock and never drifts.
  • Asks the phone to turn off its echo cancellation (AT+NREC), because the Mac does it.

Serial protocol (esp32/main/link.h)

UART0, 921600 8N1. Each frame:

0xA5 0x5A | type (1 byte) | length (2 bytes, LE) | payload | XOR of payload
Type Direction Payload
E kit → Mac event, ASCII name args…
L kit → Mac log line
C Mac → kit command, ASCII name args…
A both call audio, 16-bit LE PCM, 8 kHz mono

Commands: dial <number>, redial, answer, hangup, dtmf <digit>, clcc, audio on|off, connect [addr], disconnect, pair on|off, forget, status.

Events: hello <addr> <version>, bonded <addr>, paired <addr> <name>, name <addr> <name>, pairing 0|1, conn <state> <addr>, audio <state> <frame bytes>, call, setup, held, service, signal, roam, battery, operator <name>, clip <number>, ccwa <number>, clcc <idx> <dir> <status> <mpty> <number>, ring, volume, at, authfail. status replays the current state, so the Mac can attach at any time.

The console, the logs and the bootloader output are all switched off, so nothing else writes to the UART. The Mac resynchronises on the magic bytes, so the boot ROM's output is harmless.

Mac app (macos/)

SwiftUI, macOS 15+, Swift 6.

cd macos
swift test
./build-app.sh            # signs with a Developer ID identity if one is in the keychain
open .build/Handsfree.app
  • Kit: finds the ESP32 on /dev/cu.usbserial-*, cu.SLAB_USBtoUART* or cu.wchusbserial*, and identifies it by its hello event (PhoneLink.swift). A small C target (SerialShim) sets the 921600 baud rate with IOSSIOSPEED, which Swift can't call directly.
  • Calls: shows network, carrier, signal and battery. Dial by number or by contact name (from the Mac's Contacts), answer, decline, hang up, send DTMF, mute, and move the audio between the Mac and the phone.
  • Audio (CallAudio.swift): plays the caller through the default output and captures the default microphone. Apple's voice processing removes the speaker echo. It follows device changes, including AirPods switching into headset mode in the middle of a call.
  • Recording: once a call is answered, each side is written to its own file in ~/Library/Application Support/Handsfree/calls/<id>/: me.caf and them.caf, 16 kHz mono.
  • Transcription (CallTranscriber.swift): optional, through an AllVoice server. Each side is transcribed separately in parts of up to 5 minutes, and the parts are merged into [hh:mm:ss] Me: … / [hh:mm:ss] <contact>: … lines. The result is saved with the call's metadata (number, contact, direction, duration). Progress is cached per part, so failed runs resume. Sign in under Settings.

Known limits

  • Call audio is narrowband (8 kHz, CVSD). Wide-band mSBC would need an mSBC encoder and decoder on the ESP32 or the Mac.
  • Opening the serial port can reset the board. The phone then drops the kit for about 2 s and reconnects, so don't start the app in the middle of a call.
  • One phone per kit.
  • With AirPods as the call device, they switch to their headset profile, which also limits quality.

Notes from building it

  • AVAudioEngine with voice processing fails with -10875 (output initialisation) unless outputNode and mainMixerNode are created before setVoiceProcessingEnabled(true).
  • Repeatedly creating and destroying voice-processing engines in quick succession can hang coreaudiod. Every process then blocks on its first Core Audio call; sudo killall coreaudiod recovers. The app keeps Core Audio queries off the main thread so its window stays usable.