
# Bluetooth

> A Linux Bluetooth adapter (`hci0`) for the guest, backed by the browser's Web Bluetooth.

```js
import { createKernel, VirtualLeController, WebBluetoothCentral } from "unkernel";

const vm = await createKernel({ bluetooth: true }); // the kernel's Bluetooth transport, nothing attached
const central = new WebBluetoothCentral();
await central.init(); // devices allowed before, where the browser has getDevices()
await vm.bluetooth.attach(new VirtualLeController({ central })); // the guest gets hci0

button.onclick = () => central.requestDevice(); // Web Bluetooth's chooser, from a click
// in the guest: apt install bluez; bluetoothctl, then scan on, connect <addr>, menu gatt
```

The guest's kernel has a plain HCI transport (kwbt). What sits behind it is a controller: something that takes HCI packets from the guest and gives HCI packets back. Unmodified BlueZ runs on it (`bluetoothd`, `bluetoothctl`, `btmgmt`, `btmon`, D-Bus GATT clients such as bleak), as on a USB dongle.

Web Bluetooth is a GATT client API with no HCI, so `VirtualLeController` emulates an LE controller over a central (`WebBluetoothCentral` in browsers, `FakeGattCentral` anywhere). While the guest scans it reports the devices the user allowed (at a stable random address derived from each device), and when the guest connects it connects the device and serves its GATT database to the guest: discovery, reads (long ones too), writes with and without response, notifications and indications. What Web Bluetooth cannot do is reported to the guest as unsupported: pairing (the host's OS pairs, out of the page's sight; a characteristic that needs it fails with an authentication error), BR/EDR, advertising (the peripheral role) and L2CAP channels other than ATT.

A device is only visible once the user picked it in the browser's chooser, and only the GATT services named at that moment: `requestDevice()` asks for the standard services and `new WebBluetoothCentral({ services })` adds your own UUIDs. Browsers hide some services and characteristics (HID, firmware update services); the guest sees them as absent or refused.

`vm.bluetooth.attach(controller)` replaces whatever is attached, and `detach()` removes it: the guest sees hci0 go and come back, with its connections dropped, as when a dongle is unplugged. `vm.bluetooth.state` and `onChange()` say what is attached and whether the guest has it powered; `VirtualLeController.onActivity()` reports the guest scanning (the moment to offer the chooser) and its connections.

Other controllers fit the same interface: an object with `attach(sink)`, `send(packet)` and `detach()` (packets are H4: the packet type byte, then the HCI packet). `H4StreamController` passes a real controller's H4 through from a byte stream such as a Web Serial port's (untested on hardware).

`WebBluetoothCentral` needs Web Bluetooth: Chromium-based browsers, in a secure context; `webBluetoothBlocker()` says why it is missing. In worker mode the controller still runs on your thread, where `requestDevice()` works. `FakeGattCentral` holds scriptable peripherals (values, notifications, errors, disconnects) for tests; the CLI's `--bluetooth` attaches one with a battery, a heart rate and a loopback characteristic.

Nothing of the controller is in a snapshot (`vm.snapshot()`): after a restore the guest's adapter is replaced by whatever the new machine attaches, and connections from before fail.
