SwiftHarmonyOSBluetoothLynsi

Pairing a Mac as a BLE HID Keyboard for HarmonyOS: Knocking, Silent Reconnects, and Identity Split

Getting HarmonyOS to recognize a Mac as a physical BLE HID keyboard for Chinese pinyin composition hit three Bluetooth stack traps: dual-mode classic fallback, a 60-second CTKD stall, and silent reconnects without CCCD resubscription.

In an earlier post on remote IME ownership, one prerequisite stood out: HarmonyOS’s Xiaoyi input method only switches into hardware-keyboard pinyin composition when the phone believes a physical keyboard is attached.

Publishing a standard BLE HID over GATT keyboard from macOS sounded straightforward. Instead, capturing bluetooth_service logs on a Mate X5 (HarmonyOS 6.1) paired with macOS 27.0.1 uncovered three stack behaviors that took real-device logs on both sides to untangle (dd46604, bec93c8, 20cc8e4).

Trap 1: Public Advertising Address Triggered Classic Bluetooth Pairing

When the first prototype advertised from CoreBluetooth and the user tapped pair in HarmonyOS Settings, the phone couldn’t find a keyboard at all.

The phone’s hilog explained why: macOS advertises CoreBluetooth peripherals using its public Bluetooth address. HarmonyOS’s Bluedroid stack inspects that address, marks the Mac as dual-mode (dev_type=0x3), and—under Settings’ “auto” transport selection—attempts Classic Bluetooth pairing. macOS hosts no Classic HID service there.

The workaround is “knocking” (knock). The Mac first connects as a BLE central to Lynsi’s wake doorbell UUID (5A4C0200-...) on the phone. Seeing an active LE ACL (BTM_UseLeLink: found LE acl), the phone switches pairing to transport 2 (LE).

Trap 2: The 60-Second Spin and “Pairing Failed”

Once pairing ran over LE, the phone spun for roughly a minute and reported “Pairing failed”—yet tapping the entry under Paired Devices connected the keyboard immediately.

Logs showed Cross-Transport Key Derivation (CTKD) triggering a Classic SDP lookup over the same address that already held an active LE ACL. The controller rejected the page (Connection Already Exists), timed out after 60 seconds, and handed up an empty UUID list. Fortunately, manually tapping the bonded device calls ConnectAllowedProfiles, which allows all profiles on an empty list and brings up LE HID. In KeyboardAssist.ets, devices on API 16+ invoke connectAllowedProfiles automatically instead of making the user tap Settings.

Trap 3: Reconnecting in 1 ms—and Going Completely Silent

The toughest surprise hit on reconnect: Settings showed Lynsi Keyboard connected, yet zero HID input reports ever arrived on the phone.

Within a single Bluetooth power cycle, HarmonyOS’s HID host caches GATT handles in memory and reconnects in 1 ms without rewriting the CCCD notification descriptor, assuming the peripheral persists subscriptions across bonds. macOS CoreBluetooth does not persist CCCDs for app-hosted services. The keyboard worked once after pairing, then went silent on every reconnect until Bluetooth was toggled off and on.

Experiment 1b (dd46604) rescued the design: while the “silent” BLE keyboard is attached, key codes injected via uitest uiInput keyEvent still enter Xiaoyi’s hardware-keyboard pinyin pipeline.

Commit bec93c8 split identity from transport permanently:

  • BluetoothKeyboard.swift hosts a 128-bit HID-over-GATT service that never sends input reports, restricting GATT reads and writes to the specific phone bonded during setup (blocking iPhones on the same Apple ID from grabbing the keyboard).
  • Every keystroke travels over RemoteKeyboard.swift and RemoteControlSession.swift via uitest, making silent CCCD reconnects harmless.

Phone-Assisted Pairing Without Dropping Screen Mirroring

The initial manual flow had to drop the active Lynsi session so the phone’s doorbell would advertise, then automatically resume mirroring afterward (reopenReceiver).

In the second pass (20cc8e4, kbAssist=1), the phone keeps its doorbell open while connected when Mac sends keyboard.pair start. Once Mac knocks over the authenticated channel, KeyboardAssist.ets waits 2 seconds and pairs only if exactly one device is connected on the doorbell (0 or >1 devices aborts immediately so a nearby stranger’s device cannot be bonded as an input source).

Fixing 2.8-Second “Unused LE” Drops and Computer-Name Matching

Field testing caught one more glitch: kbPresent flickered false for ~2.8 seconds while screen mirroring kept running smoothly.

bluetoothd logged Disconnecting GATT for unused LE device (ledisconnectionreason: Unused). Because no HID notifications were subscribed and no reports flowed, macOS pruned the idle LE link. Adding an explicit LE hold protocol (kbHold=1 / hold / held / release via BluetoothKeyboardHold) keeps the link alive while remote control is active and releases it immediately on stop, sleep, or disconnect.

Finally, after reconnecting, HarmonyOS sometimes registered the keyboard under the Mac’s computer name (such as Henry'sMA, bus=5, vendor=65535, product=1) instead of Lynsi. Updating presence checks to match the uniquely bonded Mac plus HID PnP vendor 65535/0xFFFF stopped false “keyboard missing” warnings.

Separating keyboard identity from key transport let BLE HID do the one job it excels at—telling the phone’s IME a hardware keyboard is present—while leaving reliable delivery to the existing control channel.

✦

Comments

  • Loading…

Comments are reviewed before publishing; email is visible only to me.