当前位置: 首页 > news >正文

微信小程序如何搜索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设备的uuidsuccess: 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 = thisif (res.beacons.length > 0) {wx.stopBeaconDiscovery({success: function (res) {console.log('停止搜索 iBeacon 设备',res);},fail: function (res) {console.log('停止搜索 iBeacon 设备失败');}});let devices = this.data.devicesdevices.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 

http://www.lryc.cn/news/289614.html

相关文章:

  • JVM篇:垃圾回收算法
  • 2024年数学建模美赛 分析与编程
  • 05-Nacos-配置中心接入
  • 服务端开发小记02——Maven
  • DjangoURL调度器(一)
  • Typora 无法导出 pdf 问题的解决
  • uniapp封装公共的方法或者数据请求方法
  • SpringBoot AOP应用(公共字段填充)
  • NIO案例-聊天室
  • 文心一言情感关怀之旅
  • mac电脑安卓文件传输工具:Android File Transfer直装版
  • 第九篇【传奇开心果系列】beeware的toga开发移动应用示例:人口普查手机应用
  • 14.5 Flash查询和添加数据库数据
  • [C#]winform部署yolov7+CRNN实现车牌颜色识别车牌号检测识别
  • VBA技术资料MF111:将表对象转换为正常范围
  • Nginx代理服务器、HTTP调度、TCP/UDP调度、Nginx优化、HTTP错误代码、状态页面、压力测试
  • 从 React 到 Qwik:开启高效前端开发的新篇章
  • 【lodash.js】非常好用高性能的 JavaScript 实用工具库,防抖,深克隆,排序等
  • JS中的try...catch
  • 选择海外云手机需要考虑什么?
  • 物联网协议Coap之C#基于Mozi的CoapClient调用解析
  • java中如何使用Lambda表达式(一)
  • C++继承详解
  • docker数据卷的使用
  • 2024獬豸杯完整Writeup
  • Vue学习笔记之应用创建和基础知识
  • CSS3基础知识总结
  • 80.网游逆向分析与插件开发-背包的获取-自动化助手显示物品数据1
  • Python第三方扩展库NumPy
  • Dockerfile简介和基础实践