Browse Source

增加 蓝牙连接设备demo

feat-bluetooth
chenJinxu 11 months ago
parent
commit
84920aa3d6
12 changed files with 1056 additions and 57 deletions
  1. +10
    -1
      miniprogram/app.js
  2. +57
    -43
      miniprogram/app.json
  3. +140
    -0
      miniprogram/pages/bluetooth/bluetooth.js
  4. +5
    -0
      miniprogram/pages/bluetooth/bluetooth.json
  5. +22
    -0
      miniprogram/pages/bluetooth/bluetooth.wxml
  6. +59
    -0
      miniprogram/pages/bluetooth/bluetooth.wxss
  7. +552
    -0
      miniprogram/pages/devicedetail/index.js
  8. +5
    -0
      miniprogram/pages/devicedetail/index.json
  9. +103
    -0
      miniprogram/pages/devicedetail/index.wxml
  10. +90
    -0
      miniprogram/pages/devicedetail/index.wxss
  11. +9
    -11
      project.config.json
  12. +4
    -2
      project.private.config.json

+ 10
- 1
miniprogram/app.js View File

@@ -1,2 +1,11 @@
//app.js
App({})
App({
onLaunch: function () {
},
globalData: {
appid: "wx0ffb48417ce6345c",
selble: "", //所选择蓝牙的mac
bleUtil: "", //蓝牙助手
issel: "" //是否全局初始化
}
})

+ 57
- 43
miniprogram/app.json View File

@@ -1,45 +1,59 @@
{
"pages": [
"pages/home/home",
"pages/more/more",
"components/UserPrivacyDialog/index"
],
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
},
"window": {
"backgroundTextStyle": "dark",
"navigationBarBackgroundColor": "#1772cb",
"navigationBarTitleText": "随手精灵",
"navigationBarTextStyle": "white"
},
"tabBar": {
"color": "#7A7E83",
"selectedColor": "#13227a",
"backgroundColor": "#ffffff",
"list": [
{
"pagePath": "pages/home/home",
"iconPath": "images/icon/icon_home.png",
"selectedIconPath": "images/icon/icon_home_active.png",
"text": "首页"
},
{
"pagePath": "pages/more/more",
"iconPath": "images/icon/icon_more.png",
"selectedIconPath": "images/icon/icon_more_active.png",
"text": "更多"
}
]
},
"networkTimeout": {
"request": 10000,
"downloadFile": 10000
},
"debug": true,
"sitemapLocation": "sitemap.json",
"lazyCodeLoading": "requiredComponents",
"__usePrivacyCheck__": true
"pages": [
"pages/home/home",
"pages/more/more",
"components/UserPrivacyDialog/index",
"pages/bluetooth/bluetooth",
"pages/devicedetail/index"
],
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
},
"window": {
"backgroundTextStyle": "dark",
"navigationBarBackgroundColor": "#1772cb",
"navigationBarTitleText": "随手精灵",
"navigationBarTextStyle": "white"
},
"tabBar": {
"color": "#7A7E83",
"selectedColor": "#13227a",
"backgroundColor": "#ffffff",
"list": [{
"pagePath": "pages/home/home",
"iconPath": "images/icon/icon_home.png",
"selectedIconPath": "images/icon/icon_home_active.png",
"text": "首页"
},
{
"pagePath": "pages/more/more",
"iconPath": "images/icon/icon_more.png",
"selectedIconPath": "images/icon/icon_more_active.png",
"text": "更多"
},
{
"pagePath": "pages/bluetooth/bluetooth",
"iconPath": "images/icon/icon_more.png",
"selectedIconPath": "images/icon/icon_more_active.png",
"text": "蓝牙"
}
]
},
"networkTimeout": {
"request": 10000,
"downloadFile": 10000
},
"plugins": {
"ppScale-plugin": {
"version": "1.2.3",
"provider": "wx0ffb48417ce6345c"
}
},
"debug": true,
"sitemapLocation": "sitemap.json",
"lazyCodeLoading": "requiredComponents",
"__usePrivacyCheck__": true
}

+ 140
- 0
miniprogram/pages/bluetooth/bluetooth.js View File

