码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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. );

  • 相关阅读:
    深入理解Python中的布尔值:真与假
    【追光者】大学即(已)将(经)毕业,四年,我的所感所想(部分)。
    MySQL数据库
    硬件总线基础07:PCIe总线基础-事务层(1)
    《golang设计模式》第三部分·行为型模式-01-责任链模式(Chain of Responsibility)
    图像数据增强2_标注框同时修改(VOC、YOLO)
    ES6——class类实现继承
    数据驱动——新材料发展的新引擎
    Excel 数据透视表小技巧之 01如何取消透视表最后几行或最后几列的汇总
    nacos源码深度剖析理解之注册中心
  • 原文地址:https://blog.csdn.net/future_1_/article/details/139916914
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号