电容蓝牙串行插件
从 电容-蓝牙相继 (存档)
使用 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 密码
方法
- 蓝牙序列。 已启用
- 蓝牙序列。 可启用
- 蓝牙系列可操作
- 蓝牙Serial
- 蓝牙系列. 启动
- 蓝牙系列 停止使用
- 蓝牙Serial.scan
- 蓝牙系列. connect
- 蓝牙串行. 连接不安全
- 蓝牙串行. 断接
- 蓝牙Serial.is已连接
- 蓝牙系列. read
- 蓝牙Serial. read Until
- 蓝牙系列. start 通知
- 蓝牙系列 停止 通知
- 蓝牙Serial.write
可启用
如果启用了蓝牙报告 .
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');
});