新增H01pro

This commit is contained in:
qiaocl 2022-06-17 17:32:51 +08:00
parent 9911418cf9
commit 820b6ce159
7 changed files with 804 additions and 480 deletions

View File

@ -1,30 +1,30 @@
{ {
"pages": [ "pages": [
"pages/index/index", "pages/index/index",
"pages/PCD01PRO/index", "pages/PCD01PRO/index",
"pages/PCH0809/index", "pages/H01PRO/index",
"pages/PCF01B/index", "pages/PCH0809/index",
"pages/PCF01proFRK/index", "pages/PCF01B/index",
"pages/PCF08/index", "pages/PCF01proFRK/index",
"pages/PCJ02/index", "pages/PCF08/index",
"pages/G01/index", "pages/L08/index",
"pages/FB03/index", "pages/G01/index",
"pages/L08/index" "pages/FB03/index",
"pages/PCJ02/index"
], ],
"window": { "window": {
"navigationBarBackgroundColor": "#0082FE", "navigationBarBackgroundColor": "#0082FE",
"navigationBarTextStyle": "white", "navigationBarTextStyle": "white",
"navigationBarTitleText": "蓝牙连接Demo", "navigationBarTitleText": "蓝牙连接Demo",
"backgroundColor": "#eeeeee", "backgroundColor": "#eeeeee",
"backgroundTextStyle": "light" "backgroundTextStyle": "light"
}, },
"plugins": { "plugins": {
"sdkPlugin": { "sdkPlugin": {
"version": "2.1.0", "version": "2.1.0",
"provider": "wx17e93aad47cdae1a" "provider": "wx17e93aad47cdae1a"
} }
}, },
"style": "v2", "style": "v2",
"sitemapLocation": "sitemap.json" "sitemapLocation": "sitemap.json"
} }

275
pages/H01PRO/index.js Normal file
View File

@ -0,0 +1,275 @@
const util = require("../../utils/util");
const {
inArray,
ab2hex
} = util
Page({
data: {
devices: [],
connected: false,
text: '',
name: '',
weight: "",
height: "",
imp: "",
readId: "",
writeId: "",
notifyId: "",
deviceId: null,
},
onLoad: function() {},
// 初始化蓝牙模块
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: 500, //上报设备的间隔
success: (res) => {
console.log('startBluetoothDevicesDiscovery success', res)
this.onBluetoothDeviceFound()
},
})
},
// 停止搜寻附近的蓝牙外围设备
stopBluetoothDevicesDiscovery() {
wx.stopBluetoothDevicesDiscovery()
},
// 找到新设备的事件
onBluetoothDeviceFound() {
wx.onBluetoothDeviceFound((res) => {
res.devices.forEach(device => {
if (!device.name && !device.localName) {
return
}
if (device.name.indexOf('My') != -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)
device.macAddr = ab2hex(tempMac, ':').toUpperCase()
if (idx === -1) {
data[`devices[${foundDevices.length}]`] = device
} else {
data[`devices[${idx}]`] = device
}
this.setData(data)
}
})
})
},
// 连接低功耗蓝牙设备
createBLEConnection(e) {
wx.showLoading({
title: '连接中',
})
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 => {
wx.hideLoading()
wx.showToast({
title: '连接失败',
icon: 'none'
})
}
})
},
//监听蓝牙连接状态
onBLEConnectionStateChange() {
wx.onBLEConnectionStateChange((res) => {
if (!res.connected) {
setTimeout(() => {
wx.showToast({
title: '连接已断开',
icon: 'none'
})
}, 500)
this.setData({
connected: 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].uuid.indexOf('FFE0') > -1) {
wx.showLoading({
title: '获取设备的UUID成功',
})
this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
return
}
}
}
})
},
// 获取蓝牙设备某个服务中所有特征值(characteristic)
/**
* read: true读,write: true写,notify: true广播
*/
getBLEDeviceCharacteristics(deviceId, serviceId) {
let that = this
that._deviceId = deviceId
that._serviceId = serviceId
that._device.serviceId = serviceId
wx.hideLoading()
wx.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success: (res) => {
console.log('getBLEDeviceCharacteristics success', res.characteristics)
that.setData({
text: "请站在秤上,开始测量"
})
for (let i = 0; i < res.characteristics.length; i++) {
let item = res.characteristics[i];
if (item.uuid.indexOf("FFE1") != -1) {
if (item.properties.notify == true) {
that.data.notifyId = item.uuid
}
if (item.properties.read) {
that.data.readId = item.uuid
}
if (item.properties.write == true) {
that.data.writeId = item.uuid
}
}
}
// 打开监听
wx.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId: that.data.notifyId,
state: true,
})
wx.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId: that.data.writeId,
state: true,
})
wx.onBLECharacteristicValueChange((res) => {
let value = ab2hex(res.value, '');
let weight = parseInt(value.substring(4, 8), 16) / 100
let height = parseInt(value.substring(30, 34), 16) / 10
let imp0 = value.substring(28, 30) + value.substring(34, 36)
that.setData({
weight: "您的体重是:" + weight + 'kg'
})
that.setData({
height: "您的身高是:" + height + 'kg'
})
that.setData({
imp: "阻抗:" + parseInt(imp0, 16)
})
that.setData({
text: "测量结束"
})
})
},
fail(res) {
console.error('getBLEDeviceCharacteristics', res)
}
})
},
/**
* 断开蓝牙模块
*/
closeBluetoothAdapter() {
wx.closeBluetoothAdapter()
this._discoveryStarted = false
wx.showToast({
title: '断开蓝牙模块',
icon: 'none'
})
this.setData({
devices: [],
weight: "",
height: "",
imp: ""
})
},
// 断开与低功耗蓝牙设备的连接
closeBLEConnection() {
wx.closeBLEConnection({
deviceId: this._deviceId
})
wx.showToast({
title: '断开蓝牙连接',
icon: 'none'
})
this.setData({
connected: false,
devices: [],
height: "",
weight: "",
imp: ""
})
},
});