@@ -0,0 +1,140 @@
// 示例demo
const plugin = requirePlugin('ppScale-plugin')
var app = getApp();
Page({
data: {
deviceslist:[
],
param: {
appKey:"lefu7afd0a504245139b"
}
},
onLoad() {
},
onUnload() {
// 业务结束后需要调用此方法
plugin.Blue.stop()
},
// 选择设备
selItem: function(res) {
var that = this;
var devItem = res.currentTarget.dataset.item;
var device = this.data.deviceslist.find(item => item.deviceId === devItem.deviceId)
app.globalData.selmac = device;
console.log("this.globalData",app.globalData.selmac)
that.setData({
selmac: device
})
},
refresh: function(res) {
// plugin.Blue.startBluetoothDevicesDiscovery();
plugin.Blue.start([]);
console.log("刷新", plugin);
},
doback: function(res) {
if(app.globalData.selmac){
wx.navigateTo({
url: '/pages/devicedetail/index'
})
} else if(this.data.deviceslist.length <= 0) {
wx.showModal({
title: '温馨提示',
content: `请打开手机蓝牙并且双脚站上蓝牙称扫描设备`,
showCancel: false,
})
}else {
wx.showModal({
title: '温馨提示',
content: `请选择上面要连接的设备`,
showCancel: false,
})
console.log('请选择设备')
}
},
doback2: function(res) {
if(app.globalData.selmac){
wx.navigateTo({
url: '/pages/devicedetailfood/index'
})
}else {
wx.showModal({
title: '温馨提示',
content: `请选择上面要连接的设备`,
showCancel: false,
})
console.log('请选择设备')
}
},

/**
* 刷新token
* @param {function} fn 获取token后执行函数
* @param {boolean} flag 请求刷新token
*/
async refreshToken(fn, flag) {
let bodyToken = wx.getStorageSync('bodyToken');
let bodyTokenTime = wx.getStorageSync('bodyTokenTime');
if (!bodyToken || bodyTokenTime * 1000 < +new Date() || flag) {
let res = await plugin.refreshToken({
url: 'https://uniquehealth.lefuenergy.com',
data: {
"appSecret":"AlgH7XDNWG1nABz3LSKdhaYTFUdETSBmKL4pRmD5mS4=",
"appKey": "lefu7afd0a504245139b"
}
});
console.log("res", res);
if (res.data.code == 200) {
wx.setStorageSync('bodyToken', res.data.data.token);
wx.setStorageSync('bodyTokenTime', res.data.data.expireTime);
fn();
}
} else {
fn();
}
},
getDeviceSettingList(param) {
let bodyToken = wx.getStorageSync('bodyToken');
console.log("获取最新设备列表");
console.log("param",param)
plugin.getDeviceSettingList({
url: 'https://uniquehealth.lefuenergy.com',
data: param,
header: {
"token": bodyToken,
"Accept-Language": wx.getStorageSync("lang") || 'zh'
}
}).then(this.handleResponse);
},
handleResponse(res) {
if (res.data.code == 200) {
// 配置蓝牙设备列表
console.log("res.data.data",res.data.data)
plugin.Blue.setDeviceSetting(res.data.data)
} else if (res.data.code == 401) {
// token过期 重新请求token解析数据
this.refreshToken(() => {
this.getDeviceSettingList(this.data.param)
}, true);
}
},

onShow() {
// 高于1.0.5版本 如无法扫描设备 需请求获取并设置最新设备配置列表
console.log("开始扫描周边设备");
/* this.getDeviceSettingList(this.data.param) */
plugin.Blue.setDeviceSetting([ { "createBy": null, "createTime": "2023-10-11 16:57:08", "updateBy": null, "updateTime": "2023-10-20 14:05:03", "remark": null, "id": 39, "deviceName": "LFSmart Scale", "sign": "CE4004", "advLength": 20, "macAddressStart": 7, "deviceConnectType": 2, "deviceType": 1, "deviceProtocolType": 2, "deviceCalcuteType": 3, "devicePowerType": 1, "deviceFuncType": 67, "deviceAccuracyType": 2, "deviceUnitType": "0,1,11", "calorieStatus": 1, "uhStatus": 1, "status": 0, "imgUrl": null },
])
console.log("plugin.Blue",plugin)
plugin.Blue.start([],false)
// 获取扫描设备列表
plugin.bus.subscribe("devicesList", (res) => {
console.log("devicesList'", JSON.parse(JSON.stringify(res)))
this.setData({
deviceslist: res
});
});
},

})

