微信小程序中使用蓝牙信标

微信小程序使用蓝牙信标

流程:

  1. 使用

    wx.startBeaconDiscovery({})
    

    开始搜索附近的 Beacon 设备

  2. 使用

    wx.onBeaconUpdate(res=>{})
    

    监听 Beacon 设备更新事件,仅能注册一个监听

  3. 使用

    wx.stopBeaconDiscovery({})
    

    停止搜索附近的 Beacon 设备

代码:

console.log("这是测试蓝牙信标的函数");
    const _this = this
    //开始搜索
    wx.startBeaconDiscovery({
      uuids:['01122334-4556-6778-899A-ABBCCDDEEFF0','40058D93-4B8C-9435-A795-AE949793A6D8'],
      success(res){
        console.log(res);
          //搜索更新,返回res.beacons
        wx.onBeaconUpdate(res => {
          console.log(res);
          if (res.beacons.length !== 0) {
            console.log(res.beacons);
             //关闭搜索
            wx.stopBeaconDiscovery({
              success(res){

              },
              fail(err){
                console.log(err);
              }
            })
          }
        })
      },
      fail(err){
        console.log(err);
        if ('errCode' in err) {
          //处理搜索报错
          switch(err.errCode){
            case 11000:
              Dialog.alert({
                title: '错误',
                message: '系统或设备不支持',
              }).then(() => {
                // on close
              });
              break
              case 11001:
                Dialog.alert({
                  title: '错误',
                  message: '蓝牙服务不可用',
                }).then(() => {
                  // on close
                });
                break
                case 11002:
                  Dialog.alert({
                    title: '错误',
                    message: '位置服务不可用',
                  }).then(() => {
                    // on close
                  });
                  break
                  case 11005:
                    Dialog.alert({
                      title: '错误',
                      message: '系统错误',
                    }).then(() => {
                      // on close
                    });
                    break
                    case 11003:
                      Dialog.alert({
                        title: '错误',
                        message: '已经开始搜索',
                      }).then(() => {
                        // on close
                      });
                      break
          }
        }
        
      }
    })
posted @ 2023-12-21 17:20  优雅的水晶炮炮  阅读(510)  评论(0)    收藏  举报