微信小程序获取蓝牙信标

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();
    }
  },

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

相关推荐
老华带你飞5 小时前
考研论坛平台|考研论坛小程序系统|基于java和微信小程序的考研论坛平台小程序设计与实现(源码+数据库+文档)
java·vue.js·spring boot·考研·小程序·毕设·考研论坛平台小程序
毕设源码-钟学长6 小时前
【开题答辩全过程】以 基于微信小程序的美发服务系统的设计与实现为例,包含答辩的问题和答案
微信小程序·小程序
canglingyue7 小时前
微信小程序罗盘功能开发指南
微信小程序·小程序
三脚猫的喵10 小时前
微信小程序中实现AI对话、生成3D图像并使用xr-frame演示
前端·javascript·ai作画·微信小程序
2501_9151063211 小时前
App Store 软件上架全流程详解,iOS 应用发布步骤、uni-app 打包上传与审核要点完整指南
android·ios·小程序·https·uni-app·iphone·webview
海绵宝宝不喜欢侬11 小时前
UniApp微信小程序-实现蓝牙功能
微信小程序·uni-app
开发加微信:hedian11613 小时前
微信推客小程序系统开发技术实践
微信·小程序
Python大数据分析14 小时前
uniapp微信小程序商品列表数据分页+本地缓存+下拉刷新+图片懒加载
缓存·微信小程序·uni-app
小白_ysf14 小时前
uniapp和vue3项目中引入echarts 、lime-echart(微信小程序、H5等)
微信小程序·uni-app·echarts·h5·lime-echart
imHere·14 小时前
UniApp 分包异步化配置及组件引用解决方案
微信小程序·uni-app·分包