+ 5
- 0
miniprogram/pages/bluetooth/bluetooth.json View File

@@ -0,0 +1,5 @@
{
"usingComponents": {
}
}

+ 22
- 0
miniprogram/pages/bluetooth/bluetooth.wxml View File

@@ -0,0 +1,22 @@
<view style="width:100vw;height:3vw"></view>
<view bindtap='selItem' data-item='{{item}}' class="item_class flex_column flex_space" wx:for="{{deviceslist}}" wx:key="lkeys">
<view class="flex_row flex_space flex_vcenter">
<view class="flex_row flex_vcenter">
<text class="item_name_text">设备名:{{item.name || "未知设备"}}</text>
</view>
</view>
<view class="flex_row flex_space flex_vcenter flex_wrap" style="margin-top:3vw;display: flex;justify-content: space-between;">
<text class="item_name_text">设备MAC:{{item.deviceId}} &nbsp;信号值:{{item.RSSI || "0"}}</text>
<text hidden="{{selmac.deviceId != item.deviceId}}" mode="aspectFit" class="item_sel_img-active" ></text>
<text hidden="{{selmac.deviceId == item.deviceId}}" mode="aspectFit" class="item_sel_img" ></text>
</view>
<view class="line"></view>
</view>

<view>
<text bindtap='refresh' class='bottom_text'>刷新</text>
<button hidden="{{selmac.deviceId != ''}}" class="btn_disable">选择设备</button>
<button bindtap='doback' hidden="{{selmac.deviceId == ''}}" class="btn_normal" style="margin:0">选择设备</button>
</view>

<view style="width:100vw;height:20vh;"></view>

+ 59
- 0
miniprogram/pages/bluetooth/bluetooth.wxss View File

@@ -0,0 +1,59 @@
/* pages/selble/selble.wxss */
.item_class{
width: 94vw;
padding: 3vw 3vw 0vw 3vw;
background: #fff;
}
.ble_img{
width: 7vw;
height: 7vw;
}
.item_name_text{
margin-left: 5px;
font-size:90%;
font-family:PingFangSC-Medium;
font-weight:500;
color:rgba(51,51,51,1);
}
.item_sel_img{
width: 5vw;
height: 5vw;
background:#eee;
border-radius:50%
}
.item_sel_img-active{
width: 5vw;
height: 5vw;
background:rgba(12,135,255,1);
border-radius:50%
}
.line{
margin-top:3vw;
width: 100%;
height: 1px;
background: #E4E4E4;

}
.bottom_fix_show{
width:90vw;
padding: 4vw 5vw;
position: fixed;
bottom: 0;
background: #fff;
}
.bottom_text{
width: 100px;
display: flex;
justify-content: center;
font-size: 32rpx;
font-family:PingFangSC-Medium;
font-weight:500;
color:rgba(12,135,255,1);
z-index: 99999;
padding: 20rpx;
margin: 10px;
border: 1rpx solid;
border-radius: 20px;

}


+ 552
- 0
miniprogram/pages/devicedetail/index.js View File

