微信小程序获取蓝牙信标

/**
 * 搜索设备界面
 */
import Dialog from '@vant/weapp/dialog/dialog';
Page({
  data: {
    list: []
  },
  onPullDownRefresh: function () {
    wx.request({
      url: 'https://wwz.jingyi.icu/app/Explain/index',
      data: {
        scenic_id: 3
      },
      method: 'POST',
      success: (res) => {
        console.log(res);
        let that = this
        let BeaconData = res.data.data
        that.setData({
          list: BeaconData
        })
        let seach = {}
        let uuids = []
        for (let i = 0; i < res.data.data.length; i++) {
          uuids.push(res.data.data[i].deviceid)
        }
        console.log(uuids);
        // 初始化蓝牙设备
        wx.openBluetoothAdapter({
          success: function (blueres) {
            console.log('蓝牙适配器初始化成功', blueres)
            wx.startBeaconDiscovery({
              uuids: uuids, //多个uuid数组
              success: function (res) {
                console.log(res);
                wx.onBeaconUpdate(function () {
                  //设置监听事件
                  wx.getBeacons({
                    //在监听事件中获取数据
                    success: res => {
                      console.log(res.beacons) //res.beacons 为搜索到的iBeacon数据数组
                      //业务代码
                      for (let i = 0; i < BeaconData.length; i++) {
                        console.log(res.beacons[0].uuid);
                        console.log(BeaconData[i].deviceid);
                        if (res.beacons[0].uuid.toUpperCase() == BeaconData[i].deviceid.toUpperCase()) {
                          console.log(123);
                          that.setData({
                            list: BeaconData
                          })
                        }
                      }
                      console.log(seach);
                      wx.stopBeaconDiscovery({
                        success(res) {
                          console.log("已暂停搜索", res);
                        }
                      })
                    }
                  })
                })
              }
            })
          },
          fail: function (res) {
            console.log(res);
            wx.showModal({
              title: '提醒',
              content: '系统监测未打开蓝牙,请打开后下拉刷新',
            })
            console.log(uuids);
          }
        })
      }
    })
    wx.stopPullDownRefresh()  //自动关闭下拉刷新
  },
  onLoad: function () {
    if (wx.getStorageSync('info')) {
      wx.request({
        url: 'https://wwz.jingyi.icu/app/Explain/index',
        data: {
          scenic_id: 3
        },
        method: 'POST',
        success: (res) => {
          console.log(res);
          let that = this
          let BeaconData = res.data.data
          let seach = {}
          let uuids = []
          for (let i = 0; i < res.data.data.length; i++) {
            uuids.push(res.data.data[i].deviceid)
          }
          console.log(uuids);
          // 初始化蓝牙设备
          wx.openBluetoothAdapter({
            success: function (blueres) {
              console.log('蓝牙适配器初始化成功', blueres)
              wx.startBeaconDiscovery({
                uuids: uuids, //多个uuid数组
                success: function (res) {
                  console.log(res);
                  wx.onBeaconUpdate(function () {
                    //设置监听事件
                    wx.getBeacons({
                      //在监听事件中获取数据
                      success: res => {
                        console.log(res.beacons) //res.beacons 为搜索到的iBeacon数据数组
                        //业务代码
                        for (let i = 0; i < BeaconData.length; i++) {
                          console.log(res.beacons[0].uuid);
                          console.log(BeaconData[i].deviceid);
                          if (res.beacons[0].uuid.toUpperCase() == BeaconData[i].deviceid.toUpperCase()) {
                            console.log(123);
                            that.setData({
                              list: BeaconData
                            })
                          }
                        }
                        console.log(seach);
                        wx.stopBeaconDiscovery({
                          success(res) {
                            console.log("已暂停搜索", res);
                          }
                        })
                      }
                    })
                  })
                }
              })
            },
            fail: function (res) {
              console.log(res);
              wx.showModal({
                title: '提醒',
                content: '系统监测未打开蓝牙,请打开后下拉刷新',
              })
              console.log(uuids);
            }
          })
        }
      })
    } else {
      Dialog.confirm({
        title: '温馨提示',
        message: '系统监测到您当前还未登录,暂时无法进行后续操作,需要先行登录',
      })
        .then(() => {
          wx.navigateTo({
            url: '../login/login'
          })
        })
        .catch(() => {
          // on cancel
        });
    }

  }
})

如果需要配合音频使用的话 可以加一个背景音频 微信小程序自带的audio已经不能用了 这里要用的是微信的背景音频

蓝牙获取成功以后将这穿代码塞进去 src需要自己调配

 const backgroundAudioManager = wx.getBackgroundAudioManager()
                          // 1、getBackgroundAudioManager的属性
                          // 1.1歌曲歌名
                          backgroundAudioManager.title = '景区讲解'
                          // 1.2歌曲专辑名称
                          // 1.3歌曲作者
                          // 1.4设置了 src 之后会自动播放
                          backgroundAudioManager.src = 'https://wwz.jingyi.icu/uploads/20240121/2732c2e6d29a551cb1d441d1d99706ae.mp3'
                          this.setData({ musicId: options.musicId })
                    
                          // 获取音乐详情
                          this.getMusicInfo(options.musicId);
                    
                          // 创建控制音乐播放的实例
                          this.backgroundAudioManager = wx.getBackgroundAudioManager();
                          // 监视音乐播放/暂停/停止
                          this.backgroundAudioManager.onPlay(() => {
                            this.changePlayState(true);
                          });
                          this.backgroundAudioManager.onPause(() => {
                            this.changePlayState(false);
                          });
                          this.backgroundAudioManager.onStop(() => {
                            this.changePlayState(false);
                          });
                        }

// 修改播放状态的功能函数
  changePlayState(isPlay) {
    // 修改音乐是否的状态
    this.setData({ isPlay })
  },

  // 获取音乐详情的功能函数
  async getMusicInfo(musicId) {
    let songData = await request('/song/detail', { ids: musicId });

    this.setData({
      song: songData.songs,
    })
  },

  // 点击播放/暂停的回调
  handleMusicPlay() {
    let isPlay = !this.data.isPlay;

    let { musicId } = this.data;
    this.musicControl(isPlay, musicId);
  },

  // 控制音乐播放/暂停的功能函数
  async musicControl(isPlay, musicId) {
    if (isPlay) { // 音乐播放
      // 获取音乐播放链接
      let musicLinkData = await request('/song/url', { id: musicId });
      let musicLink = musicLinkData.data[0].url;

      this.backgroundAudioManager.title = this.data.song.name;
      this.backgroundAudioManager.epname = this.data.song.al.name
      this.backgroundAudioManager.singer = this.data.song.aralosongName
      this.backgroundAudioManager.src = musicLink;
    } else { // 暂停音乐
      this.backgroundAudioManager.pause();
    }
  },

 再配合一下这写就可以实现进入页面播放音频啦

上一篇:【机器学习之---数学】马尔科夫链-参考


下一篇:opejdk11 java 启动流程 java main方法怎么被jvm执行-java main方法是怎么被jvm调用的