蓝牙设备补全

This commit is contained in:
qiaocl 2022-10-13 17:15:36 +08:00
parent 820b6ce159
commit 0464e36310
14 changed files with 512 additions and 65 deletions

View File

@ -10,7 +10,9 @@
"pages/L08/index",
"pages/G01/index",
"pages/FB03/index",
"pages/PCJ02/index"
"pages/PCJ02/index",
"pages/PCL10/index",
"pages/PCL11/index"
],
"window": {
"navigationBarBackgroundColor": "#0082FE",

View File

@ -11,58 +11,69 @@ input,
textarea,
label,
navigator {
box-sizing: border-box;
box-sizing: border-box;
}
page {
--safe-bottom: env(safe-area-inset-bottom);
--safe-bottom: env(safe-area-inset-bottom);
}
.container {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
width: 100vw;
height: 100vh;
padding-bottom: var(--safe-bottom);
position: relative;
display: flex;
flex-direction: column;
align-items: center;
width: 100vw;
height: 100vh;
padding-bottom: var(--safe-bottom);
}
.header {
width: 100%;
width: 100%;
}
.header button {
font-size: 16px;
line-height: 40px;
width: 100% !important;
border-bottom: 1px solid #dfdfdf;
font-size: 16px;
line-height: 40px;
width: 100% !important;
border-bottom: 1px solid #dfdfdf;
}
.device_item {
padding: 15px;
border-bottom: 1px solid #dfdfdf;
padding: 15px;
border-bottom: 1px solid #dfdfdf;
}
.weight {
width: 100%;
margin: 15px;
padding-bottom: 15px;
text-align: center;
font-size: 18px;
font-weight: 700;
border-bottom: 1px solid #dfdfdf;
width: 100%;
margin: 15px;
padding-bottom: 15px;
text-align: center;
font-size: 18px;
font-weight: 700;
border-bottom: 1px solid #dfdfdf;
}
.list{
width: 100%;
height: auto;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
.list {
width: 100%;
height: auto;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.item{
padding: 15px;
background: #f7f7f7;
border-radius: 10px;
margin-top: 15px;
.item,
.item2 {
width: 100%;
padding: 15px 0;
background: #f7f7f7;
border-radius: 10px;
margin-top: 15px;
text-align: center;
margin: 15px 3% 0;
}
.item {
width: 44%;
margin-right: 3%;
float: left;
}

View File

@ -83,7 +83,7 @@ Page({
let buffer = device.advertisData.slice(3, 9)
device.mac = new Uint8Array(buffer) // 保存广播数据中的mac地址这是由于iOS不直接返回mac地址
let tempMac = Array.from(device.mac)
tempMac.reverse()
// tempMac.reverse()
device.macAddr = ab2hex(tempMac, ':').toUpperCase()
if (idx === -1) {
dataT[`devices[${foundDevices.length}]`] = device

164
pages/PCL10/index.js Normal file
View File

@ -0,0 +1,164 @@
const util = require("../../utils/util");
const {
inArray,
ab2hex
} = util
Page({
data: {
connected: false,
name: '',
weight: "",
imp: "",
devices: [],
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) => {
this.onBluetoothDeviceFound()
},
})
},
// 停止搜寻附近的蓝牙外围设备
stopBluetoothDevicesDiscovery() {
wx.stopBluetoothDevicesDiscovery()
},
// 找到新设备的事件
onBluetoothDeviceFound() {
let that = this
wx.onBluetoothDeviceFound((res) => {
res.devices.forEach(device => {
device.advertisData = device.advertisData ? device.advertisData : ''
if (!device.name && !device.localName) {
let value = ab2hex(device.advertisData, "")
let id = value.substring(12, 16)
if (value.indexOf('c0') !== -1 && id == '0002') {
device.name = "PCL-体脂称"
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()
//
const foundDevices = this.data.devices
const idx = inArray(foundDevices, 'deviceId', device.deviceId)
const dataT = {}
if (idx === -1) {
dataT[`devices[${foundDevices.length}]`] = device
} else {
dataT[`devices[${idx}]`] = device
}
this.setData(dataT)
let msg = parseInt(value.substring(16, 18), 16).toString(2)
let weight = parseInt(value.substring(4, 8), 16)
let type = msg.substring(5, 6) //0实时,1稳定
let num = msg.substring(3, 5) //小数点
let unit = msg.substring(1, 3) //单位
let dw1 = "kg"
if (unit == "10") {
dw1 = "lb"
}
if (num == "00") {
weight = parseInt(value.substring(4, 8), 16) / 10
}
if (num == "10") {
if (unit == "10") {
dw1 = "lb"
weight = parseInt(value.substring(4, 8), 16) / 10
} else {
weight = parseInt(value.substring(4, 8), 16) / 100
}
}
that.setData({
weight: "您的实时体重是:" + weight + dw1
})
if (type == '1') {
that.setData({
weight: "您的稳定体重是:" + weight + dw1
})
that.setData({
imp:"阻抗是:" + parseInt(value.substring(8, 12), 16) / 10
})
}
}
return
}
})
})
},
//监听蓝牙连接状态
onBLEConnectionStateChange() {
wx.onBLEConnectionStateChange((res) => {
if (!res.connected) {
wx.stopBluetoothDevicesDiscovery();
setTimeout(() => {
wx.showToast({
title: '连接已断开',
icon: 'none'
})
}, 500)
this.setData({
connected: false,
devices: [],
weight: "",
imp: ""
})
}
})
},
/**
* 断开蓝牙模块
*/
closeBluetoothAdapter() {
wx.stopBluetoothDevicesDiscovery();
wx.closeBluetoothAdapter()
this._discoveryStarted = false
wx.showToast({
title: '结束流程',
icon: 'none'
})
this.setData({
devices: [],
weight: "",
imp: ""
})
},
});

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

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

40
pages/PCL10/index.wxml Normal file
View File

@ -0,0 +1,40 @@
<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>{{imp}}</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}}"
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>

1
pages/PCL10/index.wxss Normal file
View File

@ -0,0 +1 @@
/* pages/PCD01PRO/index.wxss */

164
pages/PCL11/index.js Normal file
View File

@ -0,0 +1,164 @@
const util = require("../../utils/util");
const {
inArray,
ab2hex
} = util
Page({
data: {
connected: false,
name: '',
weight: "",
imp: "",
devices: [],
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) => {
this.onBluetoothDeviceFound()
},
})
},
// 停止搜寻附近的蓝牙外围设备
stopBluetoothDevicesDiscovery() {
wx.stopBluetoothDevicesDiscovery()
},
// 找到新设备的事件
onBluetoothDeviceFound() {
let that = this
wx.onBluetoothDeviceFound((res) => {
res.devices.forEach(device => {
device.advertisData = device.advertisData ? device.advertisData : ''
if (!device.name && !device.localName) {
let value = ab2hex(device.advertisData, "")
let id = value.substring(12, 16)
if (value.indexOf('c0') !== -1 && id == '0002') {
device.name = "PCL-体脂称"
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()
//
const foundDevices = this.data.devices
const idx = inArray(foundDevices, 'deviceId', device.deviceId)
const dataT = {}
if (idx === -1) {
dataT[`devices[${foundDevices.length}]`] = device
} else {
dataT[`devices[${idx}]`] = device
}
this.setData(dataT)
let msg = parseInt(value.substring(16, 18), 16).toString(2)
let weight = parseInt(value.substring(4, 8), 16)
let type = msg.substring(5, 6) //0实时,1稳定
let num = msg.substring(3, 5) //小数点
let unit = msg.substring(1, 3) //单位
let dw1 = "kg"
if (unit == "10") {
dw1 = "lb"
}
if (num == "00") {
weight = parseInt(value.substring(4, 8), 16) / 10
}
if (num == "10") {
if (unit == "10") {
dw1 = "lb"
weight = parseInt(value.substring(4, 8), 16) / 10
} else {
weight = parseInt(value.substring(4, 8), 16) / 100
}
}
that.setData({
weight: "您的实时体重是:" + weight + dw1
})
if (type == '1') {
that.setData({
weight: "您的稳定体重是:" + weight + dw1
})
that.setData({
imp:"阻抗是:" + parseInt(value.substring(8, 12), 16) / 10
})
}
}
return
}
})
})
},
//监听蓝牙连接状态
onBLEConnectionStateChange() {
wx.onBLEConnectionStateChange((res) => {
if (!res.connected) {
wx.stopBluetoothDevicesDiscovery();
setTimeout(() => {
wx.showToast({
title: '连接已断开',
icon: 'none'
})
}, 500)
this.setData({
connected: false,
devices: [],
weight: "",
imp: ""
})
}
})
},
/**
* 断开蓝牙模块
*/
closeBluetoothAdapter() {
wx.stopBluetoothDevicesDiscovery();
wx.closeBluetoothAdapter()
this._discoveryStarted = false
wx.showToast({
title: '结束流程',
icon: 'none'
})
this.setData({
devices: [],
weight: "",
imp: ""
})
},
});

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

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