@@ -0,0 +1,552 @@
// 示例demo
const plugin = requirePlugin('ppScale-plugin')
var app = getApp();
Page({
data: {
// 选择的设备对象
selmac: null,
// 是否在测量中
scaleing: false,
// 测量心率测量状态
state: 0,
// 体重数据
weight: '00.0',
// 测量单位
unit: 'kg',
// 蓝牙连接状态
devLink: false,
// 体重数据
currentWeight: 0,
// 阻抗数据
currentImpedance: 0,
// 扫描筛选出的设备
selectDev: null,
// 设备模型
deviceModel: null,
// 锁定数据
lastData: null,
// 域名值
domainValue: '',
// wifi名
wifiValue: '',
// 密码值
pwValue: '',
// 单位数组
deviceUnitArray: Object.keys(plugin.PPBluetoothDefine.PPDeviceUnit),
// 单位数组下标
deviceUnitIndex: 0,
// 网络dialog
netWorkFlag: false,
// 域名dialog
domainFlag: false,
// 单位dialog
unitFlag: false,
// 调试log列表
consoleList: [],
// 获取设备数据传参
param: {
appKey:"lefu7afd0a504245139b"
}
},

/**
* 页面卸载时调用
*/
onUnload() {
// 业务结束后需要调用此方法
plugin.Blue.stop()
},

/**
* 页面隐藏时调用
*/
onHide() {
// 业务结束后需要调用此方法
this.seeConsole('销毁蓝牙插件')
plugin.Blue.stop()
},
onShow() {
// 高于1.0.5版本 如无法扫描设备 需请求获取并设置最新设备配置列表
this.getDeviceSettingList(this.data.param)
// 高于1.0.5版本 如无法扫描设备 直接设置最新设备列表
// plugin.Blue.setDeviceSetting([{"createBy":null,"createTime":"2023-09-14 11:38:56","updateBy":null,"updateTime":"2023-10-18 16:08:39","remark":null,"id":12,"deviceName":"LFSmart Scale","sign":"CF4303","advLength":20,"macAddressStart":7,"deviceConnectType":2,"deviceType":1,"deviceProtocolType":2,"deviceCalcuteType":3,"devicePowerType":2,"deviceFuncType":75,"deviceAccuracyType":2,"deviceUnitType":"0,3","calorieStatus":1,"uhStatus":1,"status":0,"imgUrl":"https://unique.lefuenergy.com/dev/2023/09/28/4dianji_border_20230928102849A007.png"}])
// 使用节流函数包装测重事件处理函数
let throttledScroll = this.throttle(this.handleProgress, 1500);
// 记录设备对象
this.setData({
selmac: app.globalData.selmac
})
// 蓝牙状态监听
wx.onBluetoothAdapterStateChange((res) => {
if (!res.available) {
this.seeConsole('蓝牙设备断开')
}
})
// 是否开启插件日志
// plugin.Blue.visibleLog()
this.seeConsole('开启蓝牙开始扫描设备')
// 获取扫描设备列表
plugin.bus.subscribe("devicesList", (devList) => {
// 根据deviceId查找匹配设备
let selectDev = devList.find(item => {
return item.deviceId == this.data.selmac.deviceId
})
// 记录选择出设备
this.setData({
selectDev: selectDev
})
if (!this.data.deviceModel && this.data.selectDev) {
//获取与保存秤类型
this.seeConsole('获取秤端设备模型')
// 获取与记录设备模型
this.setData({
deviceModel: plugin.Blue.getDeviceModel(selectDev)
})
}
// 匹配广播秤
if (this.data.deviceModel && this.data.deviceModel.deviceConnectType == plugin.PPBluetoothDefine.PPDeviceConnectType
.PPDeviceConnectTypeBleAdv) {
this.seeConsole('设备连接广播秤')
// 广播秤 匹配与数据监听
plugin.Blue.setbroadcastDev(selectDev)
}
// 匹配连接秤
if (this.data.deviceModel && this.data.deviceModel.deviceConnectType == plugin.PPBluetoothDefine.PPDeviceConnectType
.PPDeviceConnectTypeBleConnect) {
// 停止扫描
this.seeConsole('停止扫描')
plugin.Blue.stopBluetoothDevicesDiscovery()
// 连接设备
this.seeConsole('开始连接设备')
plugin.Blue.createBLEConnection(selectDev)
}
});
// 设备链接回调
plugin.bus.subscribe("deviceConnect", (res) => {
this.seeConsole('成功连接设备')
// 开启监听蓝牙秤数据 必填
plugin.ScaleAction.startDataProgress()
setTimeout(()=>{
plugin.ScaleAction.syncDeviceSetting({
unit: +this.data.deviceUnitIndex,
})
},800)
});

// 设备链接回调
plugin.bus.subscribe("devicesModel", (res) => {
// console.log("res",res)
});

// 监听秤端数据回调
plugin.bus.subscribe("progressData", (res) => {
throttledScroll()
// console.log("selectDev",this.data.selectDev)
// console.log("selectDev",plugin.Blue.getDeviceModel(this.data.selectDev))
if (this.data.selectDev) {
// 广播秤类型连接
if (this.data.deviceModel && this.data.deviceModel.deviceConnectType == plugin.PPBluetoothDefine.PPDeviceConnectType
.PPDeviceConnectTypeBleAdv) {
this.setData({
devLink: true
})
}
if (res.weight > 0) {
// 有数据显示为测量中
this.setData({
scaleing: true
})
// 记录体重数据
this.setData({
weight: (res.weight / 100).toFixed(2)
})
// 获取测量过程秤显示单位
this.setData({
unit: this.getKeyByUnit(res.unit)
})
} else {
// 无数据显示为未测量
this.setData({
state: 0
})
// 无数据显示为未测量
this.setData({
scaleing: false
})
}
if (res.isHeartRating) {
// 心率测量中状态设置
this.setData({
state: 1
})
}
}
});

// 设备自动断开监听
plugin.bus.subscribe("deviceWillDisconnect", (res) => {
this.seeConsole('设备自动断开连接')
// 设备自动断连后重连
this.setData({
devLink: false
})
// 重新连接方法
this.reconnectDevice()
})

// 锁定数据
plugin.bus.subscribe("lockData", (res) => {
this.seeConsole('锁定测量数据:' + JSON.stringify(res))
// 测量结束
this.setData({
currentWeight: (res.weight / 100).toFixed(2)
})
// 记录阻抗值
this.setData({
currentImpedance: res.resistance
})
// 记录测量结果数据
this.setData({
lastData: JSON.stringify(res)
})
// 状态还原
this.setData({
state: 0
})
this.setData({
scaleing: false
})

});
// 蓝牙连接状态监听
plugin.bus.subscribe("connectState", (res) => {
// 连接状态
if (this.data.selectDev) {
if (res == plugin.BLUE_STATE.TRANSMITDATA || res == plugin.BLUE_STATE.LOCKDATA || res == plugin.BLUE_STATE
.HEARTRATING || res ==
plugin.BLUE_STATE.CONNECTSUCCESS || res == plugin.BLUE_STATE.WIFISUCCESS) {
this.setData({
devLink: true
})
} else {
this.setData({
devLink: false
})
}
// 连接失败则重新连接
if (res == plugin.BLUE_STATE.CONNECTFAILED) {
this.reconnectDevice()
}
}
});
// 秤端操作回调
plugin.bus.subscribe("syncDeviceSetting", (res) => {
this.seeConsole('成功配置设备')
})
plugin.bus.subscribe("snData", () => {
this.seeConsole('成功配置wifi网络')
})
plugin.bus.subscribe("domainChange", () => {
this.seeConsole('成功更新秤端请求服务器域名')
})
plugin.bus.subscribe("syncDeviceTimeSuccess", () => {
this.seeConsole('成功同步时间')
})
plugin.bus.subscribe("syncDeviceHistorySuccess", (e) => {
console.log("历史列表",e)
this.seeConsole('成功同步历史')
})
plugin.bus.subscribe("deviceInfo", (res) => {
console.log("deviceInfo", res)
this.seeConsole('设备信息')
})

plugin.bus.subscribe("deleteDeviceHistoryDataSuccess", () => {
this.seeConsole('成功删除同步历史')
})
// 开始扫描
plugin.Blue.start([], false)

},
connect() {
this.seeConsole('开始连接设备')
if (this.data.deviceModel && this.data.deviceModel.deviceConnectType == plugin.PPBluetoothDefine.PPDeviceConnectType
.PPDeviceConnectTypeBleConnect) {
// 连接设备
plugin.Blue.createBLEConnection(this.data.selectDev)
}
},
disconnect() {
this.seeConsole('断开连接')
plugin.Blue.disconnect()
},
switchUnit() {
this.setData({
unitFlag: true
})
},
setUserMes() {
this.seeConsole('下发用户信息')
if (this.data.devLink) {
// 根据设备已有单位切换
plugin.ScaleAction.syncDeviceSetting({
age: 28,
height: 167,
gender: 1,
})
} else {
this.seeConsole('停止下发用户信息')
wx.showToast({
title: '需要连接蓝牙秤',
icon: 'error',
duration: 1000,
mask: true
})
}
},
syncHistory() {
this.seeConsole('同步历史')
if (this.data.devLink) {
// 根据设备已有单位切换
plugin.ScaleAction.fetchDeviceHistoryData()
} else {
this.seeConsole('停止同步历史')
wx.showToast({
title: '需要连接蓝牙秤',
icon: 'error',
duration: 1000,
mask: true
})
}
},
deleteSyncHistory() {
this.seeConsole('删除同步历史')
if (this.data.devLink) {
// 根据设备已有单位切换
plugin.ScaleAction.deleteDeviceHistoryData()
} else {
this.seeConsole('停止删除同步历史')
wx.showToast({
title: '需要连接蓝牙秤',
icon: 'error',
duration: 1000,
mask: true
})
}
},
syncTime() {
this.seeConsole('同步时间')
if (this.data.devLink) {
// 根据设备已有单位切换
plugin.ScaleAction.startDataProgress()
} else {
this.seeConsole('停止同步时间')
wx.showToast({
title: '需要连接蓝牙秤',
icon: 'error',
duration: 1000,
mask: true
})
}
},
configureNetwork() {
this.setData({
netWorkFlag: true
})
},
modifyDNS() {
this.setData({
domainFlag: true
})
},
/**
* 重新连接设备
*/
reconnectDevice() {
this.seeConsole('尝试重新连接')
// 设备断开连接后重新连接
plugin.Blue.disconnect((disres) => {
if (disres.errCode == 0) {
this.seeConsole('开始扫描设备')
plugin.Blue.startBluetoothDevicesDiscovery()
} else {
this.reconnectDevice()
}
})
},

/**
* 根据单位值获取设备单位描述
* @param {number} unit 单位值
* @return {string} 单位描述
*/
getKeyByUnit(unit) {
for (const key in plugin.PPBluetoothDefine.PPDeviceUnit) {
if (plugin.PPBluetoothDefine.PPDeviceUnit[key] === unit) {
return key;
}
}
return null;
},
bindKeyInput: function (e) {
this.setData({
inputValue: e.detail.value
})
},
bindDomainInput: function (e) {
this.setData({
domainValue: e.detail.value
})
},
bindWifiInput: function (e) {
this.setData({
wifiValue: e.detail.value
})
},
bindPasswdInput: function (e) {
this.setData({
pwValue: e.detail.value
})
},
bindPickerChange: function (e) {
this.setData({
deviceUnitIndex: e.detail.value
});
},
changeUnit() {
this.seeConsole('切换单位')
if (this.data.devLink) {
// 根据设备已有单位切换
plugin.ScaleAction.syncDeviceSetting({
unit: +this.data.deviceUnitIndex,
})

} else {
this.seeConsole('停止切换单位')
wx.showToast({
title: '需要连接蓝牙秤',
icon: 'error',
duration: 1000,
mask: true
})
}
this.setData({
unitFlag: false
})
},
configWifi() {
this.seeConsole('开始配置wifi')
// 配置wifi
if (this.data.devLink && this.data.wifiValue && this.data.pwValue) {
console.log("this.data.wifiValue && this.data.pwValue",this.data.wifiValue , this.data.pwValue)
plugin.ScaleAction.configWifi(this.data.wifiValue, this.data.pwValue)
} else {
this.seeConsole('停止配置wifi')
wx.showToast({
title: '需要连接蓝牙秤或wifi名称或密码',
duration: 1000,
mask: true
})
}
this.setData({
netWorkFlag: false
})
},
changeDomain() {
this.seeConsole('开始配置域名')
// 配置域名
if (this.data.domainValue != '' && this.data.devLink) {
plugin.ScaleAction.configDomain(this.data.domainValue.replace(/^(https?:\/\/)/, ""))
} else {
this.seeConsole('停止配置域名')
wx.showToast({
title: '需要连接蓝牙秤或输入域名',
duration: 1000,
mask: true
})
}
this.setData({
domainFlag: false
})
},
// 节流函数
throttle: function (func, delay) {
let timer = null;
let lastTime = 0;
return function () {
const now = Date.now();
if (now - lastTime >= delay) {
func();
lastTime = now;
} else {
clearTimeout(timer);
timer = setTimeout(func, delay);
}
};
},
// 测量中体重记录
handleProgress: function () {
this.seeConsole('正在测量体重:' + this.data.weight)
},
seeConsole(string) {
this.setData({
consoleList: [string].concat(this.data.consoleList)
})
},
/**
* 清除控制台
*/
clear() {
this.setData({
consoleList: []
})
},
/**
* 刷新token
* @param {function} fn 获取token后执行函数
* @param {boolean} flag 请求刷新token
*/
async refreshToken(fn, flag) {
let bodyToken = wx.getStorageSync('bodyToken');
let bodyTokenTime = wx.getStorageSync('bodyTokenTime');
if (!bodyToken || bodyTokenTime * 1000 < +new Date() || flag) {
let res = await plugin.refreshToken({
url: 'https://uniquehealth.lefuenergy.com',
data: {
"appSecret":"AlgH7XDNWG1nABz3LSKdhaYTFUdETSBmKL4pRmD5mS4=",
"appKey": "lefu7afd0a504245139b"
}
});
console.log("res", res);
if (res.data.code == 200) {
wx.setStorageSync('bodyToken', res.data.data.token);
wx.setStorageSync('bodyTokenTime', res.data.data.expireTime);
fn();
}
} else {
fn();
}
},
getDeviceSettingList(param) {
let bodyToken = wx.getStorageSync('bodyToken');
plugin.getDeviceSettingList({
url: 'https://uniquehealth.lefuenergy.com',
data: param,
header: {
"token": bodyToken,
"Accept-Language": wx.getStorageSync("lang") || 'zh'
}
}).then(this.handleResponse);
},
handleResponse(res) {
if (res.data.code == 200) {
// 配置蓝牙设备列表
plugin.Blue.setDeviceSetting(res.data.data)
} else if (res.data.code == 401) {
// token过期 重新请求token解析数据
this.refreshToken(() => {
this.getDeviceSettingList(this.data.param)
}, true);
}
},
})

