// pages/search/search.js const tqlSDK = getApp().globalData.tqlSDK const app = getApp() import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast' const event = require('../../tqlsdk/event') Page({ /** * 页面的初始数据 */ data: { blueToothList: [], refreshFlag: false, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.searchBlueTooth(); event.remove('foundDevices') event.on('foundDevices', this, data => { this.setData({ blueToothList: data }) }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, // 搜索附近的蓝牙 searchBlueTooth() { tqlSDK.searchBlueTooth(this.blueToothList) }, // 查找到的设备列表 blueToothList() { tqlSDK.showBlueToothList(this) }, // 连接蓝牙 connectBlueTooth(e) { Toast.clear() Toast.loading({ message: '正在连接', forbidClick: true, duration: 0 }) tqlSDK.stopBlueToothSearch(() => {}) console.log(e) let deviceId = e.currentTarget.dataset.deviceid let penName = e.currentTarget.dataset.devicename let mac = e.currentTarget.dataset.mac setTimeout(() => { tqlSDK.createBlueToothConnection(deviceId, () => { app.globalData.deviceId = deviceId app.globalData.deviceName = penName app.globalData.deviceMac = mac // event.emit('blueToothConnect', true) this.getBlueToothService(deviceId) }, () => { Toast.clear() }) }, 1000) }, // 获取蓝牙设备服务 getBlueToothService(deviceId) { tqlSDK.getBlueToothService(deviceId, () => { setTimeout(() => { Toast.clear(); Toast.success({ message: "连接成功", duration: 2500, onClose: () => { wx.navigateBack({ delta: 0, success: () => { // setTimeout(() => { tqlSDK.getPenOfflineDataSize() // },100) } }) } }) }, 500); }) }, //刷新列表 refreshList() { setTimeout(() => { this.setData({ refreshFlag: false }) tqlSDK.allDevices.foundDevices = []; this.searchBlueTooth(); }, 1500) } })