# gan-web-bluetooth

> Library for interaction with GAN Smart Timers and GAN Smart Cubes using Web Bluetooth API

Latest version **3.0.2** (published 2024-08-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install gan-web-bluetooth
pnpm add gan-web-bluetooth
yarn add gan-web-bluetooth
bun add gan-web-bluetooth
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.2 |
| Published | 2024-08-22 |
| First published | 2023-02-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 211.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 112 |
| Author | Andy Fedotov |
| Maintainers | afedotov |
| Keywords | GAN, GANCUBE, Timer, GAN Smart Timer, GAN Smart Cube, Smart Timer, Smart Cube, Bluetooth, Web Bluetooth, Cubing, Speedcubing, Rubik's Cube |

## Links

- npm: https://www.npmjs.com/package/gan-web-bluetooth
- Repository: https://github.com/afedotov/gan-web-bluetooth
- Homepage: https://github.com/afedotov/gan-web-bluetooth#readme
- Issues: https://github.com/afedotov/gan-web-bluetooth/issues
- npm.io page: https://npm.io/package/gan-web-bluetooth

## Dependencies (3)

- [rxjs](https://npm.io/package/rxjs.md) >= 7
- [aes-js](https://npm.io/package/aes-js.md) ^3.1.2
- [@types/web-bluetooth](https://npm.io/package/@types/web-bluetooth.md) ^0.0.20

## Recent versions

- 3.0.2 (latest) — 2024-08-22
- 3.0.1 — 2024-08-21
- 2.3.1 — 2024-08-17
- 2.3.0 — 2024-08-17
- 2.2.2 — 2024-02-26
- 2.2.1 — 2024-02-19
- 2.2.0 — 2024-02-12
- 2.1.3 — 2024-01-15
- 2.1.2 — 2024-01-15
- 2.1.1 — 2024-01-11
- 2.1.0 — 2024-01-09
- 2.0.0 — 2024-01-07
- 1.0.4 — 2023-06-03
- 1.0.3 — 2023-02-27
- 1.0.2 — 2023-02-18
- … 2 more at https://npm.io/package/gan-web-bluetooth/versions

## README

## Use of GAN Smart Timers & Smart Cubes via Web Bluetooth API

This library is designed for easy interaction with GAN Smart Timers and Smart Cubes 
on the platforms that support [Web Bluetooth API](https://github.com/WebBluetoothCG/web-bluetooth/blob/main/implementation-status.md).

Nature of the GAN Smart Timer and Smart Cubes is event-driven, so this library is
depends on [RxJS](https://rxjs.dev/), and library API provide [Observable](https://rxjs.dev/guide/observable) 
where you can subscribe for events.

## Installation

Package `gan-web-bluetooth` is available in the npm registry:

[![npm version](https://badge.fury.io/js/gan-web-bluetooth.svg)](https://badge.fury.io/js/gan-web-bluetooth)

```
$ npm install gan-web-bluetooth
```

## GAN Smart Timers

Supported GAN timer devices:
- GAN Smart Timer
- GAN Halo Smart Timer

Sample application how to use this library with GAN Smart Timer can be found here:
- https://github.com/afedotov/gan-timer-display
- Live version: [GAN Timer Display](https://afedotov.github.io/gan-timer-display/)

Sample TypeScript code:
```typescript
import { connectGanTimer, GanTimerState } from 'gan-web-bluetooth';

var conn = await connectGanTimer();

conn.events$.subscribe((timerEvent) => {
    switch (timerEvent.state) {
        case GanTimerState.RUNNING:
            console.log('Timer is started');
            break;
        case GanTimerState.STOPPED:
            console.log(`Timer is stopped, recorded time = ${timerEvent.recordedTime}`);
            break;
        default:
            console.log(`Timer changed state to ${GanTimerState[timerEvent.state]}`);
    }
});
```

You can read last times stored in the timer memory:
> Please note that you should not use `getRecordedTimes()` in polling fashion 
> to get currently displayed time. Timer and its bluetooth protocol does not designed for that.
```typescript
var recTimes = await conn.getRecordedTimes();
console.log(`Time on display = ${recTimes.displayTime}`);
recTimes.previousTimes.forEach((pt, i) => console.log(`Previous time ${i} = ${pt}`));
```

#### Possible timer states and their description:

State | Description
-|-
IDLE | Timer is reset and idle
HANDS_ON | Hands are placed on the timer
HANDS_OFF | Hands removed from the timer before grace delay expired
GET_SET | Grace delay is expired and timer is ready to start
RUNNING | Timer is running
STOPPED | Timer is stopped, this event includes recorded time
FINISHED | Move to this state immediately after STOPPED
DISCONNECT | Fired when timer is disconnected from bluetooth


#### Timer state diagram:

```mermaid
stateDiagram-v2
    direction LR
    IDLE --> HANDS_ON
    HANDS_ON --> HANDS_OFF
    HANDS_OFF --> HANDS_ON
    HANDS_ON --> GET_SET
    GET_SET --> RUNNING
    RUNNING --> STOPPED
    STOPPED --> FINISHED
    FINISHED --> IDLE
```

## GAN Smart Cubes

Supported Smart Cube devices:
- GAN Gen2 protocol smart cubes:
  - GAN Mini ui FreePlay
  - GAN12 ui FreePlay
  - GAN12 ui
  - GAN356 i Carry S
  - GAN356 i Carry
  - GAN356 i 3
  - Monster Go 3Ai
- MoYu AI 2023 (this cube uses GAN Gen2 protocol)
- GAN Gen3 protocol smart cubes:
  - GAN356 i Carry 2
- GAN Gen4 protocol smart cubes:
  - GAN12 ui Maglev
  - GAN14 ui FreePlay

Sample application how to use this library with GAN Smart Cubes can be found here:
- https://github.com/afedotov/gan-cube-sample
- Live version: [gan-cube-sample](https://afedotov.github.io/gan-cube-sample/)

Sample TypeScript code:
```typescript
import { connectGanCube } from 'gan-web-bluetooth';

var conn = await connectGanCube();

conn.events$.subscribe((event) => {
    if (event.type == "FACELETS") {
        console.log("Cube facelets state", event.facelets);
    } else if (event.type == "MOVE") {
        console.log("Cube move", event.move);
    }
});

await conn.sendCubeCommand({ type: "REQUEST_FACELETS" });
```

Since internal clock of the most GAN Smart Cubes is not ideally calibrated, they typically introduce 
noticeable time skew with host device clock. Best practice here is to record timestamps of move events 
during solve using both clocks - host device and cube. Then apply linear regression algorithm 
to fit cube timestamp values and get fairly measured elapsed time. This approach is invented 
and firstly implemented by **Chen Shuang** in the **csTimer**. This library also contains `cubeTimestampLinearFit()` 
function to accomplish such procedure. You can look into the mentioned sample application code for details, 
and this [Jupyter notebook](https://github.com/afedotov/scipy-notebooks/blob/main/ts-linregress.ipynb) for visualisation
of such approach.

---
_Source: https://npm.io/package/gan-web-bluetooth · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
