快速入门
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-bluetooth-low-energy`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/bluetooth-low-energy/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
“安装”章节你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下 prompt:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-bluetooth-low-energy` plugin in my project.如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:
bun add @capgo/capacitor-bluetooth-low-energynpx cap sync导入
“导入”章节import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';快速入门
章节标题为“快速入门”import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.initialize({ mode: 'central' });示例:shimWebBluetooth
标题为“示例:shimWebBluetooth”的章节import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
BluetoothLowEnergy.shimWebBluetooth();示例:isAvailable
标题为“示例:isAvailable”的章节import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { available } = await BluetoothLowEnergy.isAvailable();常见坑点
章节标题为“常见陷阱”- 权限: 调用
checkPermissions()然后requestPermissions()在需要时。 - 监听器: 在页面卸载时移除句柄。
- 初始化一次: 在应用启动时运行 setup。
API 概览
章节标题:“API 概览”initialize
“initialize” 章节初始化 BLE 插件。
除以下情况外,必须在调用任何其他方法之前执行: addListener()。注册 adapterStateChanged 在调用之前 initialize() 以免错过 iOS 的初始状态事件。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.initialize({ mode: 'central' });shimWebBluetooth
“shimWebBluetooth” 部分在 Capacitor 上安装 Web Bluetooth shim navigator.bluetooth。
在从 Capacitor 原生应用中使用 Web Bluetooth API 之前,请手动调用此方法。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
BluetoothLowEnergy.shimWebBluetooth();isAvailable
标题为 “isAvailable” 的章节检查设备上的蓝牙是否可用。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { available } = await BluetoothLowEnergy.isAvailable();isEnabled
“isEnabled” 章节检查设备蓝牙是否已开启。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isEnabled();isLocationEnabled
“isLocationEnabled” 部分检查是否已启用定位服务(仅限 Android)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isLocationEnabled();openAppSettings
“openAppSettings” 部分打开应用设置页面。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openAppSettings();openBluetoothSettings
标题为“openBluetoothSettings”的章节打开蓝牙设置页面(仅限 Android)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openBluetoothSettings();openLocationSettings
标题为“openLocationSettings”的章节打开位置设置页面(仅限 Android)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openLocationSettings();checkPermissions
“checkPermissions” 章节检查当前权限状态。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { bluetooth, location } = await BluetoothLowEnergy.checkPermissions();requestPermissions
“requestPermissions” 章节请求蓝牙权限。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { bluetooth, location } = await BluetoothLowEnergy.requestPermissions();startScan
“startScan” 章节开始扫描 BLE 设备。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startScan({ services: ['180D'], // Heart Rate Service timeout: 10000});stopScan
“stopScan” 章节停止扫描 BLE 设备。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopScan();connect
“connect” 章节连接到 BLE 设备。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.connect({ deviceId: 'AA:BB:CC:DD:EE:FF' });disconnect
“disconnect” 章节断开与 BLE 设备的连接。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.disconnect({ deviceId: 'AA:BB:CC:DD:EE:FF' });createBond
“createBond” 部分与 BLE 设备建立绑定(仅限 Android)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.createBond({ deviceId: 'AA:BB:CC:DD:EE:FF' });isBonded
“isBonded” 部分检查设备是否已绑定(仅限 Android)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { bonded } = await BluetoothLowEnergy.isBonded({ deviceId: 'AA:BB:CC:DD:EE:FF' });discoverServices
“discoverServices” 部分发现已连接设备上的服务。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.discoverServices({ deviceId: 'AA:BB:CC:DD:EE:FF' });getServices
“getServices” 部分获取设备已发现的服务。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { services } = await BluetoothLowEnergy.getServices({ deviceId: 'AA:BB:CC:DD:EE:FF' });getConnectedDevices
“getConnectedDevices” 章节获取已连接设备列表。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { devices } = await BluetoothLowEnergy.getConnectedDevices();readCharacteristic
“readCharacteristic” 章节读取特征值。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { value } = await BluetoothLowEnergy.readCharacteristic({ deviceId: 'AA:BB:CC:DD:EE:FF', service: '180D', characteristic: '2A37'});writeCharacteristic
“writeCharacteristic” 章节向特征值写入数据。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.writeCharacteristic({ deviceId: 'AA:BB:CC:DD:EE:FF', service: '180D', characteristic: '2A39', value: [0x01]});startCharacteristicNotifications
“startCharacteristicNotifications” 章节开启特征值通知。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startCharacteristicNotifications({ deviceId: 'AA:BB:CC:DD:EE:FF', service: '180D', characteristic: '2A37'});stopCharacteristicNotifications
“stopCharacteristicNotifications” 章节停止某个特征的通知。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopCharacteristicNotifications({ deviceId: 'AA:BB:CC:DD:EE:FF', service: '180D', characteristic: '2A37'});readDescriptor
“readDescriptor” 章节读取描述符的值。
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'});writeDescriptor
“writeDescriptor” 章节向描述符写入一个值。
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]});readRssi
“readRssi” 章节读取已连接设备的 RSSI(信号强度)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { rssi } = await BluetoothLowEnergy.readRssi({ deviceId: 'AA:BB:CC:DD:EE:FF' });requestMtu
标题为“requestMtu”的章节请求更改 MTU 大小(仅限 Android)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { mtu } = await BluetoothLowEnergy.requestMtu({ deviceId: 'AA:BB:CC:DD:EE:FF', mtu: 512});requestConnectionPriority
标题为“requestConnectionPriority”的章节请求连接优先级(仅限 Android)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.requestConnectionPriority({ deviceId: 'AA:BB:CC:DD:EE:FF', priority: 'high'});startAdvertising
“startAdvertising” 部分以外围设备(BLE 服务器)身份开始广播。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startAdvertising({ name: 'MyDevice', services: ['180D']});stopAdvertising
标题为“stopAdvertising”的章节停止广播。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopAdvertising();startForegroundService
标题为 “startForegroundService” 的章节启动前台服务以在后台维持 BLE 连接(仅限 Android)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startForegroundService({ title: 'BLE Connection', body: 'Maintaining connection...'});stopForegroundService
标题为 “stopForegroundService” 的章节停止前台服务(仅限 Android)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopForegroundService();addListener('adapterStateChanged')
标题为 “addListener('adapterStateChanged')” 的章节监听蓝牙适配器电源状态变化。此事件在 iOS 和 Android 上触发,在 Web 上不会触发。自 8.2.11 版本起可用。
import { BluetoothLowEnergy, AdapterStateChangedEvent } from '@capgo/capacitor-bluetooth-low-energy';
const handle = await BluetoothLowEnergy.addListener( 'adapterStateChanged', (event: AdapterStateChangedEvent) => { console.log('Bluetooth enabled:', event.enabled); },);
// later:await handle.remove();负载为 { enabled: boolean } 。 enabled 为 true 当适配器开启时,以及 false 当它关闭时。该插件不会添加其他字段。
触发时机:
- Android: 当系统报告
BluetoothAdapter.STATE_OFF(enabled: false) 或STATE_ON(enabled: true) 时。中间状态(如正在开启或正在关闭)不会触发此事件。当前状态不会在initialize()时发送,仅在适配器随后变为关闭或开启状态时发送。 - iOS: 当当前模式下的 Core Bluetooth 管理器报告新状态,且
enabled自上次触发以来已发生变化时。enabled为true仅当该管理器的状态为.poweredOn时。其他状态(如关闭、重置、未授权等)均报告为enabled: false。在central在peripheral模式下,源为主管理器。在initialize()模式下,源为从管理器(主管理器回调不会触发)。在initialize()期间创建管理器也会报告当前状态。该初始状态回调可能在initialize()解析之前触发,且插件不会保留该事件,因此之后注册的监听器可能会错过它,只能听到后续变化。请在调用enabled: false然后enabled: true之前注册监听器。如果管理器初始状态不是.poweredOn,随后变为开启状态,iOS 可能会触发initialize()。在isEnabled()解析后,调用
以读取当前适配器状态。 centralDisconnected 当蓝牙关闭时,插件还会丢弃其持有的本地 GATT 服务和特征记录,并为每个跟踪的主管理器触发 adapterStateChanged 配合 enabled: false。在 Android 上,它还会关闭 GATT 服务器并清除广播回调。该插件不会自行恢复服务或广播。
如果你的应用是外设,请等待 adapterStateChanged 配合 enabled: true模式,然后调用 addGattService 再次为每个服务执行,并 startAdvertising 再次执行。由于插件已清除本地记录,添加相同的 Service UUID 会被接受,而不是被拒绝并提示“Service already exists”。在 Android 上,当适配器恢复时,缺失的扫描器(central 模式)或广播器(peripheral 模式)会被重新获取。你仍然需要自行添加服务并启动广播。
类型参考
“类型参考” 章节InitializeOptions
标题为“InitializeOptions”的章节插件的初始化选项。
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';}IsAvailableResult
标题为“IsAvailableResult”的章节isAvailable 方法的结果。
export interface IsAvailableResult { /** * Whether Bluetooth is available on the device. * * @since 1.0.0 */ available: boolean;}IsEnabledResult
“IsEnabledResult” 章节isEnabled 方法的结果。
export interface IsEnabledResult { /** * Whether Bluetooth is enabled on the device. * * @since 1.0.0 */ enabled: boolean;}IsLocationEnabledResult
标题为 “IsLocationEnabledResult” 的章节isLocationEnabled 方法的结果。
export interface IsLocationEnabledResult { /** * Whether location services are enabled on the device. * * @since 1.0.0 */ enabled: boolean;}PermissionStatus
标题为“PermissionStatus”的章节蓝牙和位置权限状态。
export interface PermissionStatus { /** * Bluetooth permission status. * * @since 1.0.0 */ bluetooth: PermissionState;
/** * Location permission status (Android only). * * @since 1.0.0 */ location: PermissionState;}StartScanOptions
“StartScanOptions” 章节启动扫描时的配置选项。
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;}ConnectOptions
“ConnectOptions” 章节连接设备时的配置选项。
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;}DisconnectOptions
“DisconnectOptions” 章节断开设备连接时的配置选项。
export interface DisconnectOptions { /** * The device ID to disconnect from. * * @since 1.0.0 */ deviceId: string;}CreateBondOptions
“CreateBondOptions” 章节创建绑定时的配置选项。
export interface CreateBondOptions { /** * The device ID to bond with. * * @since 1.0.0 */ deviceId: string;}IsBondedOptions
标题为 “IsBondedOptions” 的章节用于检查绑定状态的选项。
export interface IsBondedOptions { /** * The device ID to check. * * @since 1.0.0 */ deviceId: string;}IsBondedResult
标题为 “IsBondedResult” 的章节isBonded 方法的结果。
export interface IsBondedResult { /** * Whether the device is bonded. * * @since 1.0.0 */ bonded: boolean;}DiscoverServicesOptions
标题为 “DiscoverServicesOptions” 的章节用于发现服务的选项。
export interface DiscoverServicesOptions { /** * The device ID to discover services on. * * @since 1.0.0 */ deviceId: string;}AdapterStateChangedEvent
标题为 “AdapterStateChangedEvent” 的章节当蓝牙适配器开启或关闭时触发的事件。自 8.2.11 版本起可用。
export interface AdapterStateChangedEvent { /** * Whether Bluetooth is enabled (adapter powered on). * * @since 8.2.11 */ enabled: boolean;}单一事实来源
标题为“单一事实来源”的部分本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。
从入门指南继续
“从入门指南继续”章节如果你正在使用 快速入门 要规划 dashboard 和 API 操作,请将其连接到 使用 @capgo/capacitor-bluetooth-low-energy 在“使用 @capgo/capacitor-bluetooth-low-energy”中的原生功能 API 概览 关于 API Overview 中的实现细节, 简介 关于 Introduction 中的实现细节, API 密钥 有关 API Keys 的实现细节,以及 设备 有关 Devices 的实现细节。