5-13 H09身高体重模式
This commit is contained in:
parent
c76d3ea1fa
commit
9911418cf9
|
|
@ -1,432 +1,443 @@
|
|||
const util = require("../../utils/util");
|
||||
const {
|
||||
inArray,
|
||||
ab2hex
|
||||
inArray,
|
||||
ab2hex
|
||||
} = util
|
||||
|
||||
Page({
|
||||
data: {
|
||||
devices: [],
|
||||
connected: false,
|
||||
cmd: '',
|
||||
name: '',
|
||||
weight: "",
|
||||
text: "",
|
||||
imp: "",
|
||||
height: '',
|
||||
isType: false,
|
||||
sendVal: true,
|
||||
pwdModal: false,
|
||||
uuid1: null,
|
||||
uuid2: null,
|
||||
uuid3: null,
|
||||
deviceId: null,
|
||||
serviceId: null
|
||||
},
|
||||
onLoad: function() {
|
||||
// let value = "A90026051401020001439A";
|
||||
// let num = value.substring(18, 19)
|
||||
// let type = value.substring(8, 10)
|
||||
// let imp = parseInt(value.substring(10, 14), 16)
|
||||
// console.log("16进制转化:", value, num, type, imp);
|
||||
},
|
||||
// 初始化蓝牙模块
|
||||
openBluetoothAdapter() {
|
||||
wx.openBluetoothAdapter({
|
||||
success: (res) => {
|
||||
console.log('openBluetoothAdapter success', res)
|
||||
wx.showToast({
|
||||
title: '蓝牙连接中',
|
||||
icon: "none"
|
||||
})
|
||||
this.startBluetoothDevicesDiscovery()
|
||||
},
|
||||
fail: (res) => {
|
||||
if (res.errCode === 10001) {
|
||||
wx.showToast({
|
||||
title: '请打开蓝牙',
|
||||
icon: "none"
|
||||
})
|
||||
// 监听本机蓝牙状态变化的事件
|
||||
wx.onBluetoothAdapterStateChange((res) => {
|
||||
console.log('onBluetoothAdapterStateChange', res)
|
||||
if (res.available) {
|
||||
this.startBluetoothDevicesDiscovery()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
data: {
|
||||
devices: [],
|
||||
connected: false,
|
||||
cmd: '',
|
||||
name: '',
|
||||
weight: "",
|
||||
text: "",
|
||||
imp: "",
|
||||
height: '',
|
||||
isType: false,
|
||||
sendVal: true,
|
||||
pwdModal: false,
|
||||
uuid1: null,
|
||||
uuid2: null,
|
||||
uuid3: null,
|
||||
deviceId: null,
|
||||
serviceId: null
|
||||
},
|
||||
onLoad: function() {
|
||||
// let value = "A90026051401020001439A";
|
||||
// let num = value.substring(18, 19)
|
||||
// let type = value.substring(8, 10)
|
||||
// let imp = parseInt(value.substring(10, 14), 16)
|
||||
// console.log("16进制转化:", value, num, type, imp);
|
||||
},
|
||||
// 初始化蓝牙模块
|
||||
openBluetoothAdapter() {
|
||||
wx.openBluetoothAdapter({
|
||||
success: (res) => {
|
||||
console.log('openBluetoothAdapter success', res)
|
||||
wx.showToast({
|
||||
title: '蓝牙连接中',
|
||||
icon: "none"
|
||||
})
|
||||
this.startBluetoothDevicesDiscovery()
|
||||
},
|
||||
fail: (res) => {
|
||||
if (res.errCode === 10001) {
|
||||
wx.showToast({
|
||||
title: '请打开蓝牙',
|
||||
icon: "none"
|
||||
})
|
||||
// 监听本机蓝牙状态变化的事件
|
||||
wx.onBluetoothAdapterStateChange((res) => {
|
||||
console.log('onBluetoothAdapterStateChange', res)
|
||||
if (res.available) {
|
||||
this.startBluetoothDevicesDiscovery()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 开始搜寻附近的蓝牙外围设备
|
||||
startBluetoothDevicesDiscovery() {
|
||||
if (this._discoveryStarted) {
|
||||
return
|
||||
}
|
||||
this._discoveryStarted = true
|
||||
wx.startBluetoothDevicesDiscovery({
|
||||
allowDuplicatesKey: true,
|
||||
interval: 1000, //上报设备的间隔
|
||||
services: [
|
||||
"FFE0",
|
||||
],
|
||||
success: (res) => {
|
||||
console.log('startBluetoothDevicesDiscovery success', res)
|
||||
this.onBluetoothDeviceFound()
|
||||
},
|
||||
})
|
||||
},
|
||||
// 停止搜寻附近的蓝牙外围设备
|
||||
stopBluetoothDevicesDiscovery() {
|
||||
wx.stopBluetoothDevicesDiscovery()
|
||||
},
|
||||
// 开始搜寻附近的蓝牙外围设备
|
||||
startBluetoothDevicesDiscovery() {
|
||||
if (this._discoveryStarted) {
|
||||
return
|
||||
}
|
||||
this._discoveryStarted = true
|
||||
wx.startBluetoothDevicesDiscovery({
|
||||
allowDuplicatesKey: true,
|
||||
interval: 1000, //上报设备的间隔
|
||||
services: [
|
||||
"FFE0",
|
||||
],
|
||||
success: (res) => {
|
||||
console.log('startBluetoothDevicesDiscovery success', res)
|
||||
this.onBluetoothDeviceFound()
|
||||
},
|
||||
})
|
||||
},
|
||||
// 停止搜寻附近的蓝牙外围设备
|
||||
stopBluetoothDevicesDiscovery() {
|
||||
wx.stopBluetoothDevicesDiscovery()
|
||||
},
|
||||
|
||||
// 找到新设备的事件
|
||||
onBluetoothDeviceFound() {
|
||||
wx.onBluetoothDeviceFound((res) => {
|
||||
res.devices.forEach(device => {
|
||||
if (device.name.indexOf('AiLink_') != -1) {
|
||||
wx.stopBluetoothDevicesDiscovery() //搜索到名称为“AiLink_”的蓝牙后,停止搜寻附近的蓝牙
|
||||
const foundDevices = this.data.devices
|
||||
const idx = inArray(foundDevices, 'deviceId', device.deviceId)
|
||||
const data = {}
|
||||
let buff = device.advertisData.slice(-6)
|
||||
device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址,这是由于iOS不直接返回mac地址
|
||||
let tempMac = Array.from(device.mac)
|
||||
tempMac.reverse()
|
||||
device.macAddr = ab2hex(tempMac, ':').toUpperCase()
|
||||
if (idx === -1) {
|
||||
data[`devices[${foundDevices.length}]`] = device
|
||||
} else {
|
||||
data[`devices[${idx}]`] = device
|
||||
}
|
||||
console.log("131", data)
|
||||
this.setData(data)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
// 连接低功耗蓝牙设备
|
||||
createBLEConnection(e) {
|
||||
this._connLoading = true
|
||||
wx.showLoading({
|
||||
title: '连接中',
|
||||
})
|
||||
setTimeout(() => {
|
||||
if (this._connLoading) {
|
||||
this._connLoading = false
|
||||
wx.hideLoading()
|
||||
}
|
||||
}, 6000)
|
||||
const ds = e.currentTarget.dataset
|
||||
const index = ds.index
|
||||
this._device = this.data.devices[index]
|
||||
const deviceId = ds.deviceId
|
||||
const name = ds.name
|
||||
this.mac = ds.mac
|
||||
wx.createBLEConnection({
|
||||
deviceId,
|
||||
success: (res) => {
|
||||
this.setData({
|
||||
connected: true,
|
||||
name,
|
||||
deviceId,
|
||||
})
|
||||
console.log("createBLEConnection:success")
|
||||
this.onBLEConnectionStateChange()
|
||||
this.getBLEDeviceServices(deviceId)
|
||||
},
|
||||
fail: res => {
|
||||
this._connLoading = false
|
||||
wx.showToast({
|
||||
title: '连接失败',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
//监听蓝牙连接状态
|
||||
onBLEConnectionStateChange() {
|
||||
wx.onBLEConnectionStateChange((res) => {
|
||||
if (!res.connected) {
|
||||
setTimeout(() => {
|
||||
wx.showToast({
|
||||
title: '连接已断开',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
}, 500)
|
||||
this.setData({
|
||||
connected: false,
|
||||
isType: false,
|
||||
devices: [],
|
||||
weight: "",
|
||||
text: "",
|
||||
imp: ""
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
// 找到新设备的事件
|
||||
onBluetoothDeviceFound() {
|
||||
wx.onBluetoothDeviceFound((res) => {
|
||||
res.devices.forEach(device => {
|
||||
if (device.name.indexOf('AiLink_') != -1) {
|
||||
wx.stopBluetoothDevicesDiscovery() //搜索到名称为“AiLink_”的蓝牙后,停止搜寻附近的蓝牙
|
||||
const foundDevices = this.data.devices
|
||||
const idx = inArray(foundDevices, 'deviceId', device.deviceId)
|
||||
const data = {}
|
||||
let buff = device.advertisData.slice(-6)
|
||||
device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址,这是由于iOS不直接返回mac地址
|
||||
let tempMac = Array.from(device.mac)
|
||||
tempMac.reverse()
|
||||
device.macAddr = ab2hex(tempMac, ':').toUpperCase()
|
||||
if (idx === -1) {
|
||||
data[`devices[${foundDevices.length}]`] = device
|
||||
} else {
|
||||
data[`devices[${idx}]`] = device
|
||||
}
|
||||
console.log("131", data)
|
||||
this.setData(data)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
// 连接低功耗蓝牙设备
|
||||
createBLEConnection(e) {
|
||||
this._connLoading = true
|
||||
wx.showLoading({
|
||||
title: '连接中',
|
||||
})
|
||||
setTimeout(() => {
|
||||
if (this._connLoading) {
|
||||
this._connLoading = false
|
||||
wx.hideLoading()
|
||||
}
|
||||
}, 6000)
|
||||
const ds = e.currentTarget.dataset
|
||||
const index = ds.index
|
||||
this._device = this.data.devices[index]
|
||||
const deviceId = ds.deviceId
|
||||
const name = ds.name
|
||||
this.mac = ds.mac
|
||||
wx.createBLEConnection({
|
||||
deviceId,
|
||||
success: (res) => {
|
||||
this.setData({
|
||||
connected: true,
|
||||
name,
|
||||
deviceId,
|
||||
})
|
||||
console.log("createBLEConnection:success")
|
||||
this.onBLEConnectionStateChange()
|
||||
this.getBLEDeviceServices(deviceId)
|
||||
},
|
||||
fail: res => {
|
||||
this._connLoading = false
|
||||
wx.showToast({
|
||||
title: '连接失败',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
//监听蓝牙连接状态
|
||||
onBLEConnectionStateChange() {
|
||||
wx.onBLEConnectionStateChange((res) => {
|
||||
if (!res.connected) {
|
||||
setTimeout(() => {
|
||||
wx.showToast({
|
||||
title: '连接已断开',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
}, 500)
|
||||
this.setData({
|
||||
connected: false,
|
||||
isType: false,
|
||||
devices: [],
|
||||
weight: "",
|
||||
height: "",
|
||||
text: "",
|
||||
imp: ""
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 获取蓝牙设备的 serviceId
|
||||
getBLEDeviceServices(deviceId) {
|
||||
wx.getBLEDeviceServices({
|
||||
deviceId,
|
||||
success: (res) => {
|
||||
for (let i = 0; i < res.services.length; i++) {
|
||||
if (res.services[i].isPrimary && res.services[i].uuid.indexOf('FFE0') > -1) {
|
||||
this.data.serviceId = res.services[i].uuid
|
||||
this.data.deviceId = deviceId
|
||||
this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
|
||||
wx.showToast({
|
||||
title: '获取设备的UUID成功',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
// 获取蓝牙设备的 serviceId
|
||||
getBLEDeviceServices(deviceId) {
|
||||
wx.getBLEDeviceServices({
|
||||
deviceId,
|
||||
success: (res) => {
|
||||
for (let i = 0; i < res.services.length; i++) {
|
||||
if (res.services[i].isPrimary && res.services[i].uuid.indexOf('FFE0') > -1) {
|
||||
this.data.serviceId = res.services[i].uuid
|
||||
this.data.deviceId = deviceId
|
||||
this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
|
||||
wx.showToast({
|
||||
title: '获取设备的UUID成功',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 获取蓝牙设备某个服务中所有特征值(characteristic)
|
||||
/**
|
||||
* read: true读,write: true写,notify: true广播
|
||||
*/
|
||||
getBLEDeviceCharacteristics(deviceId, serviceId) {
|
||||
this._deviceId = deviceId
|
||||
this._serviceId = serviceId
|
||||
this._device.serviceId = serviceId
|
||||
let that = this
|
||||
wx.getBLEDeviceCharacteristics({
|
||||
deviceId,
|
||||
serviceId,
|
||||
success: (res) => {
|
||||
console.log('getBLEDeviceCharacteristics success', res.characteristics)
|
||||
for (let i = 0; i < res.characteristics.length; i++) {
|
||||
let item = res.characteristics[i];
|
||||
if (item.uuid.indexOf('FFE1') != -1) {
|
||||
that.data.uuid1 = item.uuid //主服务 UUID
|
||||
} else if (item.uuid.indexOf('FFE2') != -1) {
|
||||
that.data.uuid2 = item.uuid //写入设置
|
||||
} else if (item.uuid.indexOf('FFE3') != -1) {
|
||||
that.data.uuid3 = item.uuid //监听数据
|
||||
}
|
||||
}
|
||||
that.setData({
|
||||
isType: true,
|
||||
})
|
||||
wx.notifyBLECharacteristicValueChange({
|
||||
deviceId,
|
||||
serviceId,
|
||||
characteristicId: that.data.uuid2,
|
||||
state: true,
|
||||
})
|
||||
wx.notifyBLECharacteristicValueChange({
|
||||
deviceId,
|
||||
serviceId,
|
||||
characteristicId: that.data.uuid3,
|
||||
state: true,
|
||||
})
|
||||
wx.onBLECharacteristicValueChange((res) => {
|
||||
let value = ab2hex(res.value, "");
|
||||
console.log("16进制转化:", value);
|
||||
let num = value.substring(18, 19)
|
||||
let dw = value.substring(19, 20)
|
||||
let type = value.substring(8, 10)
|
||||
let typeInfo = value.substring(10, 12)
|
||||
console.log("16进制转化:", value);
|
||||
if (type == '10') {
|
||||
let data = parseInt(value.substring(13, 18), 16)
|
||||
let dw1 = "kg"
|
||||
if (dw == "1") {
|
||||
dw1 = "斤"
|
||||
console.log("体重单位:斤")
|
||||
}
|
||||
if (dw == "4") {
|
||||
dw1 = "st:lb"
|
||||
console.log("体重单位:st:lb,计算方法:1 * data + 5")
|
||||
}
|
||||
if (dw == "6") {
|
||||
dw1 = "lb"
|
||||
console.log("体重单位:lb")
|
||||
}
|
||||
if (num == "1") {
|
||||
data = parseInt(value.substring(13, 18), 16) / 10
|
||||
console.log("体重小数点后1位")
|
||||
}
|
||||
if (num == "2") {
|
||||
data = parseInt(value.substring(13, 18), 16) / 100
|
||||
console.log("体重小数点后2位")
|
||||
}
|
||||
if (num == "3") {
|
||||
data = parseInt(value.substring(13, 18), 16) / 1000
|
||||
console.log("体重小数点后3位")
|
||||
}
|
||||
if (typeInfo == "01") {
|
||||
console.log("实时体重:", data)
|
||||
that.setData({
|
||||
weight: "实时体重是:" + data + dw1
|
||||
})
|
||||
}
|
||||
if (typeInfo == "02") {
|
||||
console.log("稳定体重:", data)
|
||||
that.setData({
|
||||
weight: "稳定体重是:" + data + dw1
|
||||
})
|
||||
}
|
||||
}
|
||||
if (type == "14") { //身高模式
|
||||
let height = parseInt(value.substring(10, 14), 16)
|
||||
that.setData({
|
||||
height: "您的身高是:" + height
|
||||
})
|
||||
console.log("身高模式:", height)
|
||||
}
|
||||
if (type == "11") {
|
||||
console.log("阻抗值:", value)
|
||||
if (typeInfo == "03" || typeInfo == "04") {
|
||||
let imp = parseInt(value.substring(17, 22), 16)
|
||||
that.setData({
|
||||
imp: "阻抗值:" + imp
|
||||
})
|
||||
}
|
||||
}
|
||||
if (value.toUpperCase() == "A90026023000589A") {
|
||||
that.setData({
|
||||
text: "测量完成"
|
||||
})
|
||||
}
|
||||
});
|
||||
},
|
||||
fail(res) {
|
||||
console.error('getBLEDeviceCharacteristics', res)
|
||||
}
|
||||
})
|
||||
},
|
||||
handleHeight(e) {
|
||||
let that = this
|
||||
let type = e.currentTarget.dataset.name
|
||||
if (type == '1') {
|
||||
let j = Number(26 + 3 + 6 + 1).toString(16)
|
||||
let str = "A9002603060100" + j.substr(j.length - 2, 2) + "9A"
|
||||
let buf = new Uint8Array(str.match(/[\da-f]{2}/gi).map(function(h) {
|
||||
return parseInt(h, 16)
|
||||
}))
|
||||
that.sendData(buf.buffer)
|
||||
}
|
||||
if (type == '3') {
|
||||
that.setData({
|
||||
pwdModal: true,
|
||||
})
|
||||
}
|
||||
},
|
||||
confirmPwd() {
|
||||
let that = this
|
||||
if (!that.data.pwdInput) {
|
||||
wx.showToast({
|
||||
title: '请输入重量',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
} else {
|
||||
let w = Number(that.data.pwdInput.replace(/[&\|\\\*^%$#@\.-]/g, ""))
|
||||
let j = Number(26 + 7 + 40 + w + 3 + 2).toString(16) //校验和
|
||||
let atr = w.toString(16) //16进制后的标定数据
|
||||
let num = 0 //小数位数
|
||||
let str = null
|
||||
if (that.data.pwdInput.indexOf('.') != -1) {
|
||||
num = Number(that.data.pwdInput.toString().split(".")[1].length)
|
||||
}
|
||||
// 根据atr的位数判断下发的体重填充几个0
|
||||
let n = []
|
||||
for (var i = 1; i <= (6 - atr.length); i++) {
|
||||
n.push(0);
|
||||
}
|
||||
str = "A90026074003" + n.join("") + atr + "0" + num + "00" + j.substr(j.length - 2, 2) + "9A"
|
||||
let buf = new Uint8Array(str.match(/[\da-f]{2}/gi).map(function(h) {
|
||||
return parseInt(h, 16)
|
||||
}))
|
||||
that.sendData(buf.buffer)
|
||||
}
|
||||
},
|
||||
sendData(buffer) {
|
||||
let that = this
|
||||
wx.writeBLECharacteristicValue({
|
||||
deviceId: that.data.deviceId,
|
||||
serviceId: that.data.serviceId,
|
||||
characteristicId: that.data.uuid1,
|
||||
value: buffer,
|
||||
success: res => {
|
||||
console.log('下发指令', res.errMsg)
|
||||
wx.showToast({
|
||||
title: '下发指令成功',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
this.setData({
|
||||
pwdModal: false,
|
||||
})
|
||||
},
|
||||
fail: res => {
|
||||
wx.showToast({
|
||||
title: '下发指令失败',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
this.setData({
|
||||
pwdModal: false,
|
||||
})
|
||||
console.log("失败", res);
|
||||
},
|
||||
})
|
||||
},
|
||||
closeModal() {
|
||||
this.setData({
|
||||
pwdModal: false,
|
||||
})
|
||||
},
|
||||
/**
|
||||
* 断开蓝牙模块
|
||||
*/
|
||||
closeBluetoothAdapter() {
|
||||
wx.closeBluetoothAdapter()
|
||||
this._discoveryStarted = false
|
||||
wx.showToast({
|
||||
title: '断开蓝牙模块',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
this.setData({
|
||||
devices: [],
|
||||
weight: "",
|
||||
text: "",
|
||||
imp: "",
|
||||
isType: false
|
||||
})
|
||||
},
|
||||
// 断开与低功耗蓝牙设备的连接
|
||||
closeBLEConnection() {
|
||||
wx.closeBLEConnection({
|
||||
deviceId: this._deviceId
|
||||
})
|
||||
wx.showToast({
|
||||
title: '断开蓝牙连接',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
this.setData({
|
||||
connected: false,
|
||||
isType: false,
|
||||
devices: [],
|
||||
text: "",
|
||||
weight: "",
|
||||
imp: ""
|
||||
})
|
||||
},
|
||||
// 获取蓝牙设备某个服务中所有特征值(characteristic)
|
||||
/**
|
||||
* read: true读,write: true写,notify: true广播
|
||||
*/
|
||||
getBLEDeviceCharacteristics(deviceId, serviceId) {
|
||||
this._deviceId = deviceId
|
||||
this._serviceId = serviceId
|
||||
this._device.serviceId = serviceId
|
||||
let that = this
|
||||
wx.getBLEDeviceCharacteristics({
|
||||
deviceId,
|
||||
serviceId,
|
||||
success: (res) => {
|
||||
console.log('getBLEDeviceCharacteristics success', res.characteristics)
|
||||
for (let i = 0; i < res.characteristics.length; i++) {
|
||||
let item = res.characteristics[i];
|
||||
if (item.uuid.indexOf('FFE1') != -1) {
|
||||
that.data.uuid1 = item.uuid //主服务 UUID
|
||||
} else if (item.uuid.indexOf('FFE2') != -1) {
|
||||
that.data.uuid2 = item.uuid //写入设置
|
||||
} else if (item.uuid.indexOf('FFE3') != -1) {
|
||||
that.data.uuid3 = item.uuid //监听数据
|
||||
}
|
||||
}
|
||||
that.setData({
|
||||
isType: true,
|
||||
})
|
||||
wx.notifyBLECharacteristicValueChange({
|
||||
deviceId,
|
||||
serviceId,
|
||||
characteristicId: that.data.uuid2,
|
||||
state: true,
|
||||
})
|
||||
wx.notifyBLECharacteristicValueChange({
|
||||
deviceId,
|
||||
serviceId,
|
||||
characteristicId: that.data.uuid3,
|
||||
state: true,
|
||||
})
|
||||
wx.onBLECharacteristicValueChange((res) => {
|
||||
let value = ab2hex(res.value, "");
|
||||
console.log("16进制转化:", value);
|
||||
let num = value.substring(18, 19)
|
||||
let dw = value.substring(19, 20)
|
||||
let type = value.substring(8, 10)
|
||||
let typeInfo = value.substring(10, 12)
|
||||
console.log("16进制转化:", value);
|
||||
if (type == '10') {
|
||||
let data = parseInt(value.substring(13, 18), 16)
|
||||
let dw1 = "kg"
|
||||
if (dw == "1") {
|
||||
dw1 = "斤"
|
||||
console.log("体重单位:斤")
|
||||
}
|
||||
if (dw == "4") {
|
||||
dw1 = "st:lb"
|
||||
console.log("体重单位:st:lb,计算方法:1 * data + 5")
|
||||
}
|
||||
if (dw == "6") {
|
||||
dw1 = "lb"
|
||||
console.log("体重单位:lb")
|
||||
}
|
||||
if (num == "1") {
|
||||
data = parseInt(value.substring(13, 18), 16) / 10
|
||||
console.log("体重小数点后1位")
|
||||
}
|
||||
if (num == "2") {
|
||||
data = parseInt(value.substring(13, 18), 16) / 100
|
||||
console.log("体重小数点后2位")
|
||||
}
|
||||
if (num == "3") {
|
||||
data = parseInt(value.substring(13, 18), 16) / 1000
|
||||
console.log("体重小数点后3位")
|
||||
}
|
||||
if (typeInfo == "01") {
|
||||
console.log("实时体重:", data)
|
||||
that.setData({
|
||||
weight: "实时体重是:" + data + dw1
|
||||
})
|
||||
}
|
||||
if (typeInfo == "02") {
|
||||
console.log("稳定体重:", data)
|
||||
that.setData({
|
||||
weight: "稳定体重是:" + data + dw1
|
||||
})
|
||||
}
|
||||
}
|
||||
if (type == "14") { //身高模式
|
||||
let height = parseInt(value.substring(10, 14), 16)
|
||||
that.setData({
|
||||
height: "您的身高是:" + height
|
||||
})
|
||||
console.log("身高模式:", height)
|
||||
}
|
||||
if (type == "11") {
|
||||
console.log("阻抗值:", value)
|
||||
if (typeInfo == "03" || typeInfo == "04") {
|
||||
let imp = parseInt(value.substring(17, 22), 16)
|
||||
that.setData({
|
||||
imp: "阻抗值:" + imp
|
||||
})
|
||||
}
|
||||
}
|
||||
if (value.toUpperCase() == "A90026023000589A") {
|
||||
that.setData({
|
||||
text: "测量完成"
|
||||
})
|
||||
}
|
||||
});
|
||||
},
|
||||
fail(res) {
|
||||
console.error('getBLEDeviceCharacteristics', res)
|
||||
}
|
||||
})
|
||||
},
|
||||
handleHeight(e) {
|
||||
let that = this
|
||||
let type = e.currentTarget.dataset.name
|
||||
if (type == '1') {
|
||||
let j = Number(26 + 3 + 6 + 1).toString(16)
|
||||
let str = "A9002603060100" + j.substr(j.length - 2, 2) + "9A"
|
||||
let buf = new Uint8Array(str.match(/[\da-f]{2}/gi).map(function(h) {
|
||||
return parseInt(h, 16)
|
||||
}))
|
||||
that.sendData(buf.buffer)
|
||||
}
|
||||
if (type == '2') {
|
||||
let j = Number(26 + 3 + 6 + 2).toString(16)
|
||||
let str = "A9002603060200" + j.substr(j.length - 2, 2) + "9A"
|
||||
let buf = new Uint8Array(str.match(/[\da-f]{2}/gi).map(function(h) {
|
||||
return parseInt(h, 16)
|
||||
}))
|
||||
that.sendData(buf.buffer)
|
||||
}
|
||||
if (type == '3') {
|
||||
that.setData({
|
||||
pwdModal: true,
|
||||
})
|
||||
}
|
||||
},
|
||||
confirmPwd() {
|
||||
let that = this
|
||||
if (!that.data.pwdInput) {
|
||||
wx.showToast({
|
||||
title: '请输入重量',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
} else {
|
||||
let w = Number(that.data.pwdInput.replace(/[&\|\\\*^%$#@\.-]/g, ""))
|
||||
let j = Number(26 + 7 + 40 + w + 3 + 2).toString(16) //校验和
|
||||
let atr = w.toString(16) //16进制后的标定数据
|
||||
let num = 0 //小数位数
|
||||
let str = null
|
||||
if (that.data.pwdInput.indexOf('.') != -1) {
|
||||
num = Number(that.data.pwdInput.toString().split(".")[1].length)
|
||||
}
|
||||
// 根据atr的位数判断下发的体重填充几个0
|
||||
let n = []
|
||||
for (var i = 1; i <= (6 - atr.length); i++) {
|
||||
n.push(0);
|
||||
}
|
||||
str = "A90026074003" + n.join("") + atr + "0" + num + "00" + j.substr(j.length - 2, 2) + "9A"
|
||||
let buf = new Uint8Array(str.match(/[\da-f]{2}/gi).map(function(h) {
|
||||
return parseInt(h, 16)
|
||||
}))
|
||||
that.sendData(buf.buffer)
|
||||
}
|
||||
},
|
||||
sendData(buffer) {
|
||||
let that = this
|
||||
wx.writeBLECharacteristicValue({
|
||||
deviceId: that.data.deviceId,
|
||||
serviceId: that.data.serviceId,
|
||||
characteristicId: that.data.uuid1,
|
||||
value: buffer,
|
||||
success: res => {
|
||||
console.log('下发指令', res.errMsg)
|
||||
wx.showToast({
|
||||
title: '下发指令成功',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
this.setData({
|
||||
pwdModal: false,
|
||||
})
|
||||
},
|
||||
fail: res => {
|
||||
wx.showToast({
|
||||
title: '下发指令失败',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
this.setData({
|
||||
pwdModal: false,
|
||||
})
|
||||
console.log("失败", res);
|
||||
},
|
||||
})
|
||||
},
|
||||
closeModal() {
|
||||
this.setData({
|
||||
pwdModal: false,
|
||||
})
|
||||
},
|
||||
/**
|
||||
* 断开蓝牙模块
|
||||
*/
|
||||
closeBluetoothAdapter() {
|
||||
wx.closeBluetoothAdapter()
|
||||
this._discoveryStarted = false
|
||||
wx.showToast({
|
||||
title: '断开蓝牙模块',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
this.setData({
|
||||
devices: [],
|
||||
weight: "",
|
||||
height: "",
|
||||
text: "",
|
||||
imp: "",
|
||||
isType: false
|
||||
})
|
||||
},
|
||||
// 断开与低功耗蓝牙设备的连接
|
||||
closeBLEConnection() {
|
||||
wx.closeBLEConnection({
|
||||
deviceId: this._deviceId
|
||||
})
|
||||
wx.showToast({
|
||||
title: '断开蓝牙连接',
|
||||
duration: 2000,
|
||||
icon: 'none'
|
||||
})
|
||||
this.setData({
|
||||
connected: false,
|
||||
isType: false,
|
||||
devices: [],
|
||||
text: "",
|
||||
weight: "",
|
||||
height: "",
|
||||
imp: ""
|
||||
})
|
||||
},
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,51 +1,52 @@
|
|||
<wxs module="utils">
|
||||
module.exports.max = function(n1, n2) {
|
||||
return Math.max(n1, n2)
|
||||
}
|
||||
module.exports.len = function(arr) {
|
||||
arr = arr || []
|
||||
return arr.length
|
||||
}
|
||||
module.exports.max = function(n1, n2) {
|
||||
return Math.max(n1, n2)
|
||||
}
|
||||
module.exports.len = function(arr) {
|
||||
arr = arr || []
|
||||
return arr.length
|
||||
}
|
||||
</wxs>
|
||||
|
||||
<view class="container">
|
||||
<view class="header">
|
||||
<button bindtap="openBluetoothAdapter">开始扫描</button>
|
||||
<button bindtap="stopBluetoothDevicesDiscovery">停止扫描</button>
|
||||
<button bindtap="closeBluetoothAdapter">结束流程</button>
|
||||
<button bindtap="handleHeight" wx:if="{{isType}}" data-name="1">身高体脂模式(默认)</button>
|
||||
<button bindtap="handleHeight" wx:if="{{isType}}" data-name="3">标定模式</button>
|
||||
</view>
|
||||
<view class="weight">
|
||||
<view>{{weight}}</view>
|
||||
<view>{{imp}}</view>
|
||||
<view>{{height}}</view>
|
||||
<view>{{text}}</view>
|
||||
</view>
|
||||
<view class="devices_summary">已发现 {{devices.length}} 个外围设备:</view>
|
||||
<scroll-view class="device_list" scroll-y scroll-with-animation>
|
||||
<view wx:for="{{devices}}" wx:key="index" data-device-id="{{item.deviceId}}"
|
||||
data-name="{{item.name || item.localName}}" data-mac="{{item.mac}}" data-index="{{index}}"
|
||||
bindtap="createBLEConnection" class="device_item" hover-class="device_item_hover">
|
||||
<view style="font-size: 32rpx;">
|
||||
<text style="color:#000;font-weight:bold">{{item.name}}</text>
|
||||
<text style="font-size:26rpx">(信号强度: {{item.RSSI}}dBm)</text>
|
||||
</view>
|
||||
<view style="font-size: 26rpx">mac地址: {{item.macAddr || item.deviceId}}</view>
|
||||
<!-- <view style="font-size: 26rpx">广播数据:{{item.analyzeDataText}}</view> -->
|
||||
</view>
|
||||
</scroll-view>
|
||||
<view class="header">
|
||||
<button bindtap="openBluetoothAdapter">开始扫描</button>
|
||||
<button bindtap="stopBluetoothDevicesDiscovery">停止扫描</button>
|
||||
<button bindtap="closeBluetoothAdapter">结束流程</button>
|
||||
<button bindtap="handleHeight" wx:if="{{isType}}" data-name="1">身高体脂模式(默认)</button>
|
||||
<button bindtap="handleHeight" wx:if="{{isType}}" data-name="2">身高体重模式</button>
|
||||
<button bindtap="handleHeight" wx:if="{{isType}}" data-name="3">标定模式</button>
|
||||
</view>
|
||||
<view class="weight">
|
||||
<view>{{weight}}</view>
|
||||
<view>{{imp}}</view>
|
||||
<view>{{height}}</view>
|
||||
<view>{{text}}</view>
|
||||
</view>
|
||||
<view class="devices_summary">已发现 {{devices.length}} 个外围设备:</view>
|
||||
<scroll-view class="device_list" scroll-y scroll-with-animation>
|
||||
<view wx:for="{{devices}}" wx:key="index" data-device-id="{{item.deviceId}}"
|
||||
data-name="{{item.name || item.localName}}" data-mac="{{item.mac}}" data-index="{{index}}"
|
||||
bindtap="createBLEConnection" class="device_item" hover-class="device_item_hover">
|
||||
<view style="font-size: 32rpx;">
|
||||
<text style="color:#000;font-weight:bold">{{item.name}}</text>
|
||||
<text style="font-size:26rpx">(信号强度: {{item.RSSI}}dBm)</text>
|
||||
</view>
|
||||
<view style="font-size: 26rpx">mac地址: {{item.macAddr || item.deviceId}}</view>
|
||||
<!-- <view style="font-size: 26rpx">广播数据:{{item.analyzeDataText}}</view> -->
|
||||
</view>
|
||||
</scroll-view>
|
||||
</view>
|
||||
<view wx:if="{{pwdModal}}" class="modal_wrapper {{pwdModal ? 'modal_wrapper_show': ''}}" catchtouchmove="stopAction">
|
||||
<view class="remindModal">
|
||||
<view class="remindModal_title">标定重量</view>
|
||||
<view class="modal_input_wrap">
|
||||
<input model:value='{{pwdInput}}' class="modal_input" placeholder="请输入"
|
||||
placeholder-style="font-size:30rpx;color:#b4b4b4" />
|
||||
</view>
|
||||
<view class="remindModal_btn">
|
||||
<view class="remindModal_cancel_btn" bindtap="closeModal" hover-class="hover_tap">取消</view>
|
||||
<view class="confirm_btn" bindtap="confirmPwd">确定</view>
|
||||
</view>
|
||||
</view>
|
||||
<view class="remindModal">
|
||||
<view class="remindModal_title">标定重量</view>
|
||||
<view class="modal_input_wrap">
|
||||
<input model:value='{{pwdInput}}' class="modal_input" placeholder="请输入"
|
||||
placeholder-style="font-size:30rpx;color:#b4b4b4" />
|
||||
</view>
|
||||
<view class="remindModal_btn">
|
||||
<view class="remindModal_cancel_btn" bindtap="closeModal" hover-class="hover_tap">取消</view>
|
||||
<view class="confirm_btn" bindtap="confirmPwd">确定</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
|
|
|||
Loading…
Reference in New Issue