An open API service indexing awesome lists of open source software.

https://github.com/dokmic/bluetooth-device

Bluetooth Low-Energy Peripheral Device
https://github.com/dokmic/bluetooth-device

abstraction bluetooth bluetooth-low-energy device node peripheral typescript

Last synced: about 2 months ago
JSON representation

Bluetooth Low-Energy Peripheral Device

Awesome Lists containing this project

README

          

# Bluetooth Low-Energy Peripheral Device
[![NPM](https://img.shields.io/npm/v/bluetooth-device.svg)](https://www.npmjs.com/package/bluetooth-device)
[![Tests](https://github.com/dokmic/bluetooth-device/actions/workflows/tests.yaml/badge.svg?branch=master)](https://github.com/dokmic/bluetooth-device/actions/workflows/tests.yaml)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

This package provides an abstraction layer on top of the Bluetooth Low-Energy (BLE) peripheral device.

## Get Started
```bash
npm install --save bluetooth-device
```

## Usage
```typescript
import { BluetoothDevice } from 'bluetooth-device';

const HANDLE_TEMPERATURE_READ = 1234;
const HANDLE_TEMPERATURE_WRITE = 5678;

const thermostat = new BluetoothDevice('11:22:33:44:55:66', { timeout: 1000 });
const temperature = await thermostat.read(HANDLE_TEMPERATURE_READ);

await thermostat.write(HANDLE_TEMPERATURE_WRITE, temperature + 10);
```

You can also extend your peripheral from the `BluetoothDevice` base class.
```typescript
import { BluetoothDevice } from 'bluetooth-device';

const TEMPERATURE_MIN = 10;
const TEMPERATURE_MAX = 40;

const HANLDE_READ = 1234;
const HANDLE_WRITE = 5678;

export class Thermostat extends BluetoothDevice {
async setTemperature(temperature: number): Promise {
const value = Math.min(Math.max(TEMPERATURE_MIN, temperature), TEMPERATURE_MAX);

return await this.write(HANDLE_WRITE, value);
}

getTemperature(): Promise {
return this.read(HANLDE_READ);
}
}
```

## API
### `constructor`
Initializes a Bluetooth device instance.

```typescript
constructor(address: string, options?: object)
```
- `address` - Bluetooth address.
- `options` - Device options.
- `discoveryTimeout` - Device discovery timeout in milliseconds. By default, it equals 30 seconds.
- `idleTimeout` - Device idle timeout in milliseconds. After that, the connection will be destroyed. By default, it equals to 2 minutes.
- `retries` - The number of retries on failed operations. By default, it is 3.
- `timeout` - Device operations timeout, namely, connection, read, and write. By default, it equals 10 seconds.

### `discover`
Discovers peripheral device by address.

```typescript
discover(): Promise
```

### `connect`
Tries to establish a connection with the device.
If the device was not previously discovered, the discovery operation will be performed before.

```typescript
connect(): Promise
```

### `disconnect`
Destroys a connection with the device.

```typescript
disconnect(): Promise
```

### `notify`
Waits for the next notification.
If the device was not previously connected, the connection will be established automatically.

```typescript
notify(handle: number): Promise
```
- `handle` - The notification handle.

### `read`
Reads data from the device.
If the device was not previously connected, the connection will be established automatically.

```typescript
read(handle: number): Promise
```
- `handle` - The handle to read from.

### `write`
Writes data to the device.
If the device was not previously connected, the connection will be established automatically.

```typescript
write(handle: number, data: Buffer): Promise
```
- `handle` - The write handle.
- `data` - The data buffer.