40
pages/PCL11/index.wxml Normal file
View File

@ -0,0 +1,40 @@
<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>{{imp}}</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}}"
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>

1
pages/PCL11/index.wxss Normal file
View File

@ -0,0 +1 @@
/* pages/PCD01PRO/index.wxss */

View File

@ -25,18 +25,25 @@ Page({
})
return
}
if (type == 'H01PRO') {
wx.navigateTo({
url: `/pages/H01PRO/index`
})
return
}
if (type == 'PCH0809') {
wx.navigateTo({
url: `/pages/PCH0809/index`
})
return
}
if (type == 'PCF01B') {
wx.navigateTo({
url: `/pages/PCF01B/index`
})
return
}
// if (type == 'PCF01B') {
// wx.navigateTo({
// url: `/pages/PCF01B/index`
// })
// return
// }
if (type == 'PCF01proFRK') {
wx.navigateTo({
url: `/pages/PCF01proFRK/index`
@ -73,20 +80,27 @@ Page({
})
return
}
if (type == 'L08') {
wx.navigateTo({
url: `/pages/L08/index`
})
return
}
if (type == 'H01PRO') {
wx.navigateTo({
url: `/pages/H01PRO/index`
})
return
}
// if (type == 'L08') {
// wx.navigateTo({
// url: `/pages/L08/index`
// })
// return
// }
if (type == 'PCL10') {
wx.navigateTo({
url: `/pages/PCL10/index`
})
return
}
if (type == 'PCL11') {
wx.navigateTo({
url: `/pages/PCL11/index`
})
return
}
},
openBluetoothAdapter() {
wx.openBluetoothAdapter({

View File

@ -10,18 +10,20 @@
<view class="container">
<view class="list">
<view class="item2" bindtap="editclick" data-name="PCH0809">PCH08/09/H09B/HB02/META10/PCF01B</view>
<view class="item2" bindtap="editclick" data-name="PCJ02">PCJ02/PCJ01/L01/L03/L05/EKS9919</view>
<view class="item" bindtap="editclick" data-name="PCD01PRO">PCD01PRO</view>
<view class="item" bindtap="editclick" data-name="H01PRO">H01PRO</view>
<view class="item" bindtap="editclick" data-name="PCH0809">PCH08/09</view>
<view class="item" bindtap="editclick" data-name="PCF01B">PCF01B</view>
<view class="item" bindtap="editclick" data-name="PCF08">PCF08/PCF08B</view>
<view class="item" bindtap="editclick" data-name="L08">L08/L06</view>
<view class="item" bindtap="editclick" data-name="FB03">B03/B02/B07</view>
<view class="item" bindtap="editclick" data-name="PCL10">PCL10/PCL11</view>
<view class="item" bindtap="editclick" data-name="PCF01proFRK">PCF01pro(旧)</view>
<view class="item" bindtap="editclick" data-name="PCF01B">PCF01pro(新)</view>
<view class="item" bindtap="editclick" data-name="PCF08">PCF08</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="PCJ02">PCJ02</view>
<view class="item" bindtap="editclick" data-name="G01">G01</view>
<!-- <view class="item" bindtap="editclick" data-name="PCL11">PCL11</view> -->
<!-- <view class="item" bindtap="editclick" data-name="h018">h018</view> -->
<!-- <view class="item" bindtap="editclick" data-name="PCF01B">PCF01B</view> -->
</view>
</view>

View File

@ -23,15 +23,15 @@
"uploadWithSourceMap": true,
"compileHotReLoad": false,
"lazyloadPlaceholderEnable": false,
"useMultiFrameRuntime": true,
"useApiHook": true,
"useApiHostProcess": true,
"useMultiFrameRuntime": false,
"useApiHook": false,
"useApiHostProcess": false,
"babelSetting": {
"ignore": [],
"disablePlugins": [],
"outputPath": ""
},
"useIsolateContext": true,
"useIsolateContext": false,
"userConfirmedBundleSwitch": false,
"packNpmManually": false,
"packNpmRelationList": [],