+ 5
- 0
miniprogram/pages/devicedetail/index.json View File

@@ -0,0 +1,5 @@
{
"usingComponents": {
"ppScale": "plugin://ppScale-plugin/ppScale"
}
}

+ 103
- 0
miniprogram/pages/devicedetail/index.wxml View File

@@ -0,0 +1,103 @@
<!--pages/selble/selble.wxml-->
<view class="command">
<view class="device-item">
<view>连接状态:</view>
<view>{{devLink?'已连接':'未连接'}}</view>
</view>
<view class="device-item">
<view>设备名:</view>
<view>{{selmac.name}}</view>
</view>
<view class="device-item">
<view>设备mac:</view>
<view>{{selmac.deviceId}}</view>
</view>
<view class="device-item">
<view>体重:</view>
<view>{{weight}}</view>
</view>
<view class="device-item">
<view>当前单位:</view>
<view>{{unit}}</view>
</view>
<view class="device-item">
<view>心率测量状态:</view>
<view>{{state==0?'未测量心率':'正在测量心率'}}</view>
</view>
<view class="device-item">
<view>体重状态:</view>
<view>{{scaleing?'有体重数据':'无体重数据'}}</view>
</view>
<!-- <view class="device-item">
<view>结果数据:</view>
<view class="device-result">{"isPlus": true, "isOverload": false, "weight": 1405, "resistance": 43181, "isEnd": true, "isHeartRating": false, "heartRate": 0, "unit": 0, "dateStr": "2023-08-01 09:48:52"}</view>
</view> -->
</view>

