原生微信小程序蓝牙开发(四)

接上一篇(原生微信小程序蓝牙开发(三)

第四步:连接蓝牙(wx.createBLEConnection)

连接蓝牙低功耗设备。

若小程序在之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的 deviceId 直接尝试连接该设备,无需再次进行搜索操作。

参数

Object object

属性类型默认值必填说明
deviceIdstring蓝牙设备 id
timeoutnumber超时时间,单位 ms,不填表示不会超时
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

官方文档:wx.createBLEConnection(Object object)

下面上代码:

  // 连接蓝牙
  connetBlue(deviceId, name) {
    let that = this;
    wx.createBLEConnection({
      deviceId: deviceId, //设备id
      success: (res) => {
        if (that.data.platform == "android") {
          // 连接成功后设置MTU
          wx.setBLEMTU({
            deviceId: deviceId,
            mtu: 512,
            success: function (res) {
              wx.setStorageSync("BTdeviceId", deviceId);
              that.setData({
                BTDeviceIds: deviceId,
              });
              wx.showToast({
                title: "连接成功",
                icon: "success",
              });
              that.setData({
                deviceId: deviceId,
                "setInfo.connetBlue": "连接设备成功",
                "setInfo.connected": "已连接",
                "setInfo.MTU": "安卓设置MTU成功",
              });
              // 连接成功后关闭蓝牙搜索
              wx.stopBluetoothDevicesDiscovery({
                success: (res) => {
                  // console.log("连接蓝牙成功之后关闭蓝牙搜索", res);
                  that.setData({
                    "setInfo.findBlue": "连接成功,关闭蓝牙搜索",
                  });
                },
              });
              that.getServiceId(); //5.0
            },
            fail: function (res) {
              wx.showToast({
                title: "设置MTU失败",
                icon: "error",
              });
              that.setData({
                "setInfo.MTU": "安卓设置MTU失败",
              });
            },
          });
        } else {
          wx.showToast({
            title: "设备连接成功",
            icon: "success",
          });
          wx.setStorageSync("BTdeviceId", deviceId);
          that.setData({
            BTDeviceIds: deviceId,
          });
          that.setData({
            deviceId: deviceId,
            "setInfo.connetBlue": "连接设备成功",
            "setInfo.connected": "已连接",
          });
          wx.stopBluetoothDevicesDiscovery({
            success: (res) => {
              // console.log("连接蓝牙成功之后关闭蓝牙搜索", res);
              that.setData({
                "setInfo.findBlue": "连接成功,关闭蓝牙搜索",
              });
            },
          });
          that.getServiceId(); //5.0
        }
      },
      fail: (res) => {
        wx.showToast({
          title: "连接失败",
          icon: "error",
        });
        this.initBlue();
      },
    });
  },

注意:

  1. Android设备需要通过 wx.setBLEMTU 来协商设置蓝牙低功耗的最大传输单元,仅安卓系统 5.1 以上版本有效,iOS 因系统限制不支持所以可以不用设置。
  2. 所以在连接的时候要注意,需要分 android ios 系统进行连接后的设置
  3. 连接成功后需要通过 wx.stopBluetoothDevicesDiscovery 关闭蓝牙搜索

连接完成后进行下一步——获取蓝牙设备的服务uuid

有不懂可以直接评论区提问,我们一起学习探索。

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值