# Glasses

> Battery level and connection state of the glasses.

Source: https://docs.dev.glassly.ai/app-devs/core-concepts/glasses/

`session.glasses` reports on the glasses hardware: battery level plus charging
state, connection state, and Wi-Fi state. All are subscriptions, so you get
a callback whenever the value changes.

```typescript src/background/index.ts
session.glasses.onBattery(({ level, charging }) => {
  if (level <= 15 && !charging) {
    session.display.render([{type: "text", id: "msg", box: {x: 0, y: 0, w: 576, h: 288}, text: "Glasses battery low"}]);
  }
});
```

There's no synchronous getter for the current value. You learn the state by
subscribing and waiting for the first event.

## Battery

`onBattery` fires when the glasses report a new battery reading.

```typescript
session.glasses.onBattery((data) => {
  console.log(`${data.level}%`, data.charging ? "charging" : "on battery");
});
```

Each `BatteryData` has:

| Field | Type | Notes |
|---|---|---|
| `level` | `number` | Battery percentage, 0 to 100. |
| `charging` | `boolean` | Whether the glasses are charging. |

## Connection

`onConnection` fires when the glasses connect or disconnect.

```typescript
session.glasses.onConnection((data) => {
  if (!data.connected) {
    // The glasses dropped. Display calls won't reach the HUD until they return.
  }
});
```

Each `ConnectionData` has:

| Field | Type | Notes |
|---|---|---|
| `connected` | `boolean` | Whether the glasses are currently connected. |
| `modelName` | `string?` | Glasses model name, when reported. May be absent. |

`modelName` is optional, so guard for it before reading:

```typescript
session.glasses.onConnection(({ connected, modelName }) => {
  if (connected && modelName) {
    console.log(`Connected to ${modelName}`);
  }
});
```

## Wi-Fi

`onWifi` fires the current state on subscribe, then again on every change.
`connected` is `false` on glasses without Wi-Fi.

```typescript
session.glasses.onWifi(({ connected, ssid }) => {
  if (!connected) {
    // Streaming and similar features need the glasses on Wi-Fi.
  }
});
```

Each `WifiData` has:

| Field | Type | Notes |
|---|---|---|
| `connected` | `boolean` | Whether the glasses currently have Wi-Fi. `false` on glasses without Wi-Fi hardware. |
| `ssid` | `string?` | The connected network name, when reported. |
| `localIp` | `string?` | The glasses' local IP, when reported. |

`requestWifiSetup(reason?)` asks the host to open the glasses Wi-Fi setup flow
on the phone. `reason` is a user-facing line shown in the setup prompt.

```typescript
await session.glasses.requestWifiSetup("Streaming needs your glasses on Wi-Fi");
```

## Cleaning up

Every subscription method returns an unsubscribe function:

```typescript
const off = session.glasses.onBattery(handler);
off(); // stop listening
```

`session.glasses` covers the glasses hardware. The phone has its own battery and
connection events on [`session.phone`](https://docs.dev.glassly.ai/app-devs/core-concepts/phone.md). For what
the connected glasses can *do* (display, camera, sensors), read
`session.capabilities` once the session is [ready](https://docs.dev.glassly.ai/app-devs/core-concepts/session.md#lifecycle).