<view class="command-line">
<view wx:for="{{consoleList}}">{{item}}</view>
</view>
<view style="width:100vw;height:3vw"></view>
<view class="event-bar">
<button type="default" bindtap="connect">连接</button>
<button type="default" bindtap="disconnect">断开连接</button>
<button type="default" bindtap="syncHistory">同步历史</button>
<button type="default" bindtap="deleteSyncHistory">删除同步历史</button>
<button type="default" bindtap="syncTime">同步时间</button>
<button type="default" bindtap="switchUnit">切换单位</button>
<button type="default" bindtap="configureNetwork">配置网络</button>
<button type="default" bindtap="modifyDNS">修改服务器域名</button>
<button type="default" bindtap="setUserMes">下发用户信息</button>
</view>
<view>

</view>




<view class="dialog" wx:if="{{unitFlag}}">
<view class="dialog-content">
<view class="dialog-title">切换单位</view>
<view>
<picker mode="selector" range="{{deviceUnitArray}}" bindchange="bindPickerChange">
<view class="picker">
点击选择单位:{{deviceUnitArray[deviceUnitIndex]}}
</view>
</picker>
</view>
<button type="default" bindtap="changeUnit">切换最新单位</button>
</view>
</view>
<view class="dialog" wx:if="{{domainFlag}}">
<view class="dialog-content">
<view>
<view class="dialog-title">域名配置</view>
<view>域名:{{domainValue}}</view>
<view>
<view>
<input class="weui-input" bindinput="bindDomainInput" placeholder="输入域名" />
</view>
</view>
</view>
<button type="default" bindtap="changeDomain">更新服务器域名</button>

