> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Bluetooth, NFC and Wi-Fi fakes

Put a Bluetooth device in range, tap an NFC tag, or put the phone on a Wi-Fi network in a unit test. `import { addBluetoothDevice, tapNfcTag, setWifiNetwork } from '@blotch/angular-lynx-testing-library';`

## Bluetooth

Fakes the radio behind [`LynxBluetooth`](/api/angular-lynx/hardware/bluetooth.md). Each test starts with no devices and the radio on.

### `addBluetoothDevice()`

```typescript
function addBluetoothDevice(
  device: FakeBluetoothDevice,
): FakeBluetoothDeviceRef;
```

Puts a Bluetooth LE device in range of the app. `LynxBluetooth` finds it, connects to it and talks to it as it would a real one.

```typescript
const frame = addBluetoothDevice({
  id: 'frame',
  name: 'Blotch - XyZ1',
  services: {
    [SETUP]: {
      [CREDENTIALS]: {
        properties: ['write'],
        onWrite: (_value, device) => device.notify(SETUP, STATUS, ONLINE),
      },
      [STATUS]: { properties: ['notify'] },
    },
  },
});
await render(SetUpFrame);

fireEvent.tap(await screen.findByText('Blotch - XyZ1'));
await waitForUpdate();

expect(frame.writes).toEqual([
  {
    service: SETUP,
    characteristic: CREDENTIALS,
    value: WIFI,
    withResponse: true,
  },
]);
```

