微信小程序如何搜索iBeacon设备

简介: 微信小程序如何搜索iBeacon设备

1.首先在utils文件夹下创建bluetooth.js和ibeacon.js


2.在 bluetooth.js文件中写入

module.exports = {
  initBluetooth: function () {
    // 初始化蓝牙模块
    wx.openBluetoothAdapter({
      success: function (res) {
        console.log('蓝牙模块初始化成功');
      },
      fail: function (res) {
        console.log('蓝牙模块初始化失败');
      }
    });
  }
}

3.在 ibeacon.js中写入

module.exports = {
  registerIBeaconCallback: function (callback) {
    // 注册iBeacon回调函数
    wx.onBeaconUpdate(function (res) {
      callback(res);
    });
    // 开始搜索iBeacon设备
    wx.startBeaconDiscovery({
      uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647826'], // iBeacon设备的uuid
      success: function (res) {
        console.log('开始搜索iBeacon设备');
      },
      fail: function (res) {
        console.log('搜索iBeacon设备失败');
      }
    });
  },
}

4.在需要获取蓝牙的wxml中的page上方引入

const bluetooth = require('../../utils/bluetooth');
const ibeacon = require('../../utils/ibeacon');

5.在下方onshow事件中调用

 bluetooth.initBluetooth();
    // 注册iBeacon回调函数
    ibeacon.registerIBeaconCallback(this.handleIBeacon);

6.在page内写入获取ibeacon设备代码及停止搜索

  handleIBeacon: function (res) {
    // 处理搜索到的iBeacon设备    
    let _this = this
    if (res.beacons.length > 0) {
      wx.stopBeaconDiscovery({
        success: function (res) {
          console.log('停止搜索 iBeacon 设备',res);
        },
        fail: function (res) {
          console.log('停止搜索 iBeacon 设备失败');
        }
      });
      let devices = this.data.devices
      devices.push(res.beacons[0].uuid)
      this.setData({
        devices: devices
      })
      console.log(this.data.devices);
    }
    // 在这里可以对搜索到的设备进行进一步处理
  },

7.最后如果想检测蓝牙是否连接可以加上 wx.onBluetoothAdapterStateChange事件

 wx.onBluetoothAdapterStateChange(function(res) {
      console.log("蓝牙适配器状态变化", res);
      if (res.available) {
        console.log("蓝牙适配器可用");
      } else {
        console.log("蓝牙适配器不可用");
      }
    });

8.如果出现报错,则需要安装 npm install @babel/runtime,或检查是否正确引用了@babel/runtime/helpers/defineProperty.js 模块

module '@babel/runtime/helpers/defineProperty.js' is not defined, require args is '../../@babel/runtime/helpers/defineProperty' 
 Error: module '@babel/runtime/helpers/defineProperty.js' is not defined, require args is 
相关文章
|
3月前
|
小程序
揭秘会员卡包和搜索直达新能力,让用户更快找到你的小程序
揭秘会员卡包和搜索直达新能力,让用户更快找到你的小程序
124 0
|
3月前
|
小程序 安全 API
社区每周丨小程序基础API新增获取设备、系统等多个接口
社区每周丨小程序基础API新增获取设备、系统等多个接口
108 0
|
3月前
|
移动开发 小程序 IDE
【社区每周】小程序搜索直达能力全面开放;IDE3.0稳定版正式发布(2022年6月第二期)
【社区每周】小程序搜索直达能力全面开放;IDE3.0稳定版正式发布(2022年6月第二期)
22 0
|
5月前
|
JSON 小程序 JavaScript
|
5月前
|
前端开发 小程序
微信小程序绘制canvas时在不同 设备上的大小不同的问题
微信小程序绘制canvas时在不同 设备上的大小不同的问题
104 0
|
5月前
|
Web App开发 Linux Android开发
Chrome谷歌浏览器的WeChat微信模拟器,既可以设置模拟很多型号的手机设备Mozilla
Chrome谷歌浏览器的WeChat微信模拟器,既可以设置模拟很多型号的手机设备Mozilla
|
3月前
|
小程序 JavaScript 前端开发
【每周一个小技巧】支付宝小程序如何跳转到支付宝搜索页面
【每周一个小技巧】支付宝小程序如何跳转到支付宝搜索页面
40 0
|
4月前
|
小程序 JavaScript Java
基于微信小程序的公司后勤服务(设备)系统设计与实现(源码+lw+部署文档+讲解等)
基于微信小程序的公司后勤服务(设备)系统设计与实现(源码+lw+部署文档+讲解等)
26 0
|
5月前
|
小程序
微信小程序获取数据的方法——iBeacon蓝牙
微信小程序获取数据的方法——iBeacon蓝牙
|
5月前
|
存储 小程序
微信小程序ibeacon搜索功能制作
微信小程序ibeacon搜索功能制作
32 0