Skip to main content
Module

x/simpleble/mod.ts>SimpleBLE

Bluetooth LE bindings for Node, Deno and Bun using SimpleBLE.
Latest
class SimpleBLE
implements Bindings
Re-export
import { SimpleBLE } from "https://deno.land/x/simpleble@v1.2.0/mod.ts";

An instance of a SimpleBLE interface.

Example

import { bindings } from "https://deno.land/x/webbluetooth@VERSION/mod.ts";
import { delay } from "https://deno.land/std@0.158.0/async/mod.ts";

const adaptersCount = bindings.simpleble_adapter_get_count();
if (adaptersCount === 0) {
  console.error("No Bluetooth adapters found");
  Deno.exit(1);
}

console.log(`Found ${adaptersCount} adapters`);
const adapter = bindings.simpleble_adapter_get_handle(0);

console.log(`Starting scan for 5 seconds`);
bindings.simpleble_adapter_scan_start(adapter);
await delay(5000);
bindings.simpleble_adapter_scan_stop(adapter);
console.log(`Finished scan`);

const count = bindings.simpleble_adapter_scan_get_results_count(adapter);
if (count === 0) {
  console.error("No devices found");
  Deno.exit(1);
}
console.log(`Found ${resultsCount} devices`);

Constructors

new
SimpleBLE()

Creates a new FFI instance.

Methods

destroy(): void

Releases all resources.

Returns the MAC address of an adapter.

Returns the number of adapters found.

Get a handle for an adapter.

Returns the number of paired devices.

Returns a handle to a paired device.

Returns the name of an adapter.

Release an adapter.

simpleble_adapter_scan_for(handle: Adapter, timeout: number): boolean

Begin scanning (with timeout).

NOTE: This function blocks until the timeout is reached.

Returns the number of devices found.

Returns a handle for a peripheral device.

Returns the scanning status.

Begin scanning.

Stop scanning.

simpleble_adapter_set_callback_on_scan_found(
handle: Adapter,
cb: (
adapter: Adapter,
peripheral: Peripheral,
userdata: UserData,
) => void
,
userdata?: UserData,
): boolean

Register a callback for when a peripheral is found.

simpleble_adapter_set_callback_on_scan_start(
handle: Adapter,
cb: (adapter: Adapter, userdata: UserData) => void,
userdata?: UserData,
): boolean

Register a callback for when scanning begins.

simpleble_adapter_set_callback_on_scan_stop_wrapper(
handle: Adapter,
cb: (adapter: Adapter, userdata: UserData) => void,
userdata?: UserData,
): boolean

Register a callback for when scanning stops.

simpleble_adapter_set_callback_on_scan_updated(
handle: Adapter,
cb: (
adapter: Adapter,
peripheral: Peripheral,
userdata: UserData,
) => void
,
userdata?: UserData,
): boolean

Register a callback for when an adapter is updated.

Deallocate memory for a SimpleBLE-C handle.

Returns the MAC address of a peripheral.

Begins connecting to a peripheral.

Disconnects from a peripheral.

Returns the ID of a peripheral.

simpleble_peripheral_indicate(
handle: Peripheral,
service: string,
characteristic: string,
cb: (
service: string,
characteristic: string,
data: Uint8Array,
userdata: UserData,
) => void
,
userdata?: UserData,
): boolean

Register a callback for peripheral indications.

Returns true if a peripheral can be connected to.

Returns true if a peripheral is currently connected.

Returns true if a peripheral is already paired.

Returns the size of the manufacturer data.

Returns the manufacturer data for device.

simpleble_peripheral_notify(
handle: Peripheral,
service: string,
characteristic: string,
cb: (
service: string,
characteristic: string,
data: Uint8Array,
) => void
,
): boolean

Register a callback for peripheral notifications.

simpleble_peripheral_read(
handle: Peripheral,
service: string,
characteristic: string,
): Uint8Array | undefined

Read data from a service characteristic.

simpleble_peripheral_read_descriptor(
handle: Peripheral,
service: string,
characteristic: string,
descriptor: string,
): Uint8Array | undefined

Read data from a descriptor.

Releases a peripheral device handle.

Returns the RSSI (signal strength) of a peripheral.

Returns the number of services a peripheral has.

Returns a Service found on a peripheral.

simpleble_peripheral_set_callback_on_connected(
handle: Peripheral,
cb: (peripheral: Peripheral, userdata: UserData) => void,
userdata?: UserData,
): boolean

Register a callback for when a peripheral is connected.

simpleble_peripheral_set_callback_on_disconnected(
handle: Peripheral,
cb: (peripheral: Peripheral, userdata: UserData) => void,
userdata?: UserData,
): boolean

Register a callback for when a peripheral is disconnected.

Unpair a peripheral.

simpleble_peripheral_unsubscribe(
handle: Peripheral,
service: string,
characteristic: string,
): boolean

Stop listening for characteristic value changes.

simpleble_peripheral_write_command(
handle: Peripheral,
service: string,
characteristic: string,
data: Uint8Array,
): boolean

Write data to a characteristic (response required).

simpleble_peripheral_write_descriptor(
handle: Peripheral,
service: string,
characteristic: string,
descriptor: string,
data: Uint8Array,
): boolean

Write data to a descriptor.

simpleble_peripheral_write_request(
handle: Peripheral,
service: string,
characteristic: string,
data: Uint8Array,
): boolean

Write data to a characteristic (no response).