指南
如何在 Capacitor 中使用 Bluetooth Low Energy
使用 @capgo/capacitor-bluetooth-low-energy
用于 BLE 通信的 Capacitor Bluetooth Low Energy 插件。
安装
bun add @capgo/capacitor-bluetooth-low-energy
bunx cap sync
bunx cap sync 将插件的原生 code 复制到你的原生项目中。每次升级插件后都要重新运行。
导入
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
API 概览
| 方法 | 描述 |
|---|---|
initialize |
初始化 BLE 插件。必须在调用任何其他方法之前执行。 |
shimWebBluetooth |
在 Capacitor 上安装 Web Bluetooth shim navigator.bluetooth。在从 Capacitor 原生应用使用 Web Bluetooth API 之前,请手动调用此方法。 |
isAvailable |
检查设备是否支持蓝牙。 |
isEnabled |
检查设备是否已启用蓝牙。 |
isLocationEnabled |
检查是否已启用定位服务(仅限 Android)。 |
openAppSettings |
打开应用设置页面。 |
openBluetoothSettings |
打开蓝牙设置页面 (仅限 Android)。 |
openLocationSettings |
打开位置设置页面 (仅限 Android)。 |
checkPermissions |
检查当前的权限状态。 |
requestPermissions |
请求蓝牙权限。 |
startScan |
开始扫描 BLE 设备。 |
stopScan |
停止扫描 BLE 设备。 |
connect |
连接到 BLE 设备。 |
disconnect |
断开与 BLE 设备的连接。 |
createBond |
与 BLE 设备建立配对 (仅限 Android)。 |
isBonded |
检查设备是否已配对 (仅限 Android)。 |
discoverServices |
发现已连接设备的 services。 |
getServices |
获取设备的已发现 services。 |
getConnectedDevices |
获取已连接设备列表。 |
readCharacteristic |
读取特征值。 |
writeCharacteristic |
向特征值写入数据。 |
startCharacteristicNotifications |
开启特征值通知。 |
stopCharacteristicNotifications |
停止特征值的通知。 |
readDescriptor |
读取描述符值。 |
writeDescriptor |
向描述符写入值。 |
readRssi |
读取已连接设备的 RSSI(信号强度)。 |
requestMtu |
请求更改 MTU 大小(仅限 Android)。 |
requestConnectionPriority |
请求连接优先级(仅限 Android)。 |
startAdvertising |
作为外设(BLE server)开始广播。 |
stopAdvertising |
停止广播。 |
addGattService |
向本地 GATT server 添加带有 characteristic 的 GATT service。必须在 peripheral 模式下开始 advertising 之前调用。 |
removeGattService |
从本地 GATT server 中移除 GATT service。 |
setGattCharacteristicValue |
设置本地 GATT characteristic 的值。 |
notifyGattCharacteristicChanged |
当本地 GATT characteristic 的值发生变化时,通知已连接的 central。 |
startForegroundService |
启动前台服务以在后台维持 BLE 连接(仅限 Android)。 |
stopForegroundService |
停止前台服务(仅限 Android)。 |
示例
initialize()
初始化 BLE plugin。必须在调用任何其他方法之前执行。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.initialize({ mode: 'central' });
shimWebBluetooth()
安装 Capacitor Web Bluetooth shim navigator.bluetooth。在 Capacitor native app 中使用 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();
isLocationEnabled()
检查是否已启用定位服务(仅限 Android)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isLocationEnabled();
openAppSettings()
打开应用设置页面。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openAppSettings();
上表列出了 36 个核心方法。有关监听器、版本辅助函数以及每个方法的完整规范,请参阅 GitHub 仓库.
监听事件
addListener 会返回一个句柄。调用 handle.remove() 在屏幕卸载时,或者调用 BluetoothLowEnergy.removeAllListeners() 以清除所有监听器。
完整参考文档
继续阅读:使用 @capgo/capacitor-bluetooth-low-energy
如果你正在使用 使用 @capgo/capacitor-bluetooth-low-energy 要规划原生插件工作,请将其与 @capgo/capacitor-bluetooth-low-energy 了解 @capgo/capacitor-bluetooth-low-energy 中的实现细节, 快速入门 有关“入门指南”中的实现细节, Capgo 插件目录 查看 Capgo 插件目录中的产品工作流, Capgo 提供的 Capacitor 插件 有关 Capgo 开发的 Capacitor 插件的实现细节,以及 添加或更新插件 有关添加或更新插件的实现细节。