@e-is/capacitor-bluetooth-serial

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

Capacitor Bluetooth Serial Plugin

原始使用文档
文档目录67 节
  1. Usage
  2. API Documentation
  3. Methods
  4. isEnabled
  5. Description
  6. Parameters
  7. Quick Example
  8. canEnable
  9. Description
  10. Parameters
  11. Quick Example
  12. enable
  13. Description
  14. Parameters
  15. Quick Example
  16. disable
  17. Description
  18. Parameters
  19. Quick Example
  20. startEnabledNotifications
  21. Description
  22. Parameters
  23. Quick Example
  24. stopEnabledNotifications
  25. Description
  26. Parameters
  27. Quick Example
  28. scan
  29. Description
  30. Parameters
  31. Quick Example
  32. connect
  33. Description
  34. Parameters
  35. Quick Example
  36. connectInsecure
  37. Description
  38. Parameters
  39. Quick Example
  40. disconnect
  41. Description
  42. Parameters
  43. Quick Example
  44. isConnected
  45. Description
  46. Parameters
  47. Quick Example
  48. read
  49. Description
  50. Parameters
  51. Quick Example
  52. readUntil
  53. Description
  54. Parameters
  55. Quick Example
  56. startNotifications
  57. Description
  58. Parameters
  59. Quick Example
  60. stopNotifications
  61. Description
  62. Parameters
  63. Quick Example
  64. write
  65. Description
  66. Parameters
  67. Quick Example

Capacitor Bluetooth Serial Plugin

Forked from capacitor-bluetooth-serial (archived)

A client implementation for interacting with Bluetooth (serial), using Capacitor.

Supported platforms

  • Web
  • Android
  • iOS

Supported Capacitor versions:

  • Capacitor 6 : use version 6.x
  • Capacitor 5 : use version 5.x
  • Capacitor 4 : use version 0.6.x
  • Capacitor <= 3: Please use capacitor-bluetooth-serial (<= v0.0.3)

Usage

Install the plugin via npm

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

//...do something with plugin

API Documentation

Interface and type definitions can be found here.

API

Methods

isEnabled

Reports if bluetooth is enabled.

isEnabled(): Promise<BluetoothState>;

Description

Function isEnabled calls the success whatever bluetooth is enabled or not. The promise will contain an attribute enabled indicating if bluetooth is enabled or not enabled. The failure callback will be called only if an error occurs.

If you want to enable bluetooth afterwards, you can use #enable directly, once #enable also check if the bluetooth is conected or not.

Parameters

None.

Quick Example

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

canEnable

Reports if bluetooth can be enabled.

canEnable(): Promise<BluetoothState>;

Description

Function canEnable calls the success whatever bluetooth can be enabled or not. The promise will contain an attribute enabled indicating if bluetooth can be enabled or not. The failure callback will be called only if an error occurs.

Parameters

None.

Quick Example

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

Enable bluetooth if it is not enabled. Also request permissions for bluetooth access if it is necessary.

enable(): Promise<BluetoothState>;

Description

Function enable calls the success whatever bluetooth is successfully enabled or not. The promise will contain an attribute enabled indicating if bluetooth is enabled or not enabled after the process. The failure callback will be called only if an error occurs.

If the app does not have permission to use bluetooth, it will request it.

Parameters

None.

Quick Example

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

disable

Disable bluetooth if enabled. Also request permissions for bluetooth access if it is necessary.

disable(): Promise<BluetoothState>;

Description

Function disable calls the success whatever bluetooth is successfully disabled or not. The promise will contain an attribute enabled indicating if bluetooth is enabled or not enabled after the process. The failure callback will be called only if an error occurs.

If the app does not have permission to use bluetooth, it will request it.

Parameters

None.

Quick Example

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

startEnabledNotifications

Be notified when bluetooth status changed.

startEnabledNotifications(): Promise<void>;

Description

Function startEnabledNotifications enable status notifications. In order to retrieve the values, use an Event Listener with 'onEnabledChanged' as event name.

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

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

Parameters

None.

Quick Example

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

Stops the propagation of value changes.

stopEnabledNotifications(): Promise<void>;

Description

Function stopEnabledNotifications disable bluetooth state notifications. Additionally, the event listener has to be removed.

eventListener.remove();

Parameters

None.

Quick Example

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

scan

Discover devices visible and close to the device

scan(): Promise<BluetoothScanResult>;

Description

Android

Function scan discovers Bluetooth devices close to the device and visible. The success callback is called with a list of objects similar to list, or an empty list if no devices are found.

Example list passed to success callback.

[{
    "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"
}]

The discovery process takes a while to happen. You may want to show a progress indicator while waiting for the discover proces to finish, and the sucess callback to be invoked.

Calling connect on an unpaired Bluetooth device should begin the Android pairing process.

Parameters

None.

Quick Example

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

connect

Connect to a Bluetooth device.

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

Description

Function connect connects to a Bluetooth device. The callback Success will be called when the connection is successful. Failure is called if the connection fails.

Android

For Android, connect takes a MAC address of the remote device.

Parameters

  • { address }: Identifier of the remote device.

Quick Example

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

connectInsecure

Connect insecurely to a Bluetooth device.

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

Description

Function connectInsecure connects to a Bluetooth device. The callback Success will be called when the connection is successful. Failure is called if the connection fails. Function connectInsecure works like BluetoothSerial.connect, but creates an insecure connection to a Bluetooth device. See the Android docs for more information.

Android

For Android, connectInsecure takes a MAC address of the remote device.

Parameters

  • { address }: Identifier of the remote device.

Quick Example

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

disconnect

Disconnect a Bluetooth device.

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

Description

Function disconnect disconnects a Bluetooth device. The callback Success will be called when the disconnection is successful. Failure is called if the disconnection fails.

Android

For Android, disconnect takes a MAC address of the remote device.

Warning: If no address is passed, all devices will be disconnected.

Parameters

  • { address }: Identifier of the remote device.

Quick Example

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

isConnected

Reports the connection status.

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

Description

Function isConnected calls the success callback with the connection status (connected or not connected). Failure will be called only if an error occurs.

Android

For Android, isConnected takes a MAC address of the remote device.

Parameters

  • { address }: Identifier of the remote device.

Quick Example

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

Reads data from the buffer.

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

Description

Function read reads the data from the buffer. The data is passed to the success callback as a String. Calling read when no data is available will pass an empty String to the callback.

Parameters

  • { address }: Identifier of the remote device.

Quick Example

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

Reads data from the buffer until it reaches a delimiter.

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

Description

Function readUntil reads the data from the buffer until it reaches a delimiter. The data is passed to the success callback as a String. If the buffer does not contain the delimiter, an empty String is passed to the callback.

Parameters

  • { address }: Identifier of the remote device.
  • { delimiter }: Delimiter.

Quick Example

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

Enable and be notified when any data is received.

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

Description

Function startNotifications enable notifications. In order to retrieve the values, use an Event Listener with 'onRead' as event name.

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

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

Parameters

  • { address }: Identifier of the remote device.
  • { delimiter }: Delimiter for notification.

Quick Example

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

Stops the propagation of value changes.

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

Description

Function stopNotifications disable notifications. Additionally, the event listener has to be removed.

eventListener.remove();

Parameters

  • { address }: Identifier of the remote device.

Quick Example

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

write

Write data to the buffer.

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

Description

Function write writes data to the buffer.

Parameters

  • { address }: Identifier of the remote device.
  • { value }: String to send.

Quick Example

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 是什么?

Capacitor Bluetooth Serial Plugin

如何安装 @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 和提交记录判断。