@e-is/capacitor-bluetooth-serial

中文文档
6.0.3发布于 2025/01/01MITTypeScriptESM + CJS零依赖近 30 天下载 1.1万

电容蓝牙串行插件

中文使用文档
文档目录67 节
  1. 使用量
  2. API 文档
  3. 方法
  4. 可启用
  5. 说明
  6. 参数
  7. 快速示例
  8. 可启用
  9. 说明
  10. 参数
  11. 快速示例
  12. 启用
  13. 说明
  14. 参数
  15. 快速示例
  16. 禁用
  17. 说明
  18. 参数
  19. 快速示例
  20. 启动
  21. 说明
  22. 参数
  23. 快速示例
  24. 停止启用通知
  25. 说明
  26. 参数
  27. 快速示例
  28. 扫描
  29. 说明
  30. 参数
  31. 快速示例
  32. 连接
  33. 说明
  34. 参数
  35. 快速示例
  36. 连接不安全
  37. 说明
  38. 参数
  39. 快速示例
  40. 断开
  41. 说明
  42. 参数
  43. 快速示例
  44. 已连接
  45. 说明
  46. 参数
  47. 快速示例
  48. 读取
  49. 说明
  50. 参数
  51. 快速示例
  52. 读取直到
  53. 说明
  54. 参数
  55. 快速示例
  56. 开始通知
  57. 说明
  58. 参数
  59. 快速示例
  60. 通知
  61. 说明
  62. 参数
  63. 快速示例
  64. 写入
  65. 说明
  66. 参数
  67. 快速示例

电容蓝牙串行插件

从 电容-蓝牙相继 (存档)

使用 Capacitor 与 Bluetooth (serial) 互动的客户端执行 .

支持的平台

  • 网页
  • 机器人
  • iOS系统

支持的电容版本 :

  • 电容 6: 使用版本 6.x
  • 电容 5 : 使用版本 5.x
  • 电容器4:使用0.6.x版本
  • 请使用 电容-蓝牙相继 页:1

使用量

通过 npm 安装插件

npm install --save @e-is/capacitor-bluetooth-serial
import {BluetoothSerial} from "@e-is/capacitor-bluetooth-serial";

//...do something with plugin

API 文档

可找到接口和类型定义 这里。 。 。 .

API 密码

方法

可启用

如果启用了蓝牙报告 .

isEnabled(): Promise<BluetoothState>;

说明

函数 isEnabled 将成功称为无论蓝牙是否启用。 承诺将包含一个属性 enabled 显示是否启用了蓝牙或 没有 启用 。 只有在出现错误时才会调用失败回调 .

如果您想在之后启用蓝牙, 您可以直接使用 #enable, 一旦 #enable 也可以检查蓝牙是否被切入 .

参数

无.

快速示例

BluetoothSerial
  .isEnabled()
  .then((response: BluetoothState) => {
    const status = response.enabled ? 'enabled' : 'disabled';
    console.log(`Bluetooth is ${status}`);
  })
  .catch(() => {
    console.log('Error checking bluetooth status');
  });

可启用

如果能够启用蓝牙, 则报告 .

canEnable(): Promise<BluetoothState>;

说明

函数 canEnable 将成功称为无论蓝牙能否启用。 承诺将包含一个属性 enabled 表示是否可以启用蓝牙或 没有。只有在错误发生时,才会调用失败回调.

参数

无.

快速示例

BluetoothSerial
  .canEnable()
  .then((response: BluetoothState) => {
    const canEnable = response.enabled;
    console.log(`Bluetooth can be enabled ? ${canEnable}`);
  })
  .catch(() => {
    console.log('Error checking if bluetooth can be enabled');
  });

启用

未启用时启用蓝牙 。 如有必要,还请求蓝牙访问权限.

enable(): Promise<BluetoothState>;

说明

函数 enable 调用成功与否的蓝牙。 承诺将包含一个属性 enabled 显示是否启用了蓝牙或 没有 进程后启用。 只有在出现错误时才会调用失败回调 .

如果该应用没有使用蓝牙的许可,它会请求.

参数

无.

快速示例

BluetoothSerial
  .enable()
  .then((response: BluetoothState) => {
    const status = response.enabled ? 'enabled' : 'disabled';
    console.log(`Bluetooth is ${status}`);
  })
  .catch(() => {
    console.log('Error enabling bluetooth');
  });

禁用

启用时禁用蓝牙 。 如有必要,还请求蓝牙访问权限.

disable(): Promise<BluetoothState>;

说明

函数 disable 将成功称为蓝牙是否成功禁用. 承诺将包含一个属性 enabled 显示是否启用了蓝牙或 没有 进程后启用. 只有在出现错误时才会调用失败回调 .

如果该应用没有使用蓝牙的许可,它会请求.

参数

无.

快速示例

BluetoothSerial
  .disable()
  .then((response: BluetoothState) => {
    const status = response.enabled ? 'enabled' : 'disabled';
    console.log(`Bluetooth is ${status}`);
  })
  .catch(() => {
    console.log('Error disabling bluetooth');
  });

