微信小程序连接蓝牙设备

上一篇 微信小程序搜索蓝牙设备

 

选择搜索到的蓝牙设备列表中的设备,点击链接设备按钮进行蓝牙设备的连接。

一、data 里面定义一个参数:isCreateBLEConnection 蓝牙设备连接状态

isCreateBLEConnection:false, //蓝牙设备连接状态

二、onload 中自定义蓝牙低功耗连接状态改变事件的监听 onBLEConnectionStateChangeCallback 代码

    //蓝牙低功耗连接状态改变事件的监听
    Bluetooth.onBLEConnectionStateChangeCallback = function(res){
      //console.log(res);
      if(res.deviceId == that.data.deviceId)
      {
        that.setData({isCreateBLEConnection:res.connected});
      }
    };

三、连接和断开蓝牙设备

  //连接设备 按钮事件
  connectDeviceClick()
  {
    var that=this;
    let deviceId = that.data.deviceId;
    if(deviceId!='')
    {
      Bluetooth.CheckSystemInfo().then(res=>{
        if(res)
        {
          wx.showLoading({
            title: '连接中',
            mask:true
          });
          //console.log(that.data.isStartBluetoothdeviceDiscovery);
          //如果正在进行蓝牙搜索,先停止蓝牙搜索
          if(that.data.isStartBluetoothdeviceDiscovery)
          {
            that.stopBluetoothDevicesDiscovery();
          }
          that.createBLEConnection().then(
            function(res){
              wx.hideLoading();
            },
            function(err){
              wx.hideLoading();
            }
          );
        }
      });
    }
    else
    {
      wx.showModal({
        content: '请先选择要连接的设备',
        showCancel:false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定');
          } 
        }
      });
    }
  },
  //断开设备
  closeBLEConnection()
  {
    var that=this;
    let deviceId = that.data.deviceId;
    Bluetooth.closeBLEConnection(deviceId).then(
      function(res){
        console.log('蓝牙设备连接状态', that.data.isCreateBLEConnection);
        that.seeConsole('断开连接设备成功');
        //that.setData({ isCreateBLEConnection: false });
        //移除蓝牙低功耗连接状态改变事件的监听函数
        Bluetooth.offBLEConnectionStateChange();
        //移除蓝牙低功耗设备的特征值变化事件的全部监听函数
        Bluetooth.onBLECharacteristicValueChange();
      },
      function(err){
        that.seeConsole('断开连接设备失败');
      }
    );
  },
  //断开设备 按钮事件
  closeDeviceClick()
  {
    var that=this;
    let isCreateBLEConnection = that.data.isCreateBLEConnection;
    if(isCreateBLEConnection)
    {
      Bluetooth.CheckSystemInfo().then(res=>{
        if(res)
        {
          that.closeBLEConnection();
        }
      });
    }
    else
    {
      wx.showModal({
        content: '请先连接设备',
        showCancel:false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定');
          } 
        }
      });
    }
  },

四、页面代码

  <view class="event-bar" list-item>
    <view class="view_left">
      <button class="btn_bg" bindtap="connectDeviceClick">连接设备</button>
    </view>
    <view class="view_right">
      <button class="btn_bg" bindtap="closeDeviceClick">断开连接</button>
    </view>
  </view>

下一篇 将继续介绍 微信小程序获取连接的蓝牙设备的服务及特征值

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wsk198726

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值