微信小程序如何搜索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 

相关推荐

  1. 程序呼叫设备

    2024-01-08 08:32:02       61 阅读
  2. 程序如何分包管理

    2024-01-08 08:32:02       59 阅读
  3. 程序如何分包管理

    2024-01-08 08:32:02       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-08 08:32:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-08 08:32:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-08 08:32:02       82 阅读
  4. Python语言-面向对象

    2024-01-08 08:32:02       91 阅读

热门阅读

  1. 基于SpringBoot的旅游网站设计

    2024-01-08 08:32:02       60 阅读
  2. 多线程忙循环是什么

    2024-01-08 08:32:02       54 阅读
  3. 数据结构-怀化学院期末题

    2024-01-08 08:32:02       49 阅读
  4. springMVC获取请求参数的方式

    2024-01-08 08:32:02       63 阅读
  5. PyTorch的核心模块介绍

    2024-01-08 08:32:02       57 阅读
  6. C语言基本框架及其含义,C入门

    2024-01-08 08:32:02       58 阅读
  7. js 如何判断对象自身为空?

    2024-01-08 08:32:02       61 阅读
  8. 神经网络中参数与超参数的区别是什么?

    2024-01-08 08:32:02       53 阅读
  9. 学习尚硅谷Vue的TodoList案例下半部分总结

    2024-01-08 08:32:02       60 阅读