启动

蓝牙状态改变时通知 .

startEnabledNotifications(): Promise<void>· ;

说明

函数 startEnabledNotifications 启用状态通知 。 为了获取这些值, 使用“ 无法更改的” 事件倾听器作为事件名称 .

const eventListener = BluetoothSerial.addListener('onEnabledChanged' , (response: BluetoothState) => {

    const { enabled } = response;
    //Do something with enabled variable
});

参数

无.

快速示例

BluetoothSerial
  .startEnabledNotifications()
  .then(() => {
      eventListener = BluetoothSerial.addListener('onEnabledChanged', (data: BluetoothState) => {
        console.log('Bluetooth state changed to ' + (data.enabled ? 'Enabled' : 'Disabled'));
      });
  })
  .catch(() => {
    console.log('Error starting enabled listener');
  });

停止启用通知

停止价值变化的传播.

stopEnabledNotifications(): Promise<void>· ;

说明

函数 stopEnabledNotifications 禁用蓝牙状态通知 。 此外,事件听众必须删除.

eventListener.remove();

参数

无.

快速示例

BluetoothSerial
  .stopEnabledNotifications()
  .then(() => {
      eventListener.remove();
  })
  .catch(() => {
    console.log('Error disabling listener for device');
  });

扫描

发现设备可见并接近设备

scan(): Promise<BluetoothScanResult>;

说明

机器人

函数 scan 发现靠近设备并可见的蓝牙设备. 成功回调被调用一个类似对象列表 list,或者如果找不到设备,则列表为空.

示例列表传递给成功回调.

[{
    "class": 0,
    "id": "00:11:22:33:44:55",
    "address": "00:11:22:33:44:55",
    "name": "Device 1"
}, {
    "class": 7936,
    "id": "01:23:6645:4D67:89:00",
    "address": "01:23:6645:4D67:89:00",
    "name": "Device 2"
}]

发现过程需要一段时间. 您可能想要在等待发现的 proces 完成时显示进度指标, 并引用 supess 回调 .

通话 connect 在未发作的蓝牙设备上,应该开始Android配对过程.

参数

无.

快速示例

BluetoothSerial
  .scan()
  .then((result: BluetoothScanResult) => {
    result.devices.forEach((device: BluetoothDevice) {
        console.log(device.id);
    });
  })
  .catch(() => {
    console.log('Error scanning devices');
  });

连接

连接到蓝牙设备 .

connect(options: BluetoothConnectOptions): Promise<void>· ;

说明

函数 connect 连接到蓝牙设备。 连接成功时, 调用成功将被调用 。 如果连接失败, 则称为失败 .

机器人

对于Android, connect 使用远程设备的MAC地址.

参数

  • { 地址 }: 远程设备的标识符.

快速示例

BluetoothSerial
  .connect({
    address: '00:11:22:33:44:55',
  })
  .then(() => {
    console.log('Successfully connected')
  })
  .catch(() => {
    console.log('Error connecting...');
  });

连接不安全

连接到蓝牙设备.

connectInsecure(options: BluetoothConnectOptions): Promise<void>· ;

说明

函数 connectInsecure 连接到蓝牙设备。 连接成功时, 调用成功将被调用 。 如果连接失败, 则称为失败 . 函数 connectInsecure 喜欢 蓝牙系列. connect,但与蓝牙设备产生不安全的连接。 见 Android 文档 更多信息.

机器人

对于Android, connectInsecure 使用远程设备的MAC地址.

参数

  • { 地址 }: 远程设备的标识符.

快速示例

BluetoothSerial
  .connectInsecure({
    address: '00:11:22:33:44:55',
  })
  .then(() => {
    console.log('Successfully connected')
  })
  .catch(() => {
    console.log('Error connecting...');
  });

断开

断开蓝牙设备 .

disconnect(options: BluetoothConnectOptions): Promise<void>· ;

说明

函数 disconnect 断开蓝牙设备。 断开成功时将调用回调成功 。 如果断开失败, 则称为失败 .

机器人

对于Android, disconnect 使用远程设备的MAC地址.

3⁄4 ̄ ̧漯B:如果不通过地址,所有设备都将被断开.

参数

  • { 地址 }: 远程设备的标识符.

快速示例

BluetoothSerial
  .disconnect({
    address: '00:11:22:33:44:55',
  })
  .then(() => {
    console.log('Successfully disconnected')
  })
  .catch(() => {
    console.log('Error disconnecting...');
  });

已连接

报告联系情况.

isConnected(options: BluetoothConnectOptions): Promise<BluetoothConnectResult>· ;

说明

函数 isConnected 调用连接状态下的成功回调(连接或未连接)。 只有在错误发生时才会称为失败 .

机器人

对于Android, isConnected 使用远程设备的MAC地址.

参数

  • { 地址 }: 远程设备的标识符.

快速示例

