JavaScript
JavaScript SDK 已发布至 npm. 以 @psdk/
为包前缀.
目前已支持的蓝牙实现包括 wechat/uniapp/taro.
使用方式 (以 cpcl 和 uniapp 为例):
安装依赖
npm i @psdk/cpcl @psdk/device-ble-uniapp
import {UniappBleBluetooth} from "@psdk/device-ble-uniapp";
import {ConnectedDevice, Lifecycle, Raw} from '@psdk/frame-father';
import {CPCL, GenericCPCL} from '@psdk/cpcl';
// 构造 uniapp bluetooth 对象
const bluetooth = new UniappBleBluetooth({
allowNoName: false,
});
let connectedDevice;
bluetooth.discovered(async (devices) => {
// 发现新设备
if (!devices.length) return;
if (devices[0].name.indexOf('test')) {
// 连接设备
connectedDevice = await bluetooth.connect(devices[0]);
// ==========
const lifecycle = new Lifecycle(connectedDevice);
const cpcl = CPCL.generic(lifecycle);
const reporter = await cpcl
.raw(Raw.binary(new Uint8Array([0x10, 0xff, 0xbf])))
.write({chunkSize: 512});
console.log(reporter);
}
});
// 开启搜索
await vm.bluetooth.startDiscovery();
此外, 可以参考 demo来查看如何使用 SDK.