本文主要是介绍uniapp 低功耗蓝牙BLE分包,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ble.js
// 分包写入蓝牙async sendWriteBLECharacteristicValue(deviceId,serviceId,writeCharacteristicId,readCharacteristicId,buffer,success, // 成功回调failure, // 失败回调) {const offset = 500; // 偏移量let pos = 0; // 位置let bytes = buffer.byteLength; // 总字节let that = this;while (bytes > 0) {let endPos = bytes > offset ? pos + offset : pos + bytes;const tempBuffer = buffer.slice(pos, endPos);pos += offset;bytes -= offset;// 延迟发送await that.sendDelay(150, tempBuffer).then((buffer) => {that.writeBLECharacteristicValue(deviceId,serviceId,writeCharacteristicId,buffer,(res) => {if (buffer.byteLength < offset) {success(res);}},(err) => {failure(err);});});if (readCharacteristicId) {console.log(readCharacteristicId, "读文件");uni.readBLECharacteristicValue({deviceId: deviceId,serviceId: serviceId,characteristicId: readCharacteristicId,});}}}sendDelay(delay, buffer) {return new Promise((resolve, reject) => {setTimeout(() => resolve(buffer), delay);});}writeBLECharacteristicValue(deviceId,serviceId,characteristicId,buffer,success,failure) {plus.bluetooth.writeBLECharacteristicValue({deviceId: deviceId,serviceId: serviceId,characteristicId: characteristicId,value: buffer,success(res) {success(res);},fail(err) {if (res.errCode == "10006") {//当前连接已断开,清空连接数据}console.log("发送失败", res);failure(err);},});}
index.vue使用分包
this.$ble.sendWriteBLECharacteristicValue(deviceId, // 蓝牙地址IDserveiceId, // ABF0writeCharId, // 写入蓝牙通道 此处用ABF3readCharId,// 读取蓝牙返回数据通道 此处用ABF4buffer, // 要写入蓝牙的数据 Uint8Array(res) => {console.log("打印完成: " + JSON.stringify(res));},(err) => {console.log("打印失败: " + JSON.stringify(err));});
这篇关于uniapp 低功耗蓝牙BLE分包的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!