Zum Inhalt springen

Getting Started

Dieser Inhalt ist in Ihrer Sprache noch nicht verfügbar.

Terminal window
bun add @capgo/capacitor-bluetooth-low-energy
bunx cap sync
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';

Initialize the BLE plugin. Must be called before any other method.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.initialize({ mode: 'central' });

Install the Capacitor Web Bluetooth shim on navigator.bluetooth. Call this manually before using the Web Bluetooth API from a Capacitor native app.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
BluetoothLowEnergy.shimWebBluetooth();

Check if Bluetooth is available on the device.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { available } = await BluetoothLowEnergy.isAvailable();

Check if Bluetooth is enabled on the device.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isEnabled();

Check if location services are enabled (Android only).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isLocationEnabled();

Open the app settings page.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openAppSettings();

Open the Bluetooth settings page (Android only).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openBluetoothSettings();

Open the location settings page (Android only).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openLocationSettings();

Check the current permission status.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { bluetooth, location } = await BluetoothLowEnergy.checkPermissions();

Request Bluetooth permissions.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { bluetooth, location } = await BluetoothLowEnergy.requestPermissions();

Start scanning for BLE devices.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startScan({
services: ['180D'], // Heart Rate Service
timeout: 10000
});

Stop scanning for BLE devices.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopScan();

Connect to a BLE device.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.connect({ deviceId: 'AA:BB:CC:DD:EE:FF' });

Disconnect from a BLE device.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.disconnect({ deviceId: 'AA:BB:CC:DD:EE:FF' });

Create a bond with a BLE device (Android only).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.createBond({ deviceId: 'AA:BB:CC:DD:EE:FF' });

Check if a device is bonded (Android only).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { bonded } = await BluetoothLowEnergy.isBonded({ deviceId: 'AA:BB:CC:DD:EE:FF' });

Discover services on a connected device.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.discoverServices({ deviceId: 'AA:BB:CC:DD:EE:FF' });

Get discovered services for a device.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { services } = await BluetoothLowEnergy.getServices({ deviceId: 'AA:BB:CC:DD:EE:FF' });

Get a list of connected devices.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { devices } = await BluetoothLowEnergy.getConnectedDevices();

Read a characteristic value.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { value } = await BluetoothLowEnergy.readCharacteristic({
deviceId: 'AA:BB:CC:DD:EE:FF',
service: '180D',
characteristic: '2A37'
});

Write a value to a characteristic.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.writeCharacteristic({
deviceId: 'AA:BB:CC:DD:EE:FF',
service: '180D',
characteristic: '2A39',
value: [0x01]
});

Start notifications for a characteristic.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startCharacteristicNotifications({
deviceId: 'AA:BB:CC:DD:EE:FF',
service: '180D',
characteristic: '2A37'
});

Stop notifications for a characteristic.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopCharacteristicNotifications({
deviceId: 'AA:BB:CC:DD:EE:FF',
service: '180D',
characteristic: '2A37'
});

Read a descriptor value.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { value } = await BluetoothLowEnergy.readDescriptor({
deviceId: 'AA:BB:CC:DD:EE:FF',
service: '180D',
characteristic: '2A37',
descriptor: '2902'
});

Write a value to a descriptor.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.writeDescriptor({
deviceId: 'AA:BB:CC:DD:EE:FF',
service: '180D',
characteristic: '2A37',
descriptor: '2902',
value: [0x01, 0x00]
});

Read the RSSI (signal strength) of a connected device.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { rssi } = await BluetoothLowEnergy.readRssi({ deviceId: 'AA:BB:CC:DD:EE:FF' });

Request MTU size change (Android only).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { mtu } = await BluetoothLowEnergy.requestMtu({
deviceId: 'AA:BB:CC:DD:EE:FF',
mtu: 512
});

Request connection priority (Android only).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.requestConnectionPriority({
deviceId: 'AA:BB:CC:DD:EE:FF',
priority: 'high'
});

Start advertising as a peripheral (BLE server).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startAdvertising({
name: 'MyDevice',
services: ['180D']
});

Stop advertising.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopAdvertising();

Start a foreground service to maintain BLE connections in background (Android only).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startForegroundService({
title: 'BLE Connection',
body: 'Maintaining connection...'
});

Stop the foreground service (Android only).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopForegroundService();

Initialization options for the plugin.

export interface InitializeOptions {
/**
* The mode to initialize the plugin in.
* - 'central': Act as a BLE central (client)
* - 'peripheral': Act as a BLE peripheral (server)
*
* @default 'central'
* @since 1.0.0
*/
mode?: 'central' | 'peripheral';
}

Result of the isAvailable method.

export interface IsAvailableResult {
/**
* Whether Bluetooth is available on the device.
*
* @since 1.0.0
*/
available: boolean;
}

Result of the isEnabled method.

export interface IsEnabledResult {
/**
* Whether Bluetooth is enabled on the device.
*
* @since 1.0.0
*/
enabled: boolean;
}

Result of the isLocationEnabled method.

export interface IsLocationEnabledResult {
/**
* Whether location services are enabled on the device.
*
* @since 1.0.0
*/
enabled: boolean;
}

Permission status for Bluetooth and location.

export interface PermissionStatus {
/**
* Bluetooth permission status.
*
* @since 1.0.0
*/
bluetooth: PermissionState;
/**
* Location permission status (Android only).
*
* @since 1.0.0
*/
location: PermissionState;
}

Options for starting a scan.

export interface StartScanOptions {
/**
* List of service UUIDs to filter by.
* Only devices advertising these services will be returned.
*
* @since 1.0.0
*/
services?: string[];
/**
* Scan timeout in milliseconds.
* Set to 0 for no timeout.
*
* @default 0
* @since 1.0.0
*/
timeout?: number;
/**
* Whether to allow duplicate scan results.
*
* @default false
* @since 1.0.0
*/
allowDuplicates?: boolean;
}

Options for connecting to a device.

export interface ConnectOptions {
/**
* The device ID (MAC address on Android, UUID on iOS).
*
* @since 1.0.0
*/
deviceId: string;
/**
* Whether to automatically connect when the device becomes available.
*
* @default false
* @since 1.0.0
*/
autoConnect?: boolean;
}

Options for disconnecting from a device.

export interface DisconnectOptions {
/**
* The device ID to disconnect from.
*
* @since 1.0.0
*/
deviceId: string;
}

Options for creating a bond.

export interface CreateBondOptions {
/**
* The device ID to bond with.
*
* @since 1.0.0
*/
deviceId: string;
}

Options for checking bond status.

export interface IsBondedOptions {
/**
* The device ID to check.
*
* @since 1.0.0
*/
deviceId: string;
}

Result of the isBonded method.

export interface IsBondedResult {
/**
* Whether the device is bonded.
*
* @since 1.0.0
*/
bonded: boolean;
}

Options for discovering services.

export interface DiscoverServicesOptions {
/**
* The device ID to discover services on.
*
* @since 1.0.0
*/
deviceId: string;
}

This page is generated from the plugin’s src/definitions.ts. Re-run the sync when the public API changes upstream.