指南
蓝牙低功耗教程
使用@capgo/capacitor-bluetooth-low-energy
Capacitor蓝牙低功耗插件用于BLE通信
安装
bun add @capgo/capacitor-bluetooth-low-energy
bunx cap sync
此插件暴露的内容
initialize- 初始化BLE插件。必须在调用任何其他方法之前调用。shimWebBluetooth- 安装Capacitor Web Bluetooth shim。navigator.bluetooth手动调用此方法之前使用API Web Bluetooth Capacitor native appisAvailable- 检查设备上是否可用蓝牙。isEnabled- 检查设备蓝牙是否已启用。
示例使用
initialize
初始化 BLE 插件。必须在调用任何其他方法之前。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.initialize({ mode: 'central' });
shimWebBluetooth
在设备上安装 Capacitor Web Bluetooth shim。 navigator.bluetooth在 Capacitor 原生应用中使用 Web Bluetooth API 前请手动调用此方法。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
BluetoothLowEnergy.shimWebBluetooth();
isAvailable
检查设备上是否可用蓝牙。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { available } = await BluetoothLowEnergy.isAvailable();
isEnabled
检查设备蓝牙是否已启用。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isEnabled();
完整参考
- GitHub: https://github.com/Cap-go/capacitor-bluetooth-low-energy/
- 文档: /docs/plugins/bluetooth-low-energy/