BluetoothSerial
  .isConnected({
    address: '00:11:22:33:44:55',
  })
  .then((result: BluetoothConnectResult) => {
    const status = result.connected ? 'connected' : 'disconnected';
    console.log(`Device is ${status}`);
  })
  .catch(() => {
    console.log('Error checking connection status');
  });

读取

从缓冲器读取数据.

read(options: BluetoothReadOptions): Promise<BluetoothReadResult>· ;

说明

函数 read 读取缓冲器中的数据。 数据作为字符串传递给成功的回调. 通话 read 当没有数据可用时,会将空字符串传递到回调中.

参数

  • { 地址 }: 远程设备的标识符.

快速示例

BluetoothSerial
  .read({
    address: '00:11:22:33:44:55',
  })
  .then((result: BluetoothReadResult) => {
    console.log(result.data);
  })
  .catch(() => {
    console.log('Error reading data from device');
  });

读取直到

从缓冲器读取数据,直到其到达分隔器.

readUntil(options: BluetoothReadUntilOptions): Promise<BluetoothReadResult>· ;

说明

函数 readUntil 从缓冲器读取数据,直到它到达了分隔器。 数据作为字符串传递给成功的回调. 如果缓冲器不包含分隔符,则将空字符串传递给回调符.

参数

  • { 地址 }: 远程设备的标识符.
  • { 分隔符 }:分界线.

快速示例

BluetoothSerial
  .readUntil({
    address: '00:11:22:33:44:55',
    delimiter: '\n',
  })
  .then((result: BluetoothReadResult) => {
    console.log(result.data);
  })
  .catch(() => {
    console.log('Error reading data from device');
  });

开始通知

收到任何数据时启用并通知.

startNotifications(options: BluetoothEnableNotificationsOptions): Promise<void>· ;

说明

函数 startNotifications 启用通知。 为了取回这些值, 使用以“ 读取” 为事件名的事件倾听器 .

const eventListener = BluetoothSerial.addListener('onRead' , (response: BluetoothReadResult) => {

    const { value } = response;
    //Do something with the value
});

参数

  • { 地址 }: 远程设备的标识符.
  • { 分隔符 }: 通知的界限.

快速示例

BluetoothSerial
  .startNotifications({
    address: '00:11:22:33:44:55',
    delimiter: '\n',
  })
  .then((result: BluetoothEnableNotificationsResult) => {
      eventListener = BluetoothSerial.addListener('onRead', (data: BluetoothReadResult) => {
      console.log("Received value: " + data.value);
    });
  })
  .catch(() => {
    console.log('Error enabling listener for device');
  });

通知

停止价值变化的传播.

stopNotifications(options: BluetoothDisableNotificationsOptions): Promise<void>· ;

说明

函数 stopNotifications 禁用通知 。 此外,事件听众必须删除.

eventListener.remove();

参数

  • { 地址 }: 远程设备的标识符.

快速示例

BluetoothSerial
  .stopNotifications({
    address: '00:11:22:33:44:55',
  })
  .then(() => {
    eventListener.remove();
  })
  .catch(() => {
    console.log('Error disabling listener for device');
  });

写入

将数据写入缓冲器 .

write(options: BluetoothWriteOptions): Promise<void>· ;

说明

函数 write 将数据写入缓冲器.

参数

  • { 地址 }: 远程设备的标识符.
  • { 价值 }: 要发送字符串 .

快速示例

BluetoothSerial
  .write({
    address: '00:11:22:33:44:55',
    value: 'Hello world',
  })
  .then(() => {
    console.log('Value sent to device');
  })
  .catch(() => {
    console.log('Error writing data to device');
  });

版本与发布记录

@e-is/capacitor-bluetooth-serial 共发布 34 个版本,首次发布于 2023/04/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
6.0.3latest2025/01/011.9 年前
6.0.22025/01/011.9 年前
6.0.12025/01/011.9 年前
5.0.12025/01/011.9 年前
5.0.02025/01/011.9 年前
6.0.02025/01/011.9 年前

关于 @e-is/capacitor-bluetooth-serial 的常见问题

@e-is/capacitor-bluetooth-serial 是什么?

电容蓝牙串行插件

如何安装 @e-is/capacitor-bluetooth-serial?

在项目目录运行 npm install @e-is/capacitor-bluetooth-serial,或使用 pnpm add / yarn add / bun add。

@e-is/capacitor-bluetooth-serial 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@e-is/capacitor-bluetooth-serial 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@e-is/capacitor-bluetooth-serial 可以商用吗?

@e-is/capacitor-bluetooth-serial 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@e-is/capacitor-bluetooth-serial 的下载量有多少?

根据 npm 官方下载统计,@e-is/capacitor-bluetooth-serial 在 2026/10/13 – 2026/11/11 这 30 天内被下载 10,732 次,日均约 358 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@e-is/capacitor-bluetooth-serial 还在维护吗?

最新版本 6.0.3 发布于 2025/01/01(1.9 年前);至今共发布 34 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/04/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。