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

蓝牙开发流程

  • 打开蓝牙适配器

  • 搜索周围蓝牙

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

  • 连接想要连接的设备

  • 获取服务、特征值

  • 写数据、读数据

上代码

  • 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
        })
      }
    })
  }
})

你可能感兴趣的:(微信小程序)