介绍
@iotize/device-com-ble.node 是一个 Node.js 包,用于在 Node.js 中连接到 IoTize BLE 设备。这个包提供了一些接口,让开发者可以快速地进行 IoTize BLE 设备的连接,读写特性值,以及订阅通知等操作,同时也提供了一些示例代码让开发者可以参考、借鉴、修改,并进行开发。
安装
使用 npm 安装 @iotize/device-com-ble.node
npm install @iotize/device-com-ble.node
基本使用
@iotize/device-com-ble.node 在 Node.js 中使用十分简单,首先引入该包:
const iotizeBle = require('@iotize/device-com-ble.node');
接下来可以使用以下 API 进行操作:
连接
连接到 IoTize BLE 设备:
async function connect() { const device = await iotizeBle.connect('COM14', { timeout: 2000 }); console.log(`Connected to device: ${device}`); }
断开连接
断开与 IoTize BLE 设备的连接:
async function disconnect() { await iotizeBle.disconnect(); console.log('Disconnected from device.'); }
读特性值
读取 IoTize BLE 设备的特性值:
async function read() { const result = await iotizeBle.read('ffe1'); console.log(`Read result: ${result}`); }
写特性值
写 IoTize BLE 设备的特性值:
async function write() { const result = await iotizeBle.write('ffe1', '0A'); console.log(`Write result: ${result}`); }
订阅通知
订阅 IoTize BLE 设备的通知:
async function subscribe() { const startResult = await iotizeBle.startNotifications('ffe1', (data) => { console.log(`Received data: ${data}`); }); console.log(`Start notification result: ${startResult}`); }
停止通知
停止订阅 IoTize BLE 设备的通知:
async function unsubscribe() { const stopResult = await iotizeBle.stopNotifications('ffe1'); console.log(`Stop notification result: ${stopResult}`); }
示例代码
以下是一个完整的示例代码:
-- -------------------- ---- ------- ----- --------- - --------------------------------------- ----- -------- ------ - --- - ----- ------ - ----- -------------------------- - -------- ---- --- ---------------------- -- ------- ------------ ----- ------ - ----- ----------------------- ----------------- ------- ------------ ----- ----------------------- ------ ------------------ ------- ---------- ----- ----------- - ----- ------------------------------------ ------ -- - --------------------- ----- ---------- --- ------------------ ------------ ------- ----------------- ----- --- --------------- -- ------------------- ------- ----- ---------- - ----- ------------------------------------ ----------------- ------------ ------- ---------------- ----- ----------------------- ------------------------- ---- ---------- - ----- ----- - ------------------ - - -------
总结
通过本文介绍,您应该已经知道如何使用 @iotize/device-com-ble.node 连接 IoTize BLE 设备以及进行读写特性值和订阅通知等操作。同时,我们也提供了示例代码,以供您参考、学习和使用,希望您能够成功地进行开发。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/201962