使用微信小程序的蓝牙模块

蓝牙开发流程

  • 打开蓝牙适配器

  • 搜索周围蓝牙

  • 获取搜索过程中所搜索到的设备信息

  • 连接想要连接的设备

  • 获取服务、特征值

  • 写数据、读数据

上代码

  • index.wxml
<!--index.wxml-->
<view class="content">
  <button type="primary" class="button" bindtap="open">初始化蓝牙适配器</button>
  <button type="primary" class="button" bindtap="connect">连接设备</button>
  <button type="primary" class="button" bindtap="write">写数据</button>
  <button type="primary" class="button" bindtap="close">断开蓝牙连接</button>
</view>
  • index.js
//index.js
//获取应用实例
var app = getApp();
Page({
  data: {
    UUID: "0000FFE0-0000-1000-8000-00805F9B34FB",
    UUID2: "0000FFE1-0000-1000-8000-00805F9B34FB",
    name: "",
    deviceId: "",
    serviceId: "",
    characteristicId: ""
  },

  open: function() {
    var that = this;
    // 1. 打开蓝牙适配器
    wx.openBluetoothAdapter({
      success: function(res) {
        wx.showToast({
          title: '蓝牙',
          icon: 'success',
          duration: 1000
        })
        console.log("打开蓝牙适配器成功", res);
        // 2. 搜索蓝牙设备
        wx.startBluetoothDevicesDiscovery({
          success: function(res) {
            wx.showLoading({
              title: '正在搜索设备',
            })
            console.log(res);
            // 3. 监听寻找到新设备的事件
            wx.onBluetoothDeviceFound(
              function(e) {
                console.log(e)
                // 如果找到BT18的设备
                if (e.devices[0].name == "BT18") {
                  wx.showToast({
                    title: '搜索到BT18设备',
                    icon: 'success',
                    duration: 500
                  })
                  that.setData({
                    deviceId: e.devices[0].deviceId
                  })
                  wx.hideLoading();
                  // 4. 关闭 蓝牙搜索
                  wx.stopBluetoothDevicesDiscovery({
                    success: function(res) {
                      console.log(res)
                    },
                  })

                }
              }
            )
          },
          fail: function(res) {
            console.log(res);
          }
        })
      },
      fail: function(res) {
        console.log("打开蓝牙适配器失败", res)
      }
    })
  },

  connect: function() {
    var that = this;
    wx.showLoading({
      title: '正在连接设备',
    })
    // 5. 连接设备
    wx.createBLEConnection({
      deviceId: that.data.deviceId,
      success(res) {
        console.log(res)
        wx.getBLEDeviceServices({
          deviceId: that.data.deviceId,
          success: function(res) {
            console.log(res.services);
            that.setData({
              serviceId: res.services[0].uuid
            })
            // 获取这个服务的一个特征值
            wx.getBLEDeviceCharacteristics({
              deviceId: that.data.deviceId,
              serviceId: that.data.UUID,
              success: function(res) {
                console.log(res)
                wx.hideLoading()
                wx.showToast({
                  title: '连接成功',
                  icon: 'success',
                  duration: 500
                })
                that.setData({
                  characteristicId: res.characteristics[0].uuid
                })
              },
              fail: function(res) {
                console.log(res)
              }
            })
          },
          fail: function(res) {
            console.log(res)
          }
        })
      },
      fail: function(res) {
        console.log("连接失败", res)
      }
    })
  },

  write: function(res) {
    var that = this;
    var buffer = new ArrayBuffer(1);
    var dataview = new DataView(buffer);
    dataview.setUint8(0, 3);
    console.log(that.data.deviceId)
    console.log(that.data.serviceId, that.data.characteristicId)
    wx.writeBLECharacteristicValue({
      deviceId: that.data.deviceId,
      serviceId: that.data.UUID,
      characteristicId: that.data.UUID2,
      value: buffer,
      success: function(res) {
        console.log('writeBLECharacteristicValue success', res.errMsg)
        wx.showToast({
          title: '发送成功',
          icon: 'success',
          duration: 500
        })
      },
      fail: function(res) {
        console.log(res)
        wx.showToast({
          title: '发送失败',
          icon: 'success',
          duration: 500
        })
      }
    })
  },

  close: function(res) {
    wx.closeBLEConnection({
      deviceId: this.data.deviceId,
      success(res) {
        wx.showToast({
          title: '蓝牙已关闭',
          icon: 'success',
          duration: 500
        })
      }
    })
  }
})
  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 微信小程序可以通过蓝牙接口实现蓝牙设备的连接、数据传输等功能。微信官方提供了蓝牙demo供开发者学习和使用蓝牙demo包含两部分,一部分是微信小程序端代码,一部分是蓝牙设备端的代码。微信小程序端代码包括蓝牙设备的搜索、连接、数据读取和写入等功能。蓝牙设备端的代码需要使用蓝牙模块读取和写入数据,以实现与小程序端的通讯。 在使用蓝牙demo之前,需要先开启蓝牙并授权小程序使用蓝牙功能。开发者还需要了解蓝牙设备的协议和数据格式,才能正确读取和解析蓝牙设备发出的数据。 蓝牙demo的实现,可以广泛应用于诸如智能手表、蓝牙耳机、蓝牙体温计等智能设备的开发中。同时,通过蓝牙模块微信小程序的结合,可以实现更多的物联网应用场景。 ### 回答2: 微信小程序蓝牙demo是一个用于演示微信小程序蓝牙设备通信的示例程序。在这个demo中,我们可以了解到使用微信小程序蓝牙接口实现扫描周围蓝牙设备、连接设备、读取设备的数据等操作是如何实现的。 通过微信小程序蓝牙demo,我们可以看出小程序蓝牙接口相对简单易用,且可以适用于多种类型的蓝牙设备。同时,小程序蓝牙接口提供了完善的事件回调和错误处理机制,使得开发者可以更好地处理蓝牙通信中出现的各种问题。 除此之外,微信小程序蓝牙demo还提供了一些基础的UI界面,包括扫描设备、连接设备、数据读取等等,方便开发者进行二次开发和调试。这些基础界面可以通过微信小程序的自定义组件进行引用,也可以进行二次开发。 总之,微信小程序蓝牙demo为开发者提供了一个清晰的蓝牙接口调用示例,并且提供了可扩展的UI界面,以帮助开发者更快速、更有效地进行小程序蓝牙开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值