跳过主要内容
返回插件
@capgo/capacitor-低功耗蓝牙
教程
由 github.com/Cap-go

低功耗蓝牙

支持扫描、连接、读取、写入和接收蓝牙设备通知的全功能BLE插件

指南

蓝牙低功耗教程

使用@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 app
  • isAvailable - 检查设备上是否可用蓝牙。
  • 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();

完整参考