</view>
</view>
<view class="dialog" wx:if="{{netWorkFlag}}">
<view class="dialog-content">
<view class="dialog-title">wifi网络配置</view>
<view>
<!-- <view >wifi和密码:{{wifiValue}}-{{pwValue}}</view> -->
<view>
<view>wifi名称<input class="weui-input" bindinput="bindWifiInput" placeholder="输入wifi名称" />
</view>
<view>密码: <input class="weui-input" bindinput="bindPasswdInput" placeholder="输入wifi密码" />
</view>
</view>
</view>
<button class="dialog-button" bindtap="configWifi">配置网络</button>
</view>
</view>

<button class="clear-btn" bindtap="clear">清除控制台</button>

+ 90
- 0
miniprogram/pages/devicedetail/index.wxss View File

@@ -0,0 +1,90 @@
.device-item {
display: flex;
justify-content: space-between;
}
.device-result {
flex:1;
text-align: center;
}

.command{
background-color: black;
color: white;
margin: 24rpx;
padding: 10rpx;
margin-bottom: 0;
}
.command-line {
background-color: black;
color: white;
font-family: monospace;
padding: 20rpx;
border: 1rpx solid white;
padding: 10rpx;
overflow: auto;
height: 350rpx;
margin: 24rpx;
margin-top: 0;
overflow-y: scroll;
text-align: right;
}
.command-line view {
word-wrap: break-word;
word-break: normal;
}
.event-bar {
display: flex;
justify-content: space-between;
padding: 24rpx;
flex-wrap: wrap;
}
.event-bar button {
width: 45%;
background-color: #f8f8f8;
color: #000;
text-align: center;
padding: 10rpx;
border:1rpx #ccc solid;
margin-bottom: 10rpx;
}
.dialog {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 1;
}
.dialog-content {
background-color: #fff;
padding: 20rpx;
border-radius: 10rpx;
box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.2);
width: 80%;
}
.dialog-title {
font-size: 35rpx;
font-weight: bold;
margin-bottom: 10rpx;
text-align: center;
}
.dialog-message {
font-size: 30rpx;
margin-bottom: 20rpx;
}
.dialog-button {
padding: 10rpx 20rpx;
border-radius: 5rpx;
font-size: 30rpx;
}
.clear-btn {
margin: 24rpx;
}

