Zum Inhalt springen

Zum Starten

GitHub

Install

Install

Sie können unsere AI-gestützte Einrichtung verwenden, um das Plugin zu installieren. Fügen Sie den Capgo-Fähigkeiten Ihrer AI-Tool mithilfe der folgenden Befehl hinzu:

Terminalfenster
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

Dann verwenden Sie die folgende Anfrage:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-bluetooth-low-energy` plugin in my project.

Wenn Sie die manuelle Einrichtung bevorzugen, installieren Sie das Plugin, indem Sie die folgenden Befehle ausführen und folgen Sie den unten angegebenen plattform-spezifischen Anweisungen:

Terminalfenster
bun add @capgo/capacitor-bluetooth-low-energy
bunx cap sync

Importieren

Importieren
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';

Initialisiere das BLE-Plugin. Muss vor der Verwendung jeder anderen Methode aufgerufen werden.

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

Installiere das Capacitor Web Bluetooth Shim auf navigator.bluetooth. Rufe diese Funktion manuell auf, bevor du das Web Bluetooth API aus einer Capacitor nativen App verwendest.

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

Überprüfe, ob Bluetooth auf dem Gerät verfügbar ist.

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

Überprüfen, ob Bluetooth auf dem Gerät aktiviert ist.

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

Überprüfen, ob die Standortdienste auf dem Gerät aktiviert sind (nur Android).

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

Öffnen Sie die Einstellungen der App.

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

Öffnen Sie die Einstellungen für Bluetooth (nur Android).

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

Öffnen Sie die Einstellungen für die Standortverwaltung (nur Android).

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

Überprüfen Sie den aktuellen Status der Berechtigungen.

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

Berechtigungen für Bluetooth anfordern.

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

Scannen Sie nach BLE-Geräten.

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

BLE-Geräte abbrechen.

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

Zum BLE-Gerät verbinden.

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

Von einem BLE-Gerät trennen.

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

Mit einem BLE-Gerät einen Bond erstellen (nur Android).

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

Überprüfen, ob ein Gerät verbunden ist (nur für Android).

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

Entdecken Sie Dienste auf einem verbundenen Gerät.

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

Dienste für ein Gerät abrufen.

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

Abrufen einer Liste verbundener Geräte.

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

Ein Wert einer Eigenschaft lesen.

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

Ein Wert auf eine Eigenschaft schreiben.

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

Benachrichtigungen für eine Eigenschaft starten.

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

Benachrichtigungen für eine Eigenschaft beenden.

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

Ein Wert aus einem Beschreibungstyp lesen.

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'
});

Ein Wert auf einen Beschreibungstyp schreiben.

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]
});

Der RSSI-Wert (Signalstärke) eines verbundenen Geräts lesen.

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

Eine Änderung der MTU-Größe (nur Android) anfordern.

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

VerbindungsPriorität anfordern (nur Android).

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

Als Peripheriegerät als BLE-Server anzeigen.

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

Beenden des Advertiser.

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

Ein Hintergrunddienst starten, um BLE-Verbindungen im Hintergrund aufrechtzuerhalten (nur Android).

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

Stoppen Sie den Vordergrunddienst (nur Android).

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

Initialisierungsoptionen für das 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';
}

Resultat der Methode isAvailable.

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

Ergebnis der Methode isEnabled.

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

Ergebnis der Methode isLocationEnabled.

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

Zustand der Berechtigung für Bluetooth und Standort.

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

Optionen für den Start eines Scans.

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;
}

Optionen für die Verbindung zu einem Gerät.

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;
}

Optionen für die Trennung von einem Gerät.

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

Optionen für die Erstellung eines Bündels.

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

Optionen für die Überprüfung des Bündelstatus.

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

Ergebnis der Methode isBonded.

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

Optionen für die Entdeckung von Diensten.

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

Diese Seite wird aus dem Plugin generiert. src/definitions.tsRe-run die Synchronisation, wenn die öffentliche API upstream geändert wird.

If Sie __CAPGO_KEEP_0__ verwenden Anleitung zum Starten um das Dashboard und API-Operationen zu planen, verbinden Sie es mit Mit @capgo/capacitor-bluetooth-low-energy für die native Fähigkeit in Mit @capgo/capacitor-bluetooth-low-energy API-Übersicht für die Implementierungsdetails in API-Übersicht Einführung für die Implementierungsdetails in Einführung API-Schlüssel für die Implementierungsdetails in API-Schlüssel und Geräte für die Implementierungsdetails in Geräten.