4
pages/H01PRO/index.json Normal file
View File

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

37
pages/H01PRO/index.wxml Normal file
View File

@ -0,0 +1,37 @@
<wxs module="utils">
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>
</view>
<view class="weight">
<view>{{weight}}</view>
<view>{{height}}</view>
<view>{{imp}}</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 File

@ -1,392 +1,399 @@
const util = require("../../utils/util"); const util = require("../../utils/util");
const { const {
inArray, inArray,
ab2hex ab2hex
} = util } = util
Page({ Page({
data: { data: {
devices: [], devices: [],
connected: false, connected: false,
cmd: '', cmd: '',
name: '', name: '',
weight: "", weight: "",
text: "", text: "",
imp: "", imp: "",
deviceId: null, deviceId: null,
}, },
onLoad: function() {}, onLoad: function() {},
// 初始化蓝牙模块 // 初始化蓝牙模块
editclick: function(e) { editclick: function(e) {
let type = e.currentTarget.dataset.name let type = e.currentTarget.dataset.name
if (type == 'PCD01PRO') { if (type == 'PCD01PRO') {
wx.navigateTo({ wx.navigateTo({
url: `/pages/PCD01PRO/index` url: `/pages/PCD01PRO/index`
}) })
return return
} }
if (type == 'PCH0809') { if (type == 'PCH0809') {
wx.navigateTo({ wx.navigateTo({
url: `/pages/PCH0809/index` url: `/pages/PCH0809/index`
}) })
return return
} }
if (type == 'PCF01B') { if (type == 'PCF01B') {
wx.navigateTo({ wx.navigateTo({
url: `/pages/PCF01B/index` url: `/pages/PCF01B/index`
}) })
return return
} }
if (type == 'PCF01proFRK') { if (type == 'PCF01proFRK') {
wx.navigateTo({ wx.navigateTo({
url: `/pages/PCF01proFRK/index` url: `/pages/PCF01proFRK/index`
}) })
return return
} }
if (type == 'PCF08') { if (type == 'PCF08') {
wx.navigateTo({ wx.navigateTo({
url: `/pages/PCF08/index` url: `/pages/PCF08/index`
}) })
return return
} }
if (type == 'PCJ02') { if (type == 'PCJ02') {
wx.navigateTo({ wx.navigateTo({
url: `/pages/PCJ02/index` url: `/pages/PCJ02/index`
}) })
return return
} }
if (type == 'FB03') { if (type == 'FB03') {
wx.navigateTo({ wx.navigateTo({
url: `/pages/FB03/index` url: `/pages/FB03/index`
}) })
return return
} }
if (type == 'G01') { if (type == 'G01') {
wx.navigateTo({ wx.navigateTo({
url: `/pages/G01/index` url: `/pages/G01/index`
}) })
return return
} }
if (type == 'h018') { if (type == 'h018') {
wx.navigateTo({ wx.navigateTo({
url: `/pages/h018/index` url: `/pages/h018/index`
}) })
return return
} }
if (type == 'L08') { if (type == 'L08') {
wx.navigateTo({ wx.navigateTo({
url: `/pages/L08/index` url: `/pages/L08/index`
}) })
return return
}
if (type == 'H01PRO') {
wx.navigateTo({
url: `/pages/H01PRO/index`
})
return
}
},
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()
}
})
} }
}
})
},
}, // 开始搜寻附近的蓝牙外围设备
openBluetoothAdapter() { startBluetoothDevicesDiscovery() {
wx.openBluetoothAdapter({ if (this._discoveryStarted) {
success: (res) => { return
console.log('openBluetoothAdapter success', res) }
wx.showToast({ this._discoveryStarted = true
title: '蓝牙连接中', wx.startBluetoothDevicesDiscovery({
icon: "none" allowDuplicatesKey: true, //是否允许重复上报同一设备
}) services: [ //要搜索蓝牙设备主 service 的 uuid 列表
this.startBluetoothDevicesDiscovery() "FFE0",
}, ],
fail: (res) => { success: (res) => {
if (res.errCode === 10001) { console.log('startBluetoothDevicesDiscovery success', res)
wx.showToast({ this.onBluetoothDeviceFound()
title: '请打开蓝牙', },
icon: "none" })
}) },
// 监听本机蓝牙状态变化的事件 // 停止搜寻附近的蓝牙外围设备
wx.onBluetoothAdapterStateChange((res) => { stopBluetoothDevicesDiscovery() {
console.log('onBluetoothAdapterStateChange', res) wx.stopBluetoothDevicesDiscovery()
if (res.available) { },
this.startBluetoothDevicesDiscovery()
}
})
}
}
})
},
// 开始搜寻附近的蓝牙外围设备 // 找到新设备的事件
startBluetoothDevicesDiscovery() { onBluetoothDeviceFound() {
if (this._discoveryStarted) { wx.onBluetoothDeviceFound((res) => {
return res.devices.forEach(device => {
} if (device.name.indexOf('AiLink_') != -1) {
this._discoveryStarted = true wx.stopBluetoothDevicesDiscovery() //搜索到名称为“AiLink_”的蓝牙后停止搜寻附近的蓝牙
wx.startBluetoothDevicesDiscovery({ const foundDevices = this.data.devices
allowDuplicatesKey: true, //是否允许重复上报同一设备 const idx = inArray(foundDevices, 'deviceId', device.deviceId)
services: [ //要搜索蓝牙设备主 service 的 uuid 列表 const data = {}
"FFE0", let buff = device.advertisData.slice(-6)
], device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址这是由于iOS不直接返回mac地址
success: (res) => { let tempMac = Array.from(device.mac)
console.log('startBluetoothDevicesDiscovery success', res) tempMac.reverse()
this.onBluetoothDeviceFound() device.macAddr = ab2hex(tempMac, ':').toUpperCase()
}, if (idx === -1) {
}) data[`devices[${foundDevices.length}]`] = device
}, } else {
// 停止搜寻附近的蓝牙外围设备 data[`devices[${idx}]`] = device
stopBluetoothDevicesDiscovery() { }
wx.stopBluetoothDevicesDiscovery() 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.hideLoading()
wx.showToast({
title: '连接失败',
icon: 'none'
})
}
})
},
//监听蓝牙连接状态
onBLEConnectionStateChange() {
wx.onBLEConnectionStateChange((res) => {
if (!res.connected) {
setTimeout(() => {
wx.showToast({
title: '连接已断开',
icon: 'none'
})
}, 500)
this.setData({
connected: false,
devices: [],
weight: "",
text: "",
imp: ""
})
}
})
},
// 找到新设备的事件 // 获取蓝牙设备的 serviceId
onBluetoothDeviceFound() { getBLEDeviceServices(deviceId) {
wx.onBluetoothDeviceFound((res) => { wx.getBLEDeviceServices({
res.devices.forEach(device => { deviceId,
if (device.name.indexOf('AiLink_') != -1) { success: (res) => {
wx.stopBluetoothDevicesDiscovery() //搜索到名称为“AiLink_”的蓝牙后停止搜寻附近的蓝牙 for (let i = 0; i < res.services.length; i++) {
const foundDevices = this.data.devices if (res.services[i].isPrimary && res.services[i].uuid.indexOf('FFE0') > -1) {
const idx = inArray(foundDevices, 'deviceId', device.deviceId) wx.showLoading({
const data = {} title: '获取设备的UUID成功',
let buff = device.advertisData.slice(-6) })
device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址这是由于iOS不直接返回mac地址 this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
let tempMac = Array.from(device.mac) return
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.hideLoading()
wx.showToast({
title: '连接失败',
icon: 'none'
})
}
})
},
//监听蓝牙连接状态
onBLEConnectionStateChange() {
wx.onBLEConnectionStateChange((res) => {
if (!res.connected) {
setTimeout(() => {
wx.showToast({
title: '连接已断开',
icon: 'none'
})
}, 500)
this.setData({
connected: false,
devices: [],
weight: "",
text: "",
imp: ""
})
}
})
},
// 获取蓝牙设备的 serviceId // 获取蓝牙设备某个服务中所有特征值(characteristic)
getBLEDeviceServices(deviceId) { /**
wx.getBLEDeviceServices({ * read: true读,write: true写,notify: true广播
deviceId, */
success: (res) => { getBLEDeviceCharacteristics(deviceId, serviceId) {
for (let i = 0; i < res.services.length; i++) { this._deviceId = deviceId
if (res.services[i].isPrimary && res.services[i].uuid.indexOf('FFE0') > -1) { this._serviceId = serviceId
wx.showLoading({ this._device.serviceId = serviceId
title: '获取设备的UUID成功', wx.getBLEDeviceCharacteristics({
}) deviceId,
this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid) serviceId,
return success: (res) => {
} console.log('getBLEDeviceCharacteristics success', res.characteristics)
} wx.showLoading({
} title: '获取特征值成功',
}) })
}, //FFE1write: true FFE2notify: trueFFE3read: true, write: true, notify: true,
for (let i = 0; i < res.characteristics.length; i++) {
let characteristic = res.characteristics[i];
if (characteristic.uuid.indexOf("FFE2") != -1) {
if (characteristic.properties.notify == true) {
this.notifyBLECharacteristicValue(deviceId, serviceId, characteristic
.uuid)
console.log("ffe2服务的notifyId获取成功")
}
if (characteristic.properties.read == true) {
that.readId = characteristic.uuid
}
if (characteristic.properties.write == true) {
that.writeId = characteristic.uuid
}
break;
}
}
},
fail(res) {
console.error('getBLEDeviceCharacteristics', res)
}
})
},
//解析蓝牙返回数据
notifyBLECharacteristicValue(deviceId, serviceId, notifyId) {
let that = this
wx.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: deviceId,
serviceId: serviceId,
characteristicId: notifyId,
success(res) {
wx.onBLECharacteristicValueChange(function(res) {
let value = ab2hex(res.value);
let num = value.substring(18, 19)
let dw = value.substring(19, 20)
let typeInfo = value.substring(10, 12)
console.log("16进制转化:", value);
if (value.length == 26) {
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 (value.length == 30) {
console.log("阻抗值:", value)
if (typeInfo == "03") {
let imp = parseInt(value.substring(17, 22), 16)
that.setData({
imp: "阻抗值:" + imp
})
}
// 获取蓝牙设备某个服务中所有特征值(characteristic) }
/** if (value.toUpperCase() == "A90026023000589A") {
* read: true读,write: true写,notify: true广播 console.log("测量完成")
*/ that.setData({
getBLEDeviceCharacteristics(deviceId, serviceId) { text: "测量完成"
this._deviceId = deviceId })
this._serviceId = serviceId }
this._device.serviceId = serviceId });
wx.getBLEDeviceCharacteristics({ },
deviceId, fail(res) {
serviceId, console.log("测量失败", res.value);
success: (res) => { }
console.log('getBLEDeviceCharacteristics success', res.characteristics) });
wx.showLoading({ },
title: '获取特征值成功',
})
//FFE1write: true FFE2notify: trueFFE3read: true, write: true, notify: true,
for (let i = 0; i < res.characteristics.length; i++) {
let characteristic = res.characteristics[i];
if (characteristic.uuid.indexOf("FFE2") != -1) {
if (characteristic.properties.notify == true) {
this.notifyBLECharacteristicValue(deviceId, serviceId, characteristic
.uuid)
console.log("ffe2服务的notifyId获取成功")
}
if (characteristic.properties.read == true) {
that.readId = characteristic.uuid
}
if (characteristic.properties.write == true) {
that.writeId = characteristic.uuid
}
break;
}
}
},
fail(res) {
console.error('getBLEDeviceCharacteristics', res)
}
})
},
//解析蓝牙返回数据
notifyBLECharacteristicValue(deviceId, serviceId, notifyId) {
let that = this
wx.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: deviceId,
serviceId: serviceId,
characteristicId: notifyId,
success(res) {
wx.onBLECharacteristicValueChange(function(res) {
let value = ab2hex(res.value);
let num = value.substring(18, 19)
let dw = value.substring(19, 20)
let typeInfo = value.substring(10, 12)
console.log("16进制转化:", value);
if (value.length == 26) {
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 (value.length == 30) {
console.log("阻抗值:", value)
if (typeInfo == "03") {
let imp = parseInt(value.substring(17, 22), 16)
that.setData({
imp: "阻抗值:" + imp
})
}
} /**
if (value.toUpperCase() == "A90026023000589A") { * 断开蓝牙模块
console.log("测量完成") */
that.setData({ closeBluetoothAdapter() {
text: "测量完成" wx.closeBluetoothAdapter()
}) this._discoveryStarted = false
} wx.showToast({
}); title: '断开蓝牙模块',
}, icon: 'none'
fail(res) { })
console.log("测量失败", res.value); this.setData({
} devices: [],
}); weight: "",
}, text: "",
imp: ""
/** })
* 断开蓝牙模块 },
*/ // 断开与低功耗蓝牙设备的连接
closeBluetoothAdapter() { closeBLEConnection() {
wx.closeBluetoothAdapter() wx.closeBLEConnection({
this._discoveryStarted = false deviceId: this._deviceId
wx.showToast({ })
title: '断开蓝牙模块', wx.showToast({
icon: 'none' title: '断开蓝牙连接',
}) icon: 'none'
this.setData({ })
devices: [], this.setData({
weight: "", connected: false,
text: "", devices: [],
imp: "" text: "",
}) weight: "",
}, imp: ""
// 断开与低功耗蓝牙设备的连接 })
closeBLEConnection() { },
wx.closeBLEConnection({
deviceId: this._deviceId
})
wx.showToast({
title: '断开蓝牙连接',
icon: 'none'
})
this.setData({
connected: false,
devices: [],
text: "",
weight: "",
imp: ""
})
},
}); });

View File

@ -1,25 +1,27 @@
<wxs module="utils"> <wxs module="utils">
module.exports.max = function(n1, n2) { module.exports.max = function(n1, n2) {
return Math.max(n1, n2) return Math.max(n1, n2)
} }
module.exports.len = function(arr) { module.exports.len = function(arr) {
arr = arr || [] arr = arr || []
return arr.length return arr.length
} }
</wxs> </wxs>
<view class="container"> <view class="container">
<view class="list"> <view class="list">
<view class="item" bindtap="editclick" data-name="PCD01PRO" >PCD01PRO</view> <view class="item" bindtap="editclick" data-name="PCD01PRO">PCD01PRO</view>
<view class="item" bindtap="editclick" data-name="PCH0809">PCH08/09</view> <view class="item" bindtap="editclick" data-name="H01PRO">H01PRO</view>
<view class="item" bindtap="editclick" data-name="PCF01B">PCF01B</view> <view class="item" bindtap="editclick" data-name="PCH0809">PCH08/09</view>
<view class="item" bindtap="editclick" data-name="PCF01proFRK">PCF01pro(旧)</view> <view class="item" bindtap="editclick" data-name="PCF01B">PCF01B</view>
<view class="item" bindtap="editclick" data-name="PCF01B">PCF01pro(新)</view> <view class="item" bindtap="editclick" data-name="PCF01proFRK">PCF01pro(旧)</view>
<view class="item" bindtap="editclick" data-name="PCF08">PCF08</view> <view class="item" bindtap="editclick" data-name="PCF01B">PCF01pro(新)</view>
<view class="item" bindtap="editclick" data-name="PCJ02">PCJ02</view> <view class="item" bindtap="editclick" data-name="PCF08">PCF08</view>
<view class="item" bindtap="editclick" data-name="G01">G01</view> <view class="item" bindtap="editclick" data-name="L08">L08</view>
<view class="item" bindtap="editclick" data-name="FB03">B03</view> <view class="item" bindtap="editclick" data-name="FB03">B03</view>
<!-- <view class="item" bindtap="editclick" data-name="h018">h018</view> --> <view class="item" bindtap="editclick" data-name="PCJ02">PCJ02</view>
<view class="item" bindtap="editclick" data-name="L08">L08</view> <view class="item" bindtap="editclick" data-name="G01">G01</view>
</view> <!-- <view class="item" bindtap="editclick" data-name="h018">h018</view> -->
</view>
</view> </view>

View File

@ -1,57 +1,56 @@
{ {
"description": "项目配置文件详见文档https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html", "description": "项目配置文件详见文档https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html",
"packOptions": { "packOptions": {
"ignore": [], "ignore": [],
"include": [] "include": []
},
"setting": {
"urlCheck": true,
"es6": false,
"enhance": true,
"postcss": true,
"preloadBackgroundData": false,
"minified": true,
"newFeature": false,
"coverView": true,
"nodeModules": false,
"autoAudits": false,
"showShadowRootInWxmlPanel": true,
"scopeDataCheck": false,
"uglifyFileName": false,
"checkInvalidKey": true,
"checkSiteMap": true,
"uploadWithSourceMap": true,
"compileHotReLoad": false,
"lazyloadPlaceholderEnable": false,
"useMultiFrameRuntime": true,
"useApiHook": true,
"useApiHostProcess": false,
"babelSetting": {
"ignore": [],
"disablePlugins": [],
"outputPath": ""
}, },
"useIsolateContext": true, "setting": {
"userConfirmedBundleSwitch": false, "urlCheck": true,
"packNpmManually": false, "es6": false,
"packNpmRelationList": [], "enhance": true,
"minifyWXSS": true, "postcss": true,
"showES6CompileOption": false, "preloadBackgroundData": false,
"disableUseStrict": false, "minified": true,
"ignoreUploadUnusedFiles": true, "newFeature": false,
"useStaticServer": true, "coverView": true,
"useCompilerPlugins": false, "nodeModules": false,
"minifyWXML": true "autoAudits": false,
}, "showShadowRootInWxmlPanel": true,
"compileType": "miniprogram", "scopeDataCheck": false,
"libVersion": "2.16.0", "uglifyFileName": false,
"projectname": "bluetooth_demo", "checkInvalidKey": true,
"simulatorType": "wechat", "checkSiteMap": true,
"simulatorPluginLibVersion": {}, "uploadWithSourceMap": true,
"appid": "wxcea3504a31518eb6", "compileHotReLoad": false,
"condition": {}, "lazyloadPlaceholderEnable": false,
"editorSetting": { "useMultiFrameRuntime": true,
"tabIndent": "insertSpaces", "useApiHook": true,
"tabSize": 2 "useApiHostProcess": true,
} "babelSetting": {
"ignore": [],
"disablePlugins": [],
"outputPath": ""
},
"useIsolateContext": true,
"userConfirmedBundleSwitch": false,
"packNpmManually": false,
"packNpmRelationList": [],
"minifyWXSS": true,
"disableUseStrict": false,
"minifyWXML": true,
"showES6CompileOption": false,
"useCompilerPlugins": false,
"ignoreUploadUnusedFiles": true
},
"compileType": "miniprogram",
"libVersion": "2.16.0",
"projectname": "bluetooth_demo",
"simulatorType": "wechat",
"simulatorPluginLibVersion": {},
"appid": "wxcea3504a31518eb6",
"editorSetting": {
"tabIndent": "insertSpaces",
"tabSize": 2
},
"condition": {}
} }