+ 9
- 11
project.config.json View File

@@ -20,8 +20,6 @@
"uploadWithSourceMap": true,
"compileHotReLoad": false,
"useMultiFrameRuntime": true,
"useApiHook": true,
"useApiHostProcess": true,
"babelSetting": {
"ignore": [],
"disablePlugins": [],
@@ -37,11 +35,9 @@
"showES6CompileOption": false,
"useCompilerPlugins": false,
"minifyWXML": true,
"useStaticServer": true
"useStaticServer": true,
"ignoreUploadUnusedFiles": true
},
"appid": "wx64cc3af90544c0cd",
"projectname": "cloudbase",
"libVersion": "2.14.1",
"condition": {
"miniprogram": {
"list": [
@@ -55,13 +51,15 @@
},
"compileType": "miniprogram",
"srcMiniprogramRoot": "miniprogram/",
"packOptions": {
"ignore": [],
"include": []
},
"editorSetting": {
"tabIndent": "insertSpaces",
"tabSize": 4
},
"description": "项目配置文件,详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html"
"description": "项目配置文件,详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html",
"packOptions": {
"ignore": [],
"include": []
},
"appid": "wx7edd2168a73f701f",
"projectname": "cloudbase"
}

+ 4
- 2
project.private.config.json View File

@@ -1,6 +1,8 @@
{
"setting": {
"compileHotReLoad": true
"compileHotReLoad": true,
"urlCheck": false
},
"description": "项目私有配置文件。此文件中的内容将覆盖 project.config.json 中的相同字段。项目的改动优先同步到此文件中。详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html"
"description": "项目私有配置文件。此文件中的内容将覆盖 project.config.json 中的相同字段。项目的改动优先同步到此文件中。详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html",
"libVersion": "2.33.0"
}

Loading…
Cancel
Save