- **Scans see it.** A running scan sees it at once, and one that starts later sees it as it starts.
- **It advertises every service it has**, unless `advertisedServices` says otherwise.
- **A characteristic can be read, written and notified**, unless its `properties` say otherwise.
- **`onWrite`** is called with each value the app writes, and the device. The spec can then answer as the real device does.
- **A write longer than the device takes is refused**, as on a phone. `maxWriteLength` sets the limit for a write without response.
- **UUIDs are spelled either way**, as [`canonicalBluetoothUUID()`](/api/angular-lynx/hardware/bluetooth.md#canonicalbluetoothuuid) reads them.
- **Adding an `id` a second time throws.**

The device it returns:

| Member                                   | Description                                                            |
| ---------------------------------------- | ---------------------------------------------------------------------- |
| `connected`                              | Whether the app is connected to it                                     |
| `writes`                                 | What the app wrote, oldest first                                       |
| `notify(service, characteristic, value)` | Sends a value, which reaches the app while it's subscribed             |
| `disconnect()`                           | Ends the connection from the device's side, as going out of range does |

### `setBluetoothState()`

```typescript
function setBluetoothState(state: BluetoothState): void;
```

Sets the state of the phone's Bluetooth, as the person turning it off or refusing permission does. A spec can then check what the app shows.

```typescript
setBluetoothState('poweredOff');
await render(SetUpFrame);

expect(await screen.findByText('Turn on Bluetooth')).toBeInTheDocument();
```

- **Anything but `'poweredOn'`** ends a running scan and every connection, as it does on a phone.
- **`'unknown'`** stands for not asked yet. The person allows it when `LynxBluetooth` asks, as `requestPermission()`, `scan()` and `connect()` do.

## NFC

Fakes [`LynxNfc`](/api/angular-lynx/hardware/nfc.md)'s native module. Each test starts with a phone that reads tags, no session open and no writes.

Each answer reaches the app through the [background thread](/guide/glossary.md#background-thread), as on a phone. Wait for it with `waitForUpdate()`.

```typescript
await render(MemberCard);
fireEvent.tap(screen.getByText('Scan your card'));
tapNfcTag({ records: [{ type: 'url', value: 'https://example.com/m/42' }] });
await waitForUpdate();

expect(
  screen.getByText('Member: https://example.com/m/42'),
).toBeInTheDocument();
```

### `tapNfcTag()`

```typescript
function tapNfcTag(tag?: FakeNfcTag): void;
```

Brings a tag near the phone.

- **The open `read()`** resolves with its `records`.
- **The open `write()`** writes to it.
- **An open `scan()`** calls back with it.
- **With no session open yet**, the next one takes it.

| `FakeNfcTag` property | Default | Description                                                 |
| --------------------- | ------- | ----------------------------------------------------------- |
| `records`             | none    | What the tag holds, which a read resolves with              |
| `writable`            | `true`  | `false` for a locked tag                                    |
| `capacity`            | `888`   | The bytes its [NDEF](/guide/glossary.md#ndef) message takes |
| `id`                  | none    | Its serial number                                           |
| `ndef`                | `true`  | `false` for a tag without NDEF records                      |

### `failNfcSession()`

```typescript
function failNfcSession(
  code: 'canceled' | 'timeout' | 'busy' | 'failed',
  message?: string,
): void;
```

Ends the open session, or the next one, as iOS does: `canceled` when the person closes the sheet, `timeout` after a minute.

### `setNfcState()`

```typescript
function setNfcState(state: 'ready' | 'disabled' | 'unsupported'): void;
```

Turns NFC off (`disabled`), or takes the reader away (`unsupported`).

`state()` reads it at startup, and each time the app comes back. Send the app away and back with [`setAppState()`](/api/testing-library/fakes/device.md#setappstate).

### `nfcSession()`

```typescript
function nfcSession(): NfcSessionInfo | undefined;
```

Says what's open: its `alertMessage`, whether it's a `scan()` (`multiple`), and whether it's `writing`.

### `nfcWrites()`

```typescript
function nfcWrites(): readonly (readonly NfcRecord[])[];
```

Lists the records each `write()` wrote, as a read gives them back.

## Wi-Fi

Fakes the network behind [`LynxWifi`](/api/angular-lynx/hardware/wifi.md). Each test starts off Wi-Fi, with no joins.

### `setWifiNetwork()`

```typescript
function setWifiNetwork(network: { ssid: string; bssid?: string } | null): void;
```

Puts the spec's phone on `network`, which `LynxWifi.currentNetwork()` resolves from then on. `null` takes it off Wi-Fi.

```typescript
setWifiNetwork({ ssid: 'Home' });
await render(FrameSetup);
await waitForUpdate();

expect(screen.getByText('Home')).toBeInTheDocument();
```

- **A successful `join()`** puts the phone on the network it joined.
- **`leave()`** puts it back where it was.

### `denyWifiNetworkRead()`

```typescript
function denyWifiNetworkRead(
  code?: 'denied' | 'notFound' | 'canceled' | 'unavailable' | 'failed',
  message?: string,
): void;
```

Makes `currentNetwork()` reject with a `WifiError` of `code` from then on.

- **`code`** is `denied` by default.
- **The default message** for `denied` is the one an Android app gets when the person refuses the location prompt.

### `allowWifiNetworkRead()`

```typescript
function allowWifiNetworkRead(): void;
```

Lets `currentNetwork()` read the network again, after `denyWifiNetworkRead()`.

### `queueWifiJoinResult()`

```typescript
function queueWifiJoinResult(
  result:
    'joined' | 'denied' | 'notFound' | 'canceled' | 'unavailable' | 'failed',
  message?: string,
): void;
```

Answers the next `LynxWifi.join()` with `result`: `joined`, which puts the phone on the network, or a `WifiError` of that code.

```typescript
queueWifiJoinResult('notFound');
fireEvent.tap(screen.getByText('Connect'));
await waitForUpdate();

expect(screen.getByText('Is the frame switched on?')).toBeInTheDocument();
```

- **Each call answers one join**, in order.
- **Without one, a join succeeds.**

### `wifiJoins()`

```typescript
function wifiJoins(): readonly {
  ssid: string;
  password?: string;
  joinOnce: boolean;
}[];
```

The networks the app joined through `LynxWifi.join()` in this test, oldest first, answered or not. Each test starts with none.

### `wifiLeaves()`

```typescript
function wifiLeaves(): readonly string[];
```

The names of the networks the app left in this test. Each test starts with none.

## Types

| Type                          | Description                                                                                                                                    |
| ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `FakeBluetoothDevice`         | What `addBluetoothDevice()` takes: `id`, `name`, `rssi`, `advertisedServices`, `serviceData`, `manufacturerData`, `services`, `maxWriteLength` |
| `FakeBluetoothCharacteristic` | One characteristic in `services`: its `properties`, `value` (what a read gets) and `onWrite`                                                   |
| `FakeBluetoothDeviceRef`      | The device `addBluetoothDevice()` returns                                                                                                      |
| `FakeBluetoothWrite`          | One of its `writes`: `service`, `characteristic`, `value` and `withResponse`                                                                   |
| `FakeNfcTag`                  | What `tapNfcTag()` takes                                                                                                                       |
| `NfcSessionInfo`              | What `nfcSession()` returns: `alertMessage`, `multiple` and `writing`                                                                          |
| `NfcSessionFailure`           | The codes `failNfcSession()` takes                                                                                                             |
| `FakeWifiNetwork`             | What `setWifiNetwork()` takes: `ssid` and an optional `bssid`                                                                                  |
| `FakeWifiFailure`             | The `WifiError` codes `denyWifiNetworkRead()` and `queueWifiJoinResult()` take                                                                 |
| `WifiJoinRequest`             | One of `wifiJoins()`: `ssid`, `password` and `joinOnce`                                                                                        |

## Related

- [Device fakes](/api/testing-library/fakes/index.md)
- [`LynxBluetooth`](/api/angular-lynx/hardware/bluetooth.md)
- [`LynxNfc`](/api/angular-lynx/hardware/nfc.md)
- [`LynxWifi`](/api/angular-lynx/hardware/wifi.md)
