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
- Host: GitHub
- URL: https://github.com/dokmic/bluetooth-device
- Owner: dokmic
- Created: 2021-01-16T20:28:31.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-11-18T21:02:41.000Z (over 4 years ago)
- Last Synced: 2025-03-07T21:43:13.802Z (over 1 year ago)
- Topics: abstraction, bluetooth, bluetooth-low-energy, device, node, peripheral, typescript
- Language: TypeScript
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Bluetooth Low-Energy Peripheral Device
[](https://www.npmjs.com/package/bluetooth-device)
[](https://github.com/dokmic/bluetooth-device/actions/workflows/tests.yaml)
[](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.