终于实现了蓝牙的功能,也找到了合适的硬件,记录一下自己需要注意和总结的地方
具体的搜索、连接、断开、消息传输都已经实现了,作为项目的一个页面完成了

相应的代码地址,具体的蓝牙代码在pages/bluetooth当中

蓝牙部分代码地址

一.硬件部分

  1. HC-08 蓝牙模块 4.0BLE主从一体 CC2540 无线串口透传模块 BLE串口通信
  2. 硬件官网
    汇承官网
  3. 基本连接教程

    注意:串口模块和usb to ttl的连接方式

二. Windows串口调试助手

  1. 下载串口调试助手
  2. 设置默认波特率为9600

三.手机App测试

  1. LightBlue和Windows的调试


    注意:由图4可以看出串口通信的serviceId的UUID为FFE0(读写UUID)

四.微信小程序初始化连接(重头戏来了)

  1. 初始化蓝牙适配器
        wx.openBluetoothAdapter({success: function (res) {console.log('初始化蓝牙适配器成功' + JSON.stringify(res))that.msg = '初始化蓝牙适配器成功'wx.showModal({title: '蓝牙适配情况',content: '初始化蓝牙适配器成功'})},fail: function () {that.msg = '初始化蓝牙适配器失败'wx.showModal({title: '蓝牙适配情况',content: '蓝牙适配失败,请检查手机蓝牙和定位功能是否打开'})},complete: function () {console.log('初始化蓝牙适配器完成')}})
  1. 开启蓝牙搜索,并获取蓝牙设备列表 wx.getBluetoothAdapterState --> wx.onBluetoothAdapterStateChange --> wx.startBluetoothDevicesDiscovery --> wx.onBluetoothDeviceFound --> getBluetoothDevices
  2. 选择蓝牙设备获取相应的deviceId
  3. 连接蓝牙设备 wx.createBLEConnection --> wx.getBLEDeviceService(获取设备的ServiceId)
wx.createBLEConnection({deviceId: deviceId,success: function (res) {console.log('蓝牙设备连接成功')wx.hideLoading()wx.getBLEDeviceServices({deviceId: deviceId,success: function (res) {that.deviceService = res.servicesfor (var t = 0; t < that.deviceService.length; t++) {var service = that.deviceService[t]var serviceId = service.uuid.substring(4, 8)if (serviceId === 'FFE0') {                  //‘FFE0’为设备定义的读写UUIDthat.serviceId = service.uuid}}that.nowDevice = !that.nowDevicethat.nowService = !that.nowServiceconsole.log('获取蓝牙设备Service' + res.errMsg)},fail: function (res) {wx.showModal({title: '设备Service信息',content: '蓝牙设备连接成功' + '\n' + '设备信息获取错误' + res.errMsg})}})},fail: function (res) {console.log('蓝牙设备连接失败,请稍后重试')wx.hideLoading()wx.showModal({title: '提示',content: '蓝牙设备连接失败,请稍后重试',duration: 2000})},complete: function () {console.log('蓝牙设备连接完成')wx.hideLoading()}})
  1. 连接设备同时关闭蓝牙搜索 wx.stopBluetoothDevicesDiscovery
  2. 向串口发送数据(重要)
    wx.getBLEDeviceCharacteristics --> success: wx.notifyBLECharacteristicValueChang (启用notify功能) --> success:wx.writeBLECharacteristicValue --> success:wx.readBLECharacteristicValue(必须读取,否则Android设备无法正常向串口发送数据
wx.getBLEDeviceCharacteristics({deviceId: that.deviceId,serviceId: that.serviceId,success: function (res) {console.log(res.characteristics)that.deviceCharacteristics = res.characteristicsfor (var i = 0; i < that.deviceCharacteristics.length; i++) {that.characteristic = that.deviceCharacteristics[i]that.characteristicProperties = that.characteristic.propertiesif (that.characteristicProperties.notify === true) {that.characteristicId = that.characteristic.uuidwx.notifyBLECharacteristicValueChange({state: true, // 启用 notify 功能deviceId: that.deviceId,serviceId: that.serviceId,characteristicId: that.characteristicId,success: function (res) {console.log('开启notify成功' + that.characteristic.uuid)for (let i = 0; i < dataView.byteLength; i++) {var writeData = '0x' + dataView.getUint8(i).toString(16)that.writeDatas = that.writeDatas + '\n' + writeData}wx.writeBLECharacteristicValue({deviceId: that.deviceId,serviceId: that.serviceId,characteristicId: that.characteristicId,value: dataBuffer,success: function (res) {console.log('发送的数据:' + that.writeDatas)console.log('message发送成功')wx.showModal({title: '数据发送成功',content: that.writeDatas})wx.readBLECharacteristicValue({deviceId: that.deviceId,serviceId: that.serviceId,characteristicId: that.characteristicId,success: function (res) {console.log('读取数据成功')}})},fail: function (res) {// failconsole.log('message发送失败' + that.characteristicIdw)wx.showToast({title: '数据发送失败,请稍后重试',icon: 'none'})},complete: function (res) {// failconsole.log('message发送完成')}})},fail: function () {console.log('开启notify失败' + that.characteristicId)}})// that.writeMessage(that.deviceId, that.serviceId, that.characteristicIdw, that.characteristicIdr, that.characteristicIdn)}}},fail: function () {console.log('获取characteristic失败')}})
  1. 读取串口发送的数据 (重要)
    wx.getBLEDeviceCharacteristics --> success:wx.notifyBLECharacteristicValueChange --> success:wx.readBLECharacteristicValue --> wx.onBLECharacteristicValueChange

五.微信小程序蓝牙未启动,离开小程序开启蓝牙后再启动小程序

蓝牙页面启动流程

  1. 在onLoad中wx.openBluetoothAdapter
  2. 在onShow中判断当前蓝牙适配器的状态wx.onBluetoothAdapterStateChange --> 若更改成功,则wx.openBluetoothAdapter

六.微信小程序开发注意

  1. Android设备在readBLECharacteristicValu后可能会出现显示message传输成功但是串口调试助手收不到数据的情况
    解决方法:在writeBLECharacteristicValu后添加readBLECharacteristicValue
  2. 串口读写设备的时候注意转换数据格式,传输的数据格式为ArrayBuffer,需要转换才可以显示,直接console返回的res.value为空
    用到的转换格式代码
let data = that.sendData.split(',')let dataBuffer = new ArrayBuffer(data.length)let dataView = new DataView(dataBuffer)for (let j = 0; j < data.length; j++) {dataView.setUint8(j, '0x' + data[j])}var writeData = '0x' + dataView.getUint8(i).toString(16)
ab2hex: function (buffer) {var hexArr = Array.prototype.map.call(new Uint8Array(buffer), function (bit) {return ('00' + bit.toString(16)).slice(-2)})return hexArr.join('')}
  1. 根据serviceId获取characteristicId时需要采用for循环获取,不使用for循环会无法开启notify,使用for循环会开启两遍,两次开启UUID相同,但是第一次显示开启成功,第二次显示开启失败
  2. 实际读取串口发送数据的位置为wx.onBLECharacteristicValueChang,wx.readBLECharacteristicValue返回的res中只有接受信息是否成功的信息
  3. 问题:若手机蓝牙未启动,初始化页面无法成功开启蓝牙适配器,重新打开蓝牙后,页面无法重新开启蓝牙适配器
    解决:因为只在onLoad中定义了开始蓝牙适配器,没有在onShow中定义,通过《六》中来解决
    注意:必须在onShow中先进行判断,再选择是否调用wx.openBluetoothAdapter,否则在第一次启动页面的时候调用两次wx.openBluetoothAdapter,wx.showModal会显示两次(初始化页面的时候onLoad后会调用onShow)

微信小程序-BLE蓝牙实现demo相关推荐

  1. 微信小程序--Ble蓝牙

    在前面已经写了两篇关于Android蓝牙和ios 蓝牙开发的文章,今天带来的是微信小程序蓝牙实现. Android蓝牙 ios蓝牙(Swift) 有一段时间没有.没有写关于小程序的文章了.3月28日, ...

  2. 微信小程序实现蓝牙BLE(demo版)

    微信小程序实现蓝牙BLE(看文章最后一句话) 这是楼主在学校自己开发的用蓝牙小程序控制机械臂的(独立开发的). https://pan.baidu.com/s/1AmCW_ARhu--eapzd8Af ...

  3. 微信小程序低功耗蓝牙BLE快速开发js

    文章目录 1.前言 2.资料 3.BLE连接流程 BLE连接原理 4.index.js页面加载流程详细说明 完整代码: 1.前言 目的: 1.为了能三分钟快速开发BLE模块,特此做一个笔记,按照笔记的 ...

  4. 在HbuilderX中实现微信小程序下蓝牙连接打印机完整实战案例

    1.基础开发环境,所用到的 Api 以及实现的思路. 应用场景: 商家打印小票,小票包含顾客消费的商品明细信息以及末尾附上二维码,二维码供顾客扫码开票. HbuilderX开发工具: HBuilder ...

  5. 微信小程序入门教程+案例demo

    微信小程序入门教程+案例demo 尊重原创,转载请注明出处:原文查看惊喜更多 http://blog.csdn.net/qq137722697 首先摆在好姿态,--微信小程序开发也就那么回事.你只需要 ...

  6. 微信小程序的考勤管理Demo,包括前后端及数据库等内容

    这是一个微信小程序的考勤管理Demo,包括前后端及数据库等内容.如有错误或建议,欢迎指出. 前端:微信小程序框架 后端:koa框架基于express的新一代框架 文件:url80.ctfile.com ...

  7. 微信小程序引入高德地图Demo 快速上手

    文章目录 前言 一.获取高德key 二.引入官方实例 总结 前言 本文参照官方文档进行编写 最后引入官方实例 最终效果 ` 一.获取高德key 注册账号 https://lbs.amap.com/?r ...

  8. 微信小程序使用蓝牙连接设备流程

    微信小程序使用蓝牙连接设备流程 小程序使用蓝牙连接设备介绍 使用到的api 流程: 初始化蓝牙模块 wx.openBluetoothAdapter wx.openBluetoothAdapter({ ...

  9. 开源一个基于微信小程序的蓝牙室内定位软件(附下载链接)

    文章目录 1. 运行环境要求 2. 软件功能及程序说明 2.1 软件组成 2.2 主要功能 2.3 文件及函数功能说明 3. 软件设计及操作说明 4. 完整版代码获取 1. 运行环境要求 软件运行环境 ...

最新文章

  1. pytest接口测试之fixture传参数request
  2. linux下性能分析命令[总结]
  3. jackson 实体转json 为NULL或者为空不参加序列化
  4. 毕业设计(3)基于MicroPython的篮球计时计分器模型的设计与实现
  5. 生日快乐的代码_生日快乐,我的上电!
  6. ITCAST-C# 委托
  7. 【js】获得项目路径
  8. pycharm中如何正确配置pyqt5
  9. Bzoj2656 [Zjoi2012]数列(sequence)
  10. LeetCode 825. 适龄的朋友(计数排序+前缀和)
  11. win7设置html,Windows7怎么设置默认浏览器?
  12. 操作系统内存管理、Cache调度策略学习
  13. Linux 启动 Apache 时报错:(98)Address already in use: make_sock: could not bind to add
  14. java 怎么获取键的值_在 Java 中如何获取 Map 的所有键和值
  15. 元胞自动机简介(美赛复习一)
  16. 推荐系统之基于用户行为数据的协同过滤(Collaborative Filtering)
  17. 高仿墨迹天气下拉拉伸图片
  18. 项目中的“里程碑”就是我们常说的里程碑吗?
  19. outlook企业邮箱服务器要多少钱,怎么把企业邮箱配置到outlook中
  20. php 命格算法,八字格局中的弃命格mdash;mdash;从势格

热门文章

  1. Springboot中lookback日志配置文件
  2. web页面之SEO优化
  3. java8用chars_为什么String.chars()是Java 8中的int数据流?
  4. p38 MAPK调控酶及转录因子
  5. MPW和Pilot的区别?
  6. 快速排序解决相关问题
  7. 【MFC】VC++项目模板介绍
  8. CMake中find_package的使用
  9. 图形学-反走样/抗锯齿
  10. Net core学习系列(一)——Net Core介绍