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

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

小程序使用蓝牙连接设备介绍
使用到的api

流程:

  1. 初始化蓝牙模块 wx.openBluetoothAdapter
 wx.openBluetoothAdapter({
    
      success: function (res) {
   
        wx.showToast({
   
          title: '初始化成功',
          icon: 'success',
          duration: 800
        })
        //搜索设备
        self.findMachine(); 
      },
      fail: function (res) {
    //手机上的蓝牙没有打开
        wx.showToast({
   
          title: '请开启蓝牙',
          icon: 'error',
          duration: 1500
        })
      }
    })
  1. 搜索周边蓝牙设备 wx.startBluetoothDevicesDiscovery
findMachine() {
   
    var that = this
    //此操作比较耗费系统资源,请在搜索到需要的设备后及时调用 wx.stopBluetoothDevicesDiscovery 停止搜索。
    wx.startBluetoothDevicesDiscovery({
   
      allowDuplicatesKey: false,
      interval: 0,
      success: function (res) {
   
        console.log(res)
        wx.showLoading({
   
          title: '正在搜索设备',
        })
        //查找设备
        that.getMachine()
      }
    })
  },
  1. 找到要连接的设备(根据名称查找) wx.getBluetoothDevices
  getMachine() {
   
    var that = this
    wx.getBluetoothDevices({
   
      success: function (res) {
   
        console.log(res)
        wx.hideLoading();
        var list = res.devices;
        if (list.length != 0) {
   
          var name = "设备名称"
  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值