# danavi-bluetooth-manager

> Danavi Bluetooth Manager SDK for Web (Chrome only)

Latest version **0.1.5** (published 2021-01-19) · ISC license · 0 weekly downloads

## Install

```sh
npm install danavi-bluetooth-manager
pnpm add danavi-bluetooth-manager
yarn add danavi-bluetooth-manager
bun add danavi-bluetooth-manager
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2021-01-19 |
| First published | 2021-01-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 43.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Darren Muliawan |
| Maintainers | dialisa |

## Links

- npm: https://www.npmjs.com/package/danavi-bluetooth-manager
- npm.io page: https://npm.io/package/danavi-bluetooth-manager

## Dependencies (1)

- [@types/web-bluetooth](https://npm.io/package/@types/web-bluetooth.md) 0.0.9

## Recent versions

- 0.1.5 (latest) — 2021-01-19
- 0.1.4 — 2021-01-16
- 0.1.3 — 2021-01-15
- 0.1.2 — 2021-01-15
- 0.1.0 — 2021-01-14

## README

# Web DANAVI Bluetooth Manager (Chrome only)
This is a library to use DANAVI Bluetooth Manager API for Chrome. By using this library, your app can easily communicate with DANAVI compatible devices using Bluetooth.

## Installation
Install via ```npm```:

```npm install danavi-bluetooth-manager```

## Interfaces
### EmittedEvent
```
export interface IEmittedEvent {
    mac: string,
    action: string,
    deviceType: string,
    value: any
}
```

### OximeterMeasurement
```
export interface IOximeterMeasurement {
    bloodOxygen: number,
    heartRate: number,
    pi: number,
    timestamp: number
}
```

### OximeterTimer
```
export interface IOximeterTimer {
    remainingTime: number,
    timestamp: number
}
```

## DANAVI Bluetooth Manager API
### 1. Constructor
```
let bluetoothManager = new DanaviBluetoothManager()
```

### 2. Disconnect device
```
disconnectDevice: (mac: string) => void;
```
Cancel connection to device with MAC Address mac. After a device is disconnected, it will send ```{"mac": "TJlcHlDklRcLstDT8aGoUw==", "action": "ACTION_DEVICE_DISCONNECTED", "deviceType": "DANAVI Oximeter", "value": null})```

### 3. Scan and connect to DANAVI Oximeter
```
scanDanaviOximeter: () => void;
```
Call this function to start scanning for DANAVI Oximeter. A popup window will be opened on the top-left of your browser, select BerryMed, then click "Pair" to connect to the device. When the device is connected, the ```BluetoothManager``` will send ```{"mac": "TJlcHlDklRcLstDT8aGoUw==", "action": "ACTION_DEVICE_CONNECTED", "deviceType": "DANAVI Oximeter", "value": null}```.

### 4. Start DANAVI Oximeter measurement
```
startDanaviOximeterMeasurement: (writeToFile: boolean, withTimer: boolean, measurementLength: number, thinningInterval: number) => void;
```
Call this function to start oximeter measurement. Please wait for {"mac": "00:A0:50:6B:76:F4", "action": "ACTION_DEVICE_READY_TO_TAKE_MEASUREMENT", "deviceType": "DANAVI Oximeter", "value": null}, then prompt the user to take oximeter measurement.

On successful measurement, it will send {"mac": "TJlcHlDklRcLstDT8aGoUw==", "action": "ACTION_OXIMETER_LIVE_DATA", "deviceType": "DANAVI Oximeter", "value": {"bloodOxygen": "100", "heartRate": "79", "pi": "6", "timestamp": "1610677092388"}}

#### writeToFile

Setting this parameter to true will write the oximeter measurement to a temporary file with format ```Blood Oxygen Level, Heart Rate, Pi, Time in ms\n```. If the file is not deleted, then it will keep appending the same file across multiple measurement attempts. For example, the temporary file will look like this,

```
98,85,6,1608317817015\n
98,85,6,1608317817800\n
98,85,6,1608317818608\n
98,85,6,1608317819407\n
99,85,6,1608317820217\n
99,85,6,1608317821016\n
99,89,6,1608317821827\n
```

#### withTimer

Setting this parameter to true will set the measurement length for oximeter measurement. After some measurementLength seconds, it will stop sending the oximeter measurement event and stop writing oximeter measurement data to a temporary file. It will send the remaining measurement length in ```{"mac": "TJlcHlDklRcLstDT8aGoUw==", "action": "ACTION_OXIMETER_TIMER", "deviceType": "DANAVI Oximeter", "value": {"remainingTime": "4"}}```. When the timer hits 0, it will send ```{"mac": "TJlcHlDklRcLstDT8aGoUw==", "action": "ACTION_OXIMETER_MEASUREMENT_DONE", "deviceType": "DANAVI Oximeter", "value": null}``` to indicate that the oximeter measurement has finished.

#### measurementLength

Measurement length for oximeter measurement in seconds. If withTimer is false, then this parameter will not affect anything.

#### thinningInterval

thinningInterval is used for thinning the received oximeter measurement data. By default, the oximeter will send approximately 20 data/second. If the value is set to 20, it means that it only takes every 20th measurement (approximately 1 data/second).

### 5. Stop DANAVI Oximeter measurement
```
stopDanaviOximeterMeasurement: () => void;
```
Call this function to stop sending oximeter measurement. It will send ```{"mac": "TJlcHlDklRcLstDT8aGoUw==", "action": "ACTION_OXIMETER_MEASUREMENT_DONE", "deviceType": "DANAVI Oximeter", "value": null}``` to indicate that the oximeter measurement has finished.

### 6. Retrieve oximeter data from file
```
retrieveOximeterData: () => String;
```
Call this function to retrieve oximeter data from the temporary file. In order for this to work, must set ```writeToFile``` parameter in ```startDanaviOximeterMeasurement``` function to ```true```. On failure, it will return ```-1,-1,-1,0```.

### 7. Delete oximeter data file
```
deleteOximeterData: () => boolean;
```
Call this function to delete oximeter data temporary file. On successful delete, return ```true```.

### 8. Destroy
```
destroy: () => void;
```
Call this function when the ```DanaviBluetoothManager``` is no longer needed.

## Workflow

### 1. Import the library
import { DanaviBluetoothManager } from 'danavi-bluetooth-manager';

### 2. Initialize DanaviBluetoothManager class
```
let bluetoothManager = new DanaviBluetoothManager()
```

### 3. Subscribe to Event and register callbacks
Our API sends all type of events to ```EVENT_BLUETOOTH_DANAVI```. In the ```EmittedEvent``` class, there is an ```action``` field to indicate what type of event that you receive.

```
document.addEventListener(EVENT_BLUETOOTH_DANAVI, (e) => {
  let event: IEmittedEvent = e.detail;
  if (event.action === ACTION_DEVICE_CONNECTED) {
    onDeviceConnected(event);
  } else if (event.action === ACTION_DEVICE_DISCONNECTED) {
    onDeviceDisconnected(event);
  } else if (event.action === ACTION_DEVICE_FAILED_TO_CONNECT) {
    onDeviceFailedToConnect(event);
  } else if (event.action === ACTION_DEVICE_READY_TO_TAKE_MEASUREMENT) {
    onReadyTakeMeasurement(event);
  } else if (event.action === ACTION_OXIMETER_LIVE_DATA) {
    handleReceiveOximeterMeasurement(event);
  } else if (event.action === ACTION_OXIMETER_MEASUREMENT_DONE) {
    handleOximeterMeasurementDone(event);
  } else if (event.action === ACTION_OXIMETER_TIMER) {
    handleOximeterTimer(event);
  }
})
```

### 4. Scan for devices
Start scanning for nearby devices by using one of the scan functions. To scan DANAVI Oximeter,
```
danaviBluetoothManager.scanDanaviOximeter()
```
A popup window will be opened on the top-left of your browser, select BerryMed, then click "Pair" to connect to the device. When the device is connected, the ```BluetoothManager``` will send ```{"mac": "TJlcHlDklRcLstDT8aGoUw==", "action": "ACTION_DEVICE_CONNECTED", "deviceType": "DANAVI Oximeter", "value": null}```.

### 5. Start measurement
Once the device is connected to the browser, then can start listening for measurement event by using ```startDanaviOximeterMeasurement``` function. Wait until the app receives ```EmittedEvent``` with ```ACTION_DEVICE_READY_TO_TAKE_MEASUREMENT``` ```action```, then prompt the user to use the oximeter to take measurement.

Notification event
```{"mac": "TJlcHlDklRcLstDT8aGoUw==", "action": "ACTION_DEVICE_READY_TO_TAKE_MEASUREMENT", "deviceType": "DANAVI Oximeter", "value": null}```

### 6. Handle measurement event
After the user use the device, it will generate ```EmittedEvent``` that the app needs to handle. For example, this is the data coming from DANAVI Oximeter,
```{"mac": "00:A0:50:6B:76:F4", "action": "ACTION_OXIMETER_LIVE_DATA", "deviceType": "DANAVI Oximeter", "value": {"bloodOxygen": "100", "heartRate": "79", "pi": "6", "timestamp": "1610677092388"}}```

### 7. Cleanup
After receiving measurement, do a cleanup by calling ```disconnectDevice(mac)``` if the device is still connected, remove the listener by doing ```document.removeEventListener(EVENT_BLUETOOTH_DANAVI, callback);```, and call ```bluetoothManager.destroy()```.

## Sample Code on React
```
const OximeterPage: React.FC<Props> = (props) => {
    ...
    useEffect(() => {
        const callback = (e: any) => {
            let event: IEmittedEvent = e.detail;
            console.log(event);
            
            if (event.action === ACTION_DEVICE_CONNECTED) {
                onDeviceConnected(event);
            } else if (event.action === ACTION_DEVICE_DISCONNECTED) {
                onDeviceDisconnected(event);
            } else if (event.action === ACTION_DEVICE_FAILED_TO_CONNECT) {
                onDeviceFailedToConnect(event);
            } else if (event.action === ACTION_DEVICE_READY_TO_TAKE_MEASUREMENT) {
                onReadyTakeMeasurement(event);
            } else if (event.action === ACTION_OXIMETER_LIVE_DATA) {
                handleReceiveOximeterMeasurement(event);
            } else if (event.action === ACTION_OXIMETER_MEASUREMENT_DONE) {
                handleOximeterMeasurementDone(event);
            } else if (event.action === ACTION_OXIMETER_TIMER) {
                handleOximeterTimer(event);
            }
        }
        document.addEventListener(EVENT_BLUETOOTH_DANAVI, callback)
        return () => {
            document.removeEventListener(EVENT_BLUETOOTH_DANAVI, callback);
        }
    }, [])
    ...
}
```

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