• uniapp 低功耗蓝牙BLE分包


    ble.js

    1. // 分包写入蓝牙
    2. async sendWriteBLECharacteristicValue(
    3. deviceId,
    4. serviceId,
    5. writeCharacteristicId,
    6. readCharacteristicId,
    7. buffer,
    8. success, // 成功回调
    9. failure, // 失败回调
    10. ) {
    11. const offset = 500; // 偏移量
    12. let pos = 0; // 位置
    13. let bytes = buffer.byteLength; // 总字节
    14. let that = this;
    15. while (bytes > 0) {
    16. let endPos = bytes > offset ? pos + offset : pos + bytes;
    17. const tempBuffer = buffer.slice(pos, endPos);
    18. pos += offset;
    19. bytes -= offset;
    20. // 延迟发送
    21. await that.sendDelay(150, tempBuffer).then((buffer) => {
    22. that.writeBLECharacteristicValue(
    23. deviceId,
    24. serviceId,
    25. writeCharacteristicId,
    26. buffer,
    27. (res) => {
    28. if (buffer.byteLength < offset) {
    29. success(res);
    30. }
    31. },
    32. (err) => {
    33. failure(err);
    34. }
    35. );
    36. });
    37. if (readCharacteristicId) {
    38. console.log(readCharacteristicId, "读文件");
    39. uni.readBLECharacteristicValue({
    40. deviceId: deviceId,
    41. serviceId: serviceId,
    42. characteristicId: readCharacteristicId,
    43. });
    44. }
    45. }
    46. }
    47. sendDelay(delay, buffer) {
    48. return new Promise((resolve, reject) => {
    49. setTimeout(() => resolve(buffer), delay);
    50. });
    51. }
    52. writeBLECharacteristicValue(
    53. deviceId,
    54. serviceId,
    55. characteristicId,
    56. buffer,
    57. success,
    58. failure
    59. ) {
    60. plus.bluetooth.writeBLECharacteristicValue({
    61. deviceId: deviceId,
    62. serviceId: serviceId,
    63. characteristicId: characteristicId,
    64. value: buffer,
    65. success(res) {
    66. success(res);
    67. },
    68. fail(err) {
    69. if (res.errCode == "10006") {
    70. //当前连接已断开,清空连接数据
    71. }
    72. console.log("发送失败", res);
    73. failure(err);
    74. },
    75. });
    76. }

    index.vue使用分包

    1. this.$ble.sendWriteBLECharacteristicValue(
    2. deviceId, // 蓝牙地址ID
    3. serveiceId, // ABF0
    4. writeCharId, // 写入蓝牙通道 此处用ABF3
    5. readCharId,// 读取蓝牙返回数据通道 此处用ABF4
    6. buffer, // 要写入蓝牙的数据 Uint8Array
    7. (res) => {
    8. console.log("打印完成: " + JSON.stringify(res));},
    9. (err) => {
    10. console.log("打印失败: " + JSON.stringify(err));
    11. }
    12. );

  • 相关阅读:
    反射知识点学习
    在PhpStorm中hyperf调试的方法步骤是什么
    SpringBoot+Redis 防止用户重复登录
    Nginx启用Geoip2模块实现国家城市识别 —— 筑梦之路
    漫谈信息模型(1)
    配置https接口
    MySql分区
    鸿蒙学习笔记(1)基于eTS的入门应用结构简单梳理
    @Slf4j打印异常信息打印出堆栈信息
    什么是IT服务台?
  • 原文地址:https://blog.csdn.net/future_1_/article/details/139916914