微信小程序获取蓝牙信标

javascript 复制代码
/**
 * 搜索设备界面
 */
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需要自己调配

javascript 复制代码
 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);
                          });
                        }
javascript 复制代码
// 修改播放状态的功能函数
  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();
    }
  },

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

相关推荐
kidding7231 天前
旋转大转盘小程序
前端·css·微信小程序·小程序·前端框架
码农客栈1 天前
小程序学习(二十九)之项目打包
小程序
凡科建站1 天前
2026年艺术类教育小程序开发平台有哪些?艺术类教育小程序开发平台推荐
小程序
LT10157974442 天前
2026年在线兼容性测试工具推荐|零部署网页 / APP / 小程序实测对比
测试工具·小程序
码农客栈2 天前
小程序学习(二十八)之“订单列表”
小程序
这是个栗子3 天前
uni-app 微信小程序开发:常用事件指令(@xxx)(一)
微信小程序·小程序·uni-app
2601_962344623 天前
计算机毕业设计之基于大数据的投保数据的分析系统的设计与实现
大数据·人工智能·深度学习·机器学习·信息可视化·小程序·课程设计
黑黑的独立开发笔记3 天前
「 简记往来」第十五篇:小程序性能优化——首屏从2.5秒到1.2秒
性能优化·小程序·首屏优化·分包加载·setdata·简记往来
tcdos5 天前
不止扫码 — 微信生态深度融合(登录 + 支付 + 消息)
后端·微信小程序
小徐_23336 天前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app