跳过主要内容

蓝牙低功耗 Capacitor插件

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

安装

bun add @capgo/capacitor-bluetooth-low-energy bunx cap sync
npm、pnpm或yarn
  • npm install @capgo/capacitor-bluetooth-low-energy
  • pnpm add @capgo/capacitor-bluetooth-low-energy
  • yarn add @capgo/capacitor-bluetooth-low-energy

指南

如何使用蓝牙低功耗在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的实现细节 添加或更新插件 为添加或更新插件的实现细节

常见问题

蓝牙低功耗插件常见问题

如何在Capacitor应用中安装蓝牙低功耗插件?

运行“bun add @capgo/capacitor-蓝牙低功耗”(或“npm install @capgo/capacitor-蓝牙低功耗”),然后运行“bunx cap sync”,使iOS和Android项目捕获到本地code。从“@capgo/capacitor-蓝牙低功耗”中导入它到您的code应用中。

@capgo/capacitor-蓝牙低功耗与React、Vue和Angular兼容吗?

是的。蓝牙低功耗是一个Capacitor包,因此它与任何运行在Capacitor中的Web框架兼容,包括Ionic、React、Vue、Angular、Svelte和普通JavaScript。

哪个Capacitor版本支持@capgo/capacitor-蓝牙低功耗?

Capgo插件遵循Capacitor的主要版本:使用与Capacitor主要版本匹配的插件主要版本(例如插件v8与Capacitor 8)。在GitHub README中列出的兼容性表格列出了维护的版本。

是否@capgo/capacitor-蓝牙低功耗免费且开源?

是。源码code在GitHub上公开,https://github.com/Cap-go/capacitor-蓝牙低功耗/,并且可以从npm免费安装。欢迎bug报告和pull请求。

是否可以在不经过App Store审核的情况下更新code使用蓝牙低功耗?

安装或升级插件会改变原生code,因此需要新建一个商店构建。之后,使用Capgo实时更新的JavaScript、HTML和CSS代码可以立即部署。

不必等待应用程序审查就可以将蓝牙低功耗更改推送到商店

一旦插件在您的商店构建中,Capgo实时更新将将您的JavaScript、HTML和CSS更改推送给用户,仅需几分钟。

从Capgo开始