指南
如何使用蓝牙低功耗在Capacitor
使用@capgo/capacitor-蓝牙低功耗
Capacitor蓝牙低功耗插件用于BLE通信。
安装
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. Call this manually before using the Web Bluetooth API from a Capacitor native app. |
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 |
在已连接设备上发现服务。 |
getServices |
获取设备的已发现服务。 |
getConnectedDevices |
获取已连接设备的列表。 |
readCharacteristic |
读取特性值。 |
writeCharacteristic |
将值写入特性。 |
startCharacteristicNotifications |
为特性启动通知。 |
stopCharacteristicNotifications |
停止特性通知。 |
readDescriptor |
读取描述符值。 |
writeDescriptor |
将值写入描述符。 |
readRssi |
读取已连接设备的 RSSI(信号强度)。 |
requestMtu |
请求MTU大小更改(仅Android)。 |
requestConnectionPriority |
请求连接优先级(仅Android)。 |
startAdvertising |
作为BLE服务器开始广播(BLE服务器)。 |
stopAdvertising |
停止广播。 |
addGattService |
在本地GATT服务器中添加一个GATT服务及其特性。必须在开始广播之前在外设模式下调用。 |
removeGattService |
从本地GATT服务器中删除一个GATT服务。 |
setGattCharacteristicValue |
设置本地GATT特性的值。 |
notifyGattCharacteristicChanged |
通知已连接的中央设备本地GATT特性值已更改。 |
startForegroundService |
开始前台服务以在后台维持BLE连接(仅Android)。 |
stopForegroundService |
停止前台服务(仅Android)。 |
示例
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 前,手动调用此 shim。
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 个核心方法。有关监听器和版本辅助以及每个方法的完整契约的文档,请参见 __CAPGO_KEEP_0__ 仓库。 GitHub 仓库.
返回句柄。调用
addListener 当屏幕卸载时,或 handle.remove() 以清除所有监听器。 BluetoothLowEnergy.removeAllListeners() 在 __CAPGO_KEEP_0__ native app 中使用 Web Bluetooth __CAPGO_KEEP_0__ 前,手动调用此 shim。
全参考
继续使用 @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插件由Capgo的实现细节 添加或更新插件 为添加或更新插件的实现细节