蓝牙连接demo
This commit is contained in:
parent
1703ff1a7c
commit
dfc0ce357a
32
app.json
32
app.json
|
|
@ -1,32 +0,0 @@
|
||||||
{
|
|
||||||
"pages": [
|
|
||||||
"pages/index/index",
|
|
||||||
"pages/PCD01PRO/index",
|
|
||||||
"pages/H01PRO/index",
|
|
||||||
"pages/PCH0809/index",
|
|
||||||
"pages/PCF01pro/index",
|
|
||||||
"pages/PCF01proFRK/index",
|
|
||||||
"pages/PCF08/index",
|
|
||||||
"pages/L08/index",
|
|
||||||
"pages/G01/index",
|
|
||||||
"pages/FB03/index",
|
|
||||||
"pages/PCJ02/index",
|
|
||||||
"pages/PCL10/index",
|
|
||||||
"pages/PCL11/index"
|
|
||||||
],
|
|
||||||
"window": {
|
|
||||||
"navigationBarBackgroundColor": "#0082FE",
|
|
||||||
"navigationBarTextStyle": "white",
|
|
||||||
"navigationBarTitleText": "蓝牙连接Demo",
|
|
||||||
"backgroundColor": "#eeeeee",
|
|
||||||
"backgroundTextStyle": "light"
|
|
||||||
},
|
|
||||||
"plugins": {
|
|
||||||
"sdkPlugin": {
|
|
||||||
"version": "2.1.0",
|
|
||||||
"provider": "wx17e93aad47cdae1a"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"style": "v2",
|
|
||||||
"sitemapLocation": "sitemap.json"
|
|
||||||
}
|
|
||||||
79
app.wxss
79
app.wxss
|
|
@ -1,79 +0,0 @@
|
||||||
/**app.wxss**/
|
|
||||||
view,
|
|
||||||
cover-view,
|
|
||||||
scroll-view,
|
|
||||||
swiper,
|
|
||||||
swiper-item,
|
|
||||||
movable-area,
|
|
||||||
movable-view,
|
|
||||||
button,
|
|
||||||
input,
|
|
||||||
textarea,
|
|
||||||
label,
|
|
||||||
navigator {
|
|
||||||
box-sizing: border-box;
|
|
||||||
}
|
|
||||||
|
|
||||||
page {
|
|
||||||
--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);
|
|
||||||
}
|
|
||||||
|
|
||||||
.header {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.header button {
|
|
||||||
font-size: 16px;
|
|
||||||
line-height: 40px;
|
|
||||||
width: 100% !important;
|
|
||||||
border-bottom: 1px solid #dfdfdf;
|
|
||||||
}
|
|
||||||
|
|
||||||
.device_item {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
.list {
|
|
||||||
width: 100%;
|
|
||||||
height: auto;
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-between;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
}
|
|
||||||
|
|
||||||
.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;
|
|
||||||
}
|
|
||||||
|
|
@ -1,319 +0,0 @@
|
||||||
const util = require("../../utils/util");
|
|
||||||
const {
|
|
||||||
inArray,
|
|
||||||
ab2hex
|
|
||||||
} = util
|
|
||||||
const plugin = requirePlugin("sdkPlugin").AiLink;
|
|
||||||
Page({
|
|
||||||
data: {
|
|
||||||
devices: [],
|
|
||||||
connected: false,
|
|
||||||
name: '',
|
|
||||||
weight: "",
|
|
||||||
text: "",
|
|
||||||
uuid1: "",
|
|
||||||
uuid2: "",
|
|
||||||
uuid3: "",
|
|
||||||
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: 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
|
|
||||||
}
|
|
||||||
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: "",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
// 获取蓝牙设备的 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) {
|
|
||||||
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
|
|
||||||
this._deviceId = deviceId
|
|
||||||
this._serviceId = serviceId
|
|
||||||
this._device.serviceId = serviceId
|
|
||||||
wx.getBLEDeviceCharacteristics({
|
|
||||||
deviceId,
|
|
||||||
serviceId,
|
|
||||||
success: (res) => {
|
|
||||||
console.log("特征值:", res)
|
|
||||||
for (let i = 0; i < res.characteristics.length; i++) {
|
|
||||||
let item = res.characteristics[i];
|
|
||||||
if (item.uuid.indexOf('0000FFE1') != -1) {
|
|
||||||
that.data.uuid1 = item.uuid //主服务 UUID
|
|
||||||
} else if (item.uuid.indexOf('0000FE2') != -1) {
|
|
||||||
that.data.uuid2 = item.uuid //写入设置
|
|
||||||
} else if (item.uuid.indexOf('0000FFE3') != -1) {
|
|
||||||
that.data.uuid3 = item.uuid //监听数据
|
|
||||||
}
|
|
||||||
}
|
|
||||||
wx.notifyBLECharacteristicValueChange({
|
|
||||||
deviceId,
|
|
||||||
serviceId,
|
|
||||||
characteristicId: that.data.uuid2,
|
|
||||||
state: true,
|
|
||||||
})
|
|
||||||
wx.notifyBLECharacteristicValueChange({
|
|
||||||
deviceId,
|
|
||||||
serviceId,
|
|
||||||
characteristicId: that.data.uuid3,
|
|
||||||
state: true,
|
|
||||||
})
|
|
||||||
plugin.initPlugin(res.characteristics, this._device)
|
|
||||||
wx.onBLECharacteristicValueChange((characteristic) => {
|
|
||||||
let bleData = plugin.parseBleData(characteristic.value)
|
|
||||||
let dw1 = "kg"
|
|
||||||
if (bleData.status == 0) {
|
|
||||||
console.log("握手成功")
|
|
||||||
} else if (bleData.status == 1) {
|
|
||||||
let payload = ab2hex(bleData.data, '')
|
|
||||||
let typeInfo = payload.substring(0, 2)
|
|
||||||
let unit = payload.substring(6, 8) // 单位
|
|
||||||
let num = payload.substring(8, 9) //正负数
|
|
||||||
let dot = payload.substring(9, 10) //小数点
|
|
||||||
let data = parseInt(payload.substring(2, 6), 16)
|
|
||||||
if (unit == "01") {
|
|
||||||
dw1 = "斤"
|
|
||||||
}
|
|
||||||
if (unit == "04") {
|
|
||||||
dw1 = "lb"
|
|
||||||
}
|
|
||||||
if (unit == "05") {
|
|
||||||
dw1 = "g"
|
|
||||||
}
|
|
||||||
if (unit == "06") {
|
|
||||||
dw1 = 'lb'
|
|
||||||
}
|
|
||||||
//
|
|
||||||
if (num == "0") {
|
|
||||||
that.setData({
|
|
||||||
text: "重量为正数"
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (num == "1") {
|
|
||||||
that.setData({
|
|
||||||
text: "重量为负数"
|
|
||||||
})
|
|
||||||
}
|
|
||||||
//
|
|
||||||
if (dot == "1") {
|
|
||||||
data = data / 10
|
|
||||||
}
|
|
||||||
if (dot == "2") {
|
|
||||||
data = data / 100
|
|
||||||
}
|
|
||||||
if (dot == "3") {
|
|
||||||
data = data / 1000
|
|
||||||
}
|
|
||||||
if (typeInfo == "01") {
|
|
||||||
that.setData({
|
|
||||||
weight: "稳定体重是" + data + dw1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (typeInfo == "02") {
|
|
||||||
that.setData({
|
|
||||||
weight: "实时体重是:" + data + dw1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
},
|
|
||||||
fail(res) {
|
|
||||||
console.error('getBLEDeviceCharacteristics', res)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
handleIsNum() {
|
|
||||||
let that = this
|
|
||||||
let str = [0x83, 0x01]
|
|
||||||
plugin.sendDataOfA7(str)
|
|
||||||
},
|
|
||||||
// 归零
|
|
||||||
handleIsLing() {
|
|
||||||
let str = [0x83, 0x00]
|
|
||||||
console.log("归零指令", str)
|
|
||||||
plugin.sendDataOfA7(str)
|
|
||||||
},
|
|
||||||
/**
|
|
||||||
* 断开蓝牙模块
|
|
||||||
*/
|
|
||||||
closeBluetoothAdapter() {
|
|
||||||
wx.closeBluetoothAdapter()
|
|
||||||
this._discoveryStarted = false
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙模块',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
devices: [],
|
|
||||||
weight: "",
|
|
||||||
text: "",
|
|
||||||
})
|
|
||||||
},
|
|
||||||
// 断开与低功耗蓝牙设备的连接
|
|
||||||
closeBLEConnection() {
|
|
||||||
wx.closeBLEConnection({
|
|
||||||
deviceId: this._deviceId
|
|
||||||
})
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙连接',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
connected: false,
|
|
||||||
devices: [],
|
|
||||||
text: "",
|
|
||||||
weight: "",
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,42 +0,0 @@
|
||||||
<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="closeBluetoothAdapter">结束流程</button>
|
|
||||||
<button bindtap="handleIsNum">重量锁定</button>
|
|
||||||
<button bindtap="handleIsLing">归零</button>
|
|
||||||
</view>
|
|
||||||
<view class="weight">
|
|
||||||
<view>{{weight}}</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>
|
|
||||||
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
/* pages/PCD01PRO/index.wxss */
|
|
||||||
|
|
@ -1,154 +0,0 @@
|
||||||
const util = require("../../utils/util");
|
|
||||||
const {
|
|
||||||
inArray,
|
|
||||||
ab2hex
|
|
||||||
} = util
|
|
||||||
Page({
|
|
||||||
data: {
|
|
||||||
connected: false,
|
|
||||||
height: "",
|
|
||||||
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: 1000, //上报设备的间隔
|
|
||||||
services: [
|
|
||||||
"FFF0",
|
|
||||||
],
|
|
||||||
success: (res) => {
|
|
||||||
this.onBluetoothDeviceFound()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
},
|
|
||||||
// 停止搜寻附近的蓝牙外围设备
|
|
||||||
stopBluetoothDevicesDiscovery() {
|
|
||||||
wx.stopBluetoothDevicesDiscovery()
|
|
||||||
},
|
|
||||||
|
|
||||||
// 找到新设备的事件
|
|
||||||
onBluetoothDeviceFound() {
|
|
||||||
let that = this
|
|
||||||
wx.onBluetoothDeviceFound((res) => {
|
|
||||||
res.devices.forEach(device => {
|
|
||||||
if (!device.name && !device.localName) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (device.name.indexOf("WSD") !== -1) {
|
|
||||||
let value = ab2hex(device.advertisData, "")
|
|
||||||
let type = value.substring(22, 24)
|
|
||||||
let num = value.substring(28, 29)
|
|
||||||
let dw = value.substring(29, 30)
|
|
||||||
let data = parseInt(value.substring(24, 28), 16)
|
|
||||||
let unit = "cm"
|
|
||||||
const foundDevices = this.data.devices
|
|
||||||
const idx = inArray(foundDevices, 'deviceId', device.deviceId)
|
|
||||||
const dataT = {}
|
|
||||||
let buffer = device.advertisData.slice(3, 9)
|
|
||||||
device.mac = new Uint8Array(buffer) // 保存广播数据中的mac地址,这是由于iOS不直接返回mac地址
|
|
||||||
let tempMac = Array.from(device.mac)
|
|
||||||
// tempMac.reverse()
|
|
||||||
device.macAddr = ab2hex(tempMac, ':').toUpperCase()
|
|
||||||
if (idx === -1) {
|
|
||||||
dataT[`devices[${foundDevices.length}]`] = device
|
|
||||||
} else {
|
|
||||||
dataT[`devices[${idx}]`] = device
|
|
||||||
}
|
|
||||||
this.setData(dataT)
|
|
||||||
if (dw == "1") {
|
|
||||||
unit = "FT"
|
|
||||||
data = data * 2.54
|
|
||||||
}
|
|
||||||
if (num == "1") {
|
|
||||||
data = data / 10
|
|
||||||
}
|
|
||||||
if (num == "2") {
|
|
||||||
data = data / 100
|
|
||||||
}
|
|
||||||
if (num == "3") {
|
|
||||||
data = data / 1000
|
|
||||||
}
|
|
||||||
if (type == "01") {
|
|
||||||
that.setData({
|
|
||||||
height: data + unit
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
})
|
|
||||||
},
|
|
||||||
//监听蓝牙连接状态
|
|
||||||
onBLEConnectionStateChange() {
|
|
||||||
wx.onBLEConnectionStateChange((res) => {
|
|
||||||
if (!res.connected) {
|
|
||||||
wx.stopBluetoothDevicesDiscovery();
|
|
||||||
setTimeout(() => {
|
|
||||||
wx.showToast({
|
|
||||||
title: '连接已断开',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
}, 500)
|
|
||||||
this.setData({
|
|
||||||
connected: false,
|
|
||||||
devices: [],
|
|
||||||
height: "",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 断开蓝牙模块
|
|
||||||
*/
|
|
||||||
closeBluetoothAdapter() {
|
|
||||||
wx.stopBluetoothDevicesDiscovery();
|
|
||||||
wx.closeBluetoothAdapter()
|
|
||||||
this._discoveryStarted = false
|
|
||||||
wx.showToast({
|
|
||||||
title: '结束流程',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
devices: [],
|
|
||||||
height: "",
|
|
||||||
})
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,38 +0,0 @@
|
||||||
<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="closeBluetoothAdapter">结束流程</button>
|
|
||||||
</view>
|
|
||||||
<view class="weight">
|
|
||||||
<view>{{height}}</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 +0,0 @@
|
||||||
/* pages/PCD01PRO/index.wxss */
|
|
||||||
|
|
@ -1,275 +0,0 @@
|
||||||
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: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,37 +0,0 @@
|
||||||
<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>
|
|
||||||
|
|
@ -1,353 +0,0 @@
|
||||||
const util = require("../../utils/util");
|
|
||||||
const {
|
|
||||||
inArray,
|
|
||||||
ab2hex
|
|
||||||
} = util
|
|
||||||
const plugin = requirePlugin("sdkPlugin").AiLink;
|
|
||||||
Page({
|
|
||||||
data: {
|
|
||||||
devices: [],
|
|
||||||
connected: false,
|
|
||||||
name: '',
|
|
||||||
weight: "",
|
|
||||||
text: "",
|
|
||||||
imp2: '',
|
|
||||||
imp3: '',
|
|
||||||
imp4: '',
|
|
||||||
imp5: '',
|
|
||||||
imp7: '',
|
|
||||||
uuid1: null,
|
|
||||||
uuid2: null,
|
|
||||||
uuid3: null,
|
|
||||||
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: 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 && !device.localName) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
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: "",
|
|
||||||
text: "",
|
|
||||||
imp2: '',
|
|
||||||
imp3: '',
|
|
||||||
imp4: '',
|
|
||||||
imp5: '',
|
|
||||||
imp7: '',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
// 获取蓝牙设备的 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) {
|
|
||||||
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)
|
|
||||||
for (let i = 0; i < res.characteristics.length; i++) {
|
|
||||||
let item = res.characteristics[i];
|
|
||||||
if (item.uuid.indexOf('0000FFE1') != -1) {
|
|
||||||
that.data.uuid1 = item.uuid //主服务 UUID
|
|
||||||
} else if (item.uuid.indexOf('0000FFE2') != -1) {
|
|
||||||
that.data.uuid2 = item.uuid //写入设置
|
|
||||||
} else if (item.uuid.indexOf('0000FFE3') != -1) {
|
|
||||||
that.data.uuid3 = item.uuid //监听数据
|
|
||||||
}
|
|
||||||
}
|
|
||||||
wx.notifyBLECharacteristicValueChange({
|
|
||||||
deviceId,
|
|
||||||
serviceId,
|
|
||||||
characteristicId: that.data.uuid2,
|
|
||||||
state: true,
|
|
||||||
})
|
|
||||||
wx.notifyBLECharacteristicValueChange({
|
|
||||||
deviceId,
|
|
||||||
serviceId,
|
|
||||||
characteristicId: that.data.uuid3,
|
|
||||||
state: true,
|
|
||||||
})
|
|
||||||
plugin.initPlugin(res.characteristics, this._device)
|
|
||||||
wx.onBLECharacteristicValueChange((characteristic) => {
|
|
||||||
let bleData = plugin.parseBleData(characteristic.value)
|
|
||||||
if (bleData.status == 0) {
|
|
||||||
console.log("握手成功")
|
|
||||||
} else if (bleData.status == 1) {
|
|
||||||
let payload = ab2hex(bleData.data, '')
|
|
||||||
let dw1 = "kg"
|
|
||||||
let type = payload.substring(0, 2)
|
|
||||||
let typeInfo = payload.substring(2, 4)
|
|
||||||
if (type == "01") { //体脂模式
|
|
||||||
let data = parseInt(payload.substring(4, 10), 16)
|
|
||||||
let num = payload.substring(10, 11)
|
|
||||||
let dw = payload.substring(11, 12)
|
|
||||||
if (dw == "1") {
|
|
||||||
dw1 = "斤"
|
|
||||||
}
|
|
||||||
if (num == "1") {
|
|
||||||
data = data / 10
|
|
||||||
}
|
|
||||||
if (num == "2") {
|
|
||||||
data = data / 100
|
|
||||||
}
|
|
||||||
if (num == "3") {
|
|
||||||
data = data / 1000
|
|
||||||
}
|
|
||||||
if (typeInfo == "01") {
|
|
||||||
that.setData({
|
|
||||||
weight: "实时体重是:" + data + dw1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (typeInfo == "02") {
|
|
||||||
that.setData({
|
|
||||||
weight: "稳定体重是:" + data + dw1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (type == "02") {
|
|
||||||
console.log("阻抗类型:", payload, type, typeInfo, parseInt(payload
|
|
||||||
.substring(6, 14), 16)) //阻抗模式
|
|
||||||
if (typeInfo == "02") {
|
|
||||||
that.setData({
|
|
||||||
imp7: "阻抗测量失败"
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (typeInfo == "03") {
|
|
||||||
let mcu = payload.substring(4, 6)
|
|
||||||
if (mcu == "02") {
|
|
||||||
that.setData({
|
|
||||||
imp2: "左手阻抗:" + parseInt(payload.substring(
|
|
||||||
6, 14), 16)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (mcu == "03") {
|
|
||||||
that.setData({
|
|
||||||
imp3: "右手阻抗:" + parseInt(payload.substring(
|
|
||||||
6, 14), 16)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (mcu == "04") {
|
|
||||||
that.setData({
|
|
||||||
imp4: "左脚阻抗:" + parseInt(payload.substring(
|
|
||||||
6, 14), 16)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (mcu == "05") {
|
|
||||||
that.setData({
|
|
||||||
imp5: "右脚阻抗:" + parseInt(payload.substring(
|
|
||||||
6, 14), 16)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mcu == "07") {
|
|
||||||
that.setData({
|
|
||||||
imp7: "躯干阻抗:" + parseInt(payload.substring(
|
|
||||||
6, 14), 16)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (type == "0f") {
|
|
||||||
that.setData({
|
|
||||||
text: "测量完成"
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
fail(res) {
|
|
||||||
console.error('getBLEDeviceCharacteristics', res)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 断开蓝牙模块
|
|
||||||
*/
|
|
||||||
closeBluetoothAdapter() {
|
|
||||||
wx.closeBluetoothAdapter()
|
|
||||||
this._discoveryStarted = false
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙模块',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
devices: [],
|
|
||||||
weight: "",
|
|
||||||
text: "",
|
|
||||||
imp2: '',
|
|
||||||
imp3: '',
|
|
||||||
imp4: '',
|
|
||||||
imp5: '',
|
|
||||||
imp7: '',
|
|
||||||
})
|
|
||||||
},
|
|
||||||
// 断开与低功耗蓝牙设备的连接
|
|
||||||
closeBLEConnection() {
|
|
||||||
wx.closeBLEConnection({
|
|
||||||
deviceId: this._deviceId
|
|
||||||
})
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙连接',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
connected: false,
|
|
||||||
devices: [],
|
|
||||||
weight: "",
|
|
||||||
text: "",
|
|
||||||
imp2: '',
|
|
||||||
imp3: '',
|
|
||||||
imp4: '',
|
|
||||||
imp5: '',
|
|
||||||
imp7: '',
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,46 +0,0 @@
|
||||||
<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>{{text}}</view>
|
|
||||||
<view>{{imp2}}</view>
|
|
||||||
<view>{{imp3}}</view>
|
|
||||||
<view>{{imp4}}</view>
|
|
||||||
<view>{{imp5}}</view>
|
|
||||||
<view>{{imp7}}</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>
|
|
||||||
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
/* pages/PCD01PRO/index.wxss */
|
|
||||||
|
|
@ -1,329 +0,0 @@
|
||||||
const util = require("../../utils/util");
|
|
||||||
const {
|
|
||||||
inArray,
|
|
||||||
ab2hex
|
|
||||||
} = util
|
|
||||||
|
|
||||||
Page({
|
|
||||||
data: {
|
|
||||||
devices: [],
|
|
||||||
connected: false,
|
|
||||||
cmd: '',
|
|
||||||
name: '',
|
|
||||||
weight: "",
|
|
||||||
text: "",
|
|
||||||
imp: "",
|
|
||||||
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: 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.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
|
|
||||||
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) {
|
|
||||||
wx.showLoading({
|
|
||||||
title: '获取设备的UUID成功',
|
|
||||||
})
|
|
||||||
this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
// 获取蓝牙设备某个服务中所有特征值(characteristic)
|
|
||||||
/**
|
|
||||||
* read: true读,write: true写,notify: true广播
|
|
||||||
*/
|
|
||||||
getBLEDeviceCharacteristics(deviceId, serviceId) {
|
|
||||||
this._deviceId = deviceId
|
|
||||||
this._serviceId = serviceId
|
|
||||||
this._device.serviceId = serviceId
|
|
||||||
wx.getBLEDeviceCharacteristics({
|
|
||||||
deviceId,
|
|
||||||
serviceId,
|
|
||||||
success: (res) => {
|
|
||||||
console.log('getBLEDeviceCharacteristics success', res.characteristics)
|
|
||||||
wx.showLoading({
|
|
||||||
title: '获取特征值成功',
|
|
||||||
})
|
|
||||||
//FFE1:write: true, FFE2:notify: true,FFE3:read: 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({
|
|
||||||
text: "测量完成"
|
|
||||||
})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
fail(res) {
|
|
||||||
console.log("测量失败", res.value);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 断开蓝牙模块
|
|
||||||
*/
|
|
||||||
closeBluetoothAdapter() {
|
|
||||||
wx.closeBluetoothAdapter()
|
|
||||||
this._discoveryStarted = false
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙模块',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
devices: [],
|
|
||||||
weight: "",
|
|
||||||
text: "",
|
|
||||||
imp: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
// 断开与低功耗蓝牙设备的连接
|
|
||||||
closeBLEConnection() {
|
|
||||||
wx.closeBLEConnection({
|
|
||||||
deviceId: this._deviceId
|
|
||||||
})
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙连接',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
connected: false,
|
|
||||||
devices: [],
|
|
||||||
text: "",
|
|
||||||
weight: "",
|
|
||||||
imp: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,42 +0,0 @@
|
||||||
<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>{{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>
|
|
||||||
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
/* pages/PCD01PRO/index.wxss */
|
|
||||||
|
|
@ -1,339 +0,0 @@
|
||||||
const util = require("../../utils/util");
|
|
||||||
const {
|
|
||||||
inArray,
|
|
||||||
ab2hex
|
|
||||||
} = util
|
|
||||||
|
|
||||||
Page({
|
|
||||||
data: {
|
|
||||||
devices: [],
|
|
||||||
connected: false,
|
|
||||||
cmd: '',
|
|
||||||
name: '',
|
|
||||||
weight: "",
|
|
||||||
height:"",
|
|
||||||
text: "",
|
|
||||||
imp: "",
|
|
||||||
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: 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.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].isPrimary && 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) {
|
|
||||||
this._deviceId = deviceId
|
|
||||||
this._serviceId = serviceId
|
|
||||||
this._device.serviceId = serviceId
|
|
||||||
wx.getBLEDeviceCharacteristics({
|
|
||||||
deviceId,
|
|
||||||
serviceId,
|
|
||||||
success: (res) => {
|
|
||||||
console.log('getBLEDeviceCharacteristics success', res.characteristics)
|
|
||||||
wx.showLoading({
|
|
||||||
title: '获取特征值成功',
|
|
||||||
})
|
|
||||||
//FFE1:write: true, FFE2:notify: true,FFE3:read: 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 type = value.substring(8, 10)
|
|
||||||
let typeInfo = value.substring(10, 12)
|
|
||||||
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) / 10
|
|
||||||
that.setData({
|
|
||||||
height: "身高是:" + 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") {
|
|
||||||
console.log("测量完成")
|
|
||||||
that.setData({
|
|
||||||
text: "测量完成"
|
|
||||||
})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
fail(res) {
|
|
||||||
console.log("测量失败", res.value);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 断开蓝牙模块
|
|
||||||
*/
|
|
||||||
closeBluetoothAdapter() {
|
|
||||||
wx.closeBluetoothAdapter()
|
|
||||||
this._discoveryStarted = false
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙模块',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
devices: [],
|
|
||||||
weight: "",
|
|
||||||
height:"",
|
|
||||||
text: "",
|
|
||||||
imp: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
// 断开与低功耗蓝牙设备的连接
|
|
||||||
closeBLEConnection() {
|
|
||||||
wx.closeBLEConnection({
|
|
||||||
deviceId: this._deviceId
|
|
||||||
})
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙连接',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
connected: false,
|
|
||||||
devices: [],
|
|
||||||
text: "",
|
|
||||||
height:"",
|
|
||||||
weight: "",
|
|
||||||
imp: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,43 +0,0 @@
|
||||||
<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>
|
|
||||||
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
/* pages/PCD01PRO/index.wxss */
|
|
||||||
|
|
@ -1,287 +0,0 @@
|
||||||
const util = require("../../utils/util");
|
|
||||||
const {
|
|
||||||
inArray,
|
|
||||||
ab2hex
|
|
||||||
} = util
|
|
||||||
const plugin = requirePlugin("sdkPlugin").AiLink;
|
|
||||||
Page({
|
|
||||||
data: {
|
|
||||||
devices: [],
|
|
||||||
connected: false,
|
|
||||||
cmd: '',
|
|
||||||
name: '',
|
|
||||||
weight: "",
|
|
||||||
height: "",
|
|
||||||
text: "",
|
|
||||||
imp: "",
|
|
||||||
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: 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 && !device.localName) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
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].isPrimary && 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)
|
|
||||||
// 初始化插件
|
|
||||||
plugin.initPlugin(res.characteristics, that._device)
|
|
||||||
|
|
||||||
wx.onBLECharacteristicValueChange((characteristic) => {
|
|
||||||
let bleData = plugin.parseBleData(characteristic.value)
|
|
||||||
let dw1 = "kg"
|
|
||||||
if (bleData.status == 0) {
|
|
||||||
console.log("握手成功")
|
|
||||||
} else if (bleData.status == 1) {
|
|
||||||
let payload = ab2hex(bleData.data, '')
|
|
||||||
let type = payload.substring(0, 2)
|
|
||||||
let typeInfo = payload.substring(4, 6)
|
|
||||||
console.log("payload", payload)
|
|
||||||
if (type == "10" || type == "40") { //体脂模式
|
|
||||||
let data = parseInt(payload.substring(6, 12), 16)
|
|
||||||
let num = payload.substring(12, 13)
|
|
||||||
let dw = payload.substring(13, 14)
|
|
||||||
if (dw == "1") {
|
|
||||||
dw1 = "斤"
|
|
||||||
}
|
|
||||||
if (num == "1") {
|
|
||||||
data = data / 10
|
|
||||||
}
|
|
||||||
if (num == "2") {
|
|
||||||
data = data / 100
|
|
||||||
}
|
|
||||||
if (num == "3") {
|
|
||||||
data = data / 1000
|
|
||||||
}
|
|
||||||
if (typeInfo == "01") {
|
|
||||||
that.setData({
|
|
||||||
weight: "实时体重是:" + data + dw1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (typeInfo == "02") {
|
|
||||||
that.setData({
|
|
||||||
weight: "稳定体重是:" + data + dw1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (type == "14" || type == "41") { //身高模式
|
|
||||||
let height = parseInt(payload.substring(4, 8), 16)
|
|
||||||
that.setData({
|
|
||||||
height: "身高是:" + height
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (type == "11") { //阻抗模式
|
|
||||||
if (typeInfo == "03" || typeInfo == "04") {
|
|
||||||
let imp = parseInt(payload.substring(8, 12), 16)
|
|
||||||
console.log("imp", payload, imp)
|
|
||||||
that.setData({
|
|
||||||
imp: "阻抗值:" + imp
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
fail(res) {
|
|
||||||
console.error('getBLEDeviceCharacteristics', res)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 断开蓝牙模块
|
|
||||||
*/
|
|
||||||
closeBluetoothAdapter() {
|
|
||||||
wx.closeBluetoothAdapter()
|
|
||||||
this._discoveryStarted = false
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙模块',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
devices: [],
|
|
||||||
weight: "",
|
|
||||||
height: "",
|
|
||||||
text: "",
|
|
||||||
imp: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
// 断开与低功耗蓝牙设备的连接
|
|
||||||
closeBLEConnection() {
|
|
||||||
wx.closeBLEConnection({
|
|
||||||
deviceId: this._deviceId
|
|
||||||
})
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙连接',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
connected: false,
|
|
||||||
devices: [],
|
|
||||||
text: "",
|
|
||||||
height: "",
|
|
||||||
weight: "",
|
|
||||||
imp: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,43 +0,0 @@
|
||||||
<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>
|
|
||||||
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
/* pages/PCD01PRO/index.wxss */
|
|
||||||
|
|
@ -1,406 +0,0 @@
|
||||||
const util = require("../../utils/util");
|
|
||||||
const {
|
|
||||||
inArray,
|
|
||||||
ab2hex
|
|
||||||
} = util
|
|
||||||
Page({
|
|
||||||
data: {
|
|
||||||
devices: [],
|
|
||||||
connected: false,
|
|
||||||
cmd: '',
|
|
||||||
name: '',
|
|
||||||
weight: "",
|
|
||||||
height: "",
|
|
||||||
text: "",
|
|
||||||
imp0: '',
|
|
||||||
imp1: '',
|
|
||||||
imp2: '',
|
|
||||||
imp3: '',
|
|
||||||
imp4: '',
|
|
||||||
imp5: '',
|
|
||||||
imp7: '',
|
|
||||||
uuid1: null,
|
|
||||||
uuid2: null,
|
|
||||||
uuid3: null,
|
|
||||||
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: 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 && !device.localName) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
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: "",
|
|
||||||
imp0: '',
|
|
||||||
imp1: '',
|
|
||||||
imp2: '',
|
|
||||||
imp3: '',
|
|
||||||
imp4: '',
|
|
||||||
imp5: '',
|
|
||||||
imp7: '',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
// 获取蓝牙设备的 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) {
|
|
||||||
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)
|
|
||||||
wx.showLoading({
|
|
||||||
title: '获取特征值成功',
|
|
||||||
})
|
|
||||||
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 //写入设置
|
|
||||||
that.notifyBLECharacteristicValue(deviceId, serviceId, item.uuid)
|
|
||||||
} else if (item.uuid.indexOf('FFE3') != -1) {
|
|
||||||
that.data.uuid3 = item.uuid //监听数据
|
|
||||||
}
|
|
||||||
}
|
|
||||||
wx.hideLoading()
|
|
||||||
},
|
|
||||||
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((characteristic) => {
|
|
||||||
let value = ab2hex(characteristic.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("type", type)
|
|
||||||
if (type == "01") { // 称重模式
|
|
||||||
let data = parseInt(value.substring(13, 18), 16)
|
|
||||||
let dw1 = "kg"
|
|
||||||
if (dw == "1") {
|
|
||||||
dw1 = "斤"
|
|
||||||
}
|
|
||||||
if (dw == "4") {
|
|
||||||
dw1 = "st:lb"
|
|
||||||
}
|
|
||||||
if (dw == "6") {
|
|
||||||
dw1 = "lb"
|
|
||||||
}
|
|
||||||
if (num == "1") {
|
|
||||||
data = parseInt(value.substring(13, 18), 16) / 10
|
|
||||||
}
|
|
||||||
if (num == "2") {
|
|
||||||
data = parseInt(value.substring(13, 18), 16) / 100
|
|
||||||
}
|
|
||||||
if (num == "3") {
|
|
||||||
data = parseInt(value.substring(13, 18), 16) / 1000
|
|
||||||
}
|
|
||||||
if (typeInfo == "01") {
|
|
||||||
that.setData({
|
|
||||||
weight: "实时体重是:" + data + dw1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (typeInfo == "02") {
|
|
||||||
that.setData({
|
|
||||||
weight: "稳定体重是:" + data + dw1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (type == "02") { //阻抗
|
|
||||||
let mcu = value.substring(12, 14)
|
|
||||||
console.log("typeInfo", typeInfo)
|
|
||||||
if (typeInfo == "02") {
|
|
||||||
that.setData({
|
|
||||||
imp0: "阻抗值测量失败"
|
|
||||||
})
|
|
||||||
} else if (typeInfo == "03") {
|
|
||||||
if (mcu == "00") {
|
|
||||||
let imp0 = parseInt(value.substring(14, 22), 16)
|
|
||||||
that.setData({
|
|
||||||
imp0: "双脚阻抗:" + imp0
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (mcu == "01") {
|
|
||||||
let imp1 = parseInt(value.substring(14, 22), 16)
|
|
||||||
that.setData({
|
|
||||||
imp1: "双手阻抗:" + imp1
|
|
||||||
})
|
|
||||||
console.log("双手", imp1)
|
|
||||||
}
|
|
||||||
if (mcu == "02") {
|
|
||||||
let imp2 = parseInt(value.substring(14, 22), 16)
|
|
||||||
that.setData({
|
|
||||||
imp2: "左手阻抗:" + imp2
|
|
||||||
})
|
|
||||||
console.log("左手阻抗", imp2)
|
|
||||||
}
|
|
||||||
if (mcu == "03") {
|
|
||||||
let imp3 = parseInt(value.substring(14, 22), 16)
|
|
||||||
that.setData({
|
|
||||||
imp3: "右手阻抗:" + imp3
|
|
||||||
})
|
|
||||||
console.log("右手阻抗", imp3)
|
|
||||||
}
|
|
||||||
if (mcu == "04") {
|
|
||||||
let imp4 = parseInt(value.substring(14, 22), 16)
|
|
||||||
that.setData({
|
|
||||||
imp4: "左脚阻抗:" + imp4
|
|
||||||
})
|
|
||||||
console.log("左脚阻抗", imp4)
|
|
||||||
}
|
|
||||||
if (mcu == "05") {
|
|
||||||
let imp5 = parseInt(value.substring(14, 22), 16)
|
|
||||||
that.setData({
|
|
||||||
imp5: "右脚阻抗:" + imp5
|
|
||||||
})
|
|
||||||
console.log("右脚阻抗", imp5)
|
|
||||||
}
|
|
||||||
if (mcu == "07") {
|
|
||||||
let imp7 = parseInt(value.substring(14, 22), 16)
|
|
||||||
that.setData({
|
|
||||||
imp7: "右全身阻抗:" + imp7
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (type == "05") { //身高
|
|
||||||
let height = parseInt(value.substring(14, 18), 16) / 10
|
|
||||||
let dw1 = "cm"
|
|
||||||
if (dw == "01") {
|
|
||||||
dw1 = "inch"
|
|
||||||
}
|
|
||||||
if (dw == "02") {
|
|
||||||
dw1 = "ft-in"
|
|
||||||
}
|
|
||||||
if (typeInfo == "02") {
|
|
||||||
that.setData({
|
|
||||||
height: "身高:" + height + "单位:" + dw1
|
|
||||||
})
|
|
||||||
} else if (typeInfo == "03" || typeInfo == "04") {
|
|
||||||
that.setData({
|
|
||||||
height: "身高测量失败"
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
fail(res) {
|
|
||||||
console.error('getBLEDeviceCharacteristics', res)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 断开蓝牙模块
|
|
||||||
*/
|
|
||||||
closeBluetoothAdapter() {
|
|
||||||
wx.closeBluetoothAdapter()
|
|
||||||
this._discoveryStarted = false
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙模块',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
devices: [],
|
|
||||||
weight: "",
|
|
||||||
height: "",
|
|
||||||
text: "",
|
|
||||||
imp: "",
|
|
||||||
imp0: '',
|
|
||||||
imp1: '',
|
|
||||||
imp2: '',
|
|
||||||
imp3: '',
|
|
||||||
imp4: '',
|
|
||||||
imp5: '',
|
|
||||||
imp7: '',
|
|
||||||
})
|
|
||||||
},
|
|
||||||
// 断开与低功耗蓝牙设备的连接
|
|
||||||
closeBLEConnection() {
|
|
||||||
wx.closeBLEConnection({
|
|
||||||
deviceId: this._deviceId
|
|
||||||
})
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙连接',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
connected: false,
|
|
||||||
devices: [],
|
|
||||||
text: "",
|
|
||||||
height: "",
|
|
||||||
weight: "",
|
|
||||||
imp: "",
|
|
||||||
imp0: '',
|
|
||||||
imp1: '',
|
|
||||||
imp2: '',
|
|
||||||
imp3: '',
|
|
||||||
imp4: '',
|
|
||||||
imp5: '',
|
|
||||||
imp7: '',
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,48 +0,0 @@
|
||||||
<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>{{imp0}}</view>
|
|
||||||
<view>{{imp1}}</view>
|
|
||||||
<view>{{imp2}}</view>
|
|
||||||
<view>{{imp3}}</view>
|
|
||||||
<view>{{imp4}}</view>
|
|
||||||
<view>{{imp5}}</view>
|
|
||||||
<view>{{imp7}}</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>
|
|
||||||
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
/* pages/PCD01PRO/index.wxss */
|
|
||||||
|
|
@ -1,430 +0,0 @@
|
||||||
const util = require("../../utils/util");
|
|
||||||
const {
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
// 开始搜寻附近的蓝牙外围设备
|
|
||||||
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: ""
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
// 获取蓝牙设备的 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: '断开蓝牙模块',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
devices: [],
|
|
||||||
weight: "",
|
|
||||||
text: "",
|
|
||||||
imp: "",
|
|
||||||
isType: false
|
|
||||||
})
|
|
||||||
},
|
|
||||||
// 断开与低功耗蓝牙设备的连接
|
|
||||||
closeBLEConnection() {
|
|
||||||
wx.closeBLEConnection({
|
|
||||||
deviceId: this._deviceId
|
|
||||||
})
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙连接',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
connected: false,
|
|
||||||
isType: false,
|
|
||||||
devices: [],
|
|
||||||
text: "",
|
|
||||||
weight: "",
|
|
||||||
imp: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
@ -1,442 +0,0 @@
|
||||||
const util = require("../../utils/util");
|
|
||||||
const {
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
// 开始搜寻附近的蓝牙外围设备
|
|
||||||
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: "",
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
// 获取蓝牙设备某个服务中所有特征值(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, "");
|
|
||||||
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, type);
|
|
||||||
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,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,52 +0,0 @@
|
||||||
<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>
|
|
||||||
<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>
|
|
||||||
|
|
@ -1,104 +0,0 @@
|
||||||
/* pages/PCH0809/index.wxss */
|
|
||||||
.modal_wrapper {
|
|
||||||
position: fixed;
|
|
||||||
top: 0;
|
|
||||||
left: 0;
|
|
||||||
right: 0;
|
|
||||||
z-index: -1;
|
|
||||||
background: rgba(0, 0, 0, 0.5);
|
|
||||||
transition: .4s all;
|
|
||||||
height: 200vh;
|
|
||||||
opacity: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.modal_wrapper_show {
|
|
||||||
transform: translateY(-100vh);
|
|
||||||
opacity: 1;
|
|
||||||
overflow-x: hidden;
|
|
||||||
overflow-y: auto;
|
|
||||||
z-index: 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
.remindModal {
|
|
||||||
position: absolute;
|
|
||||||
bottom: 50vh;
|
|
||||||
left: 50%;
|
|
||||||
z-index: 101;
|
|
||||||
transform: translate(-50%, 50%);
|
|
||||||
background: #fff;
|
|
||||||
width: 600rpx;
|
|
||||||
padding: 40rpx 60rpx;
|
|
||||||
border-radius: 20rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.remindModal_title {
|
|
||||||
padding: 10rpx 30rpx 50rpx;
|
|
||||||
text-align: center;
|
|
||||||
font-size: 32rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.remindModal_content {
|
|
||||||
font-size: 30rpx;
|
|
||||||
line-height: 1.6;
|
|
||||||
text-align: center;
|
|
||||||
padding-top: 30rpx;
|
|
||||||
min-height: 100rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.remindModal_btn {
|
|
||||||
margin-top: 80rpx;
|
|
||||||
margin-bottom: 10rpx;
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-evenly;
|
|
||||||
}
|
|
||||||
|
|
||||||
.remindModal_cancel_btn {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
width: 45%;
|
|
||||||
height: 100rpx;
|
|
||||||
border: 1px solid transparent;
|
|
||||||
margin: auto;
|
|
||||||
background: #dfdfdf;
|
|
||||||
color: #fff;
|
|
||||||
font-size: 16px;
|
|
||||||
letter-spacing: 2rpx;
|
|
||||||
border-radius: 10px;
|
|
||||||
white-space: nowrap;
|
|
||||||
}
|
|
||||||
|
|
||||||
.modal_input_wrap {
|
|
||||||
width: 100%;
|
|
||||||
height: 80rpx;
|
|
||||||
padding: 0 10rpx;
|
|
||||||
border: 1px solid #dcdcdc;
|
|
||||||
border-radius: 10rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.modal_input {
|
|
||||||
width: 100%;
|
|
||||||
height: 100%;
|
|
||||||
padding: 10rpx;
|
|
||||||
font-size: 30rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.confirm_btn {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
width: 45%;
|
|
||||||
height: 100rpx;
|
|
||||||
border: 1px solid transparent;
|
|
||||||
margin: auto;
|
|
||||||
background: #1977FF;
|
|
||||||
color: #fff;
|
|
||||||
font-size: 16px;
|
|
||||||
letter-spacing: 2rpx;
|
|
||||||
border-radius: 10px;
|
|
||||||
white-space: nowrap;
|
|
||||||
}
|
|
||||||
|
|
||||||
.item_hover {
|
|
||||||
background-color: rgba(0, 0, 0, .1);
|
|
||||||
}
|
|
||||||
|
|
@ -1,184 +0,0 @@
|
||||||
const util = require("../../utils/util");
|
|
||||||
const {
|
|
||||||
inArray,
|
|
||||||
ab2hex
|
|
||||||
} = util
|
|
||||||
const plugin = requirePlugin("sdkPlugin").AiLink;
|
|
||||||
Page({
|
|
||||||
data: {
|
|
||||||
connected: false,
|
|
||||||
name: '',
|
|
||||||
weight: "",
|
|
||||||
height: "",
|
|
||||||
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: 1000, //上报设备的间隔
|
|
||||||
services: [
|
|
||||||
"F0A0",
|
|
||||||
],
|
|
||||||
success: (res) => {
|
|
||||||
this.onBluetoothDeviceFound()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
},
|
|
||||||
// 停止搜寻附近的蓝牙外围设备
|
|
||||||
stopBluetoothDevicesDiscovery() {
|
|
||||||
wx.stopBluetoothDevicesDiscovery()
|
|
||||||
},
|
|
||||||
|
|
||||||
// 找到新设备的事件
|
|
||||||
onBluetoothDeviceFound() {
|
|
||||||
let that = this
|
|
||||||
wx.onBluetoothDeviceFound((res) => {
|
|
||||||
res.devices.forEach(device => {
|
|
||||||
if (!device.name && !device.localName) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (device.advertisServiceUUIDs[0].indexOf("F0A0") !== -1) {
|
|
||||||
let parseDataRes = plugin.parseBroadcastData(device.advertisData)
|
|
||||||
let analyzeData = plugin.analyzeBroadcastScaleData(parseDataRes)
|
|
||||||
let analyzeDataText = analyzeData.text
|
|
||||||
let data = analyzeData.data
|
|
||||||
const foundDevices = this.data.devices
|
|
||||||
const idx = inArray(foundDevices, 'deviceId', device.deviceId)
|
|
||||||
const dataT = {}
|
|
||||||
let buffer = device.advertisData.slice(0, 8)
|
|
||||||
device.mac = new Uint8Array(buffer) // 保存广播数据中的mac地址,这是由于iOS不直接返回mac地址
|
|
||||||
let tempMac = Array.from(device.mac)
|
|
||||||
tempMac.reverse()
|
|
||||||
device.macAddr = ab2hex(tempMac, ':').toUpperCase()
|
|
||||||
that.imp = dataT.adc
|
|
||||||
if (idx === -1) {
|
|
||||||
dataT[`devices[${foundDevices.length}]`] = device
|
|
||||||
} else {
|
|
||||||
dataT[`devices[${idx}]`] = device
|
|
||||||
}
|
|
||||||
this.setData(dataT)
|
|
||||||
console.log("analyzeData", analyzeData)
|
|
||||||
if (parseDataRes.status == 1) {
|
|
||||||
let dw1 = "kg"
|
|
||||||
let dwH = "CM"
|
|
||||||
// 体重单位
|
|
||||||
if (data.weightUnit == "1") {
|
|
||||||
dw1 = "斤"
|
|
||||||
}
|
|
||||||
if (data.weightUnit == "4") {
|
|
||||||
dw1 = "st:lb"
|
|
||||||
data.weight = 1 * data.weight + 5
|
|
||||||
}
|
|
||||||
if (data.weightUnit == "6") {
|
|
||||||
dw1 = "lb"
|
|
||||||
}
|
|
||||||
if (data.weightDecimal == "1") {
|
|
||||||
data.weight = data.weight / 10
|
|
||||||
}
|
|
||||||
if (data.weightDecimal == "2") {
|
|
||||||
data.weight = data.weight / 100
|
|
||||||
}
|
|
||||||
if (data.weightDecimal == "3") {
|
|
||||||
data.weight = data.weight / 1000
|
|
||||||
}
|
|
||||||
that.setData({
|
|
||||||
weight: analyzeData.text
|
|
||||||
})
|
|
||||||
// that.setData({
|
|
||||||
// weight: "您的体重是:" + data.weight + dw1
|
|
||||||
// })
|
|
||||||
//身高单位
|
|
||||||
// if (data.tempUnit == "1") {
|
|
||||||
// dwH = "FT"
|
|
||||||
// data.tempNegative = data.tempNegative * 2.54
|
|
||||||
// }
|
|
||||||
// that.setData({
|
|
||||||
// height: "您的身高是:" + data.tempNegative / 10 + dwH
|
|
||||||
// })
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
})
|
|
||||||
},
|
|
||||||
//监听蓝牙连接状态
|
|
||||||
onBLEConnectionStateChange() {
|
|
||||||
wx.onBLEConnectionStateChange((res) => {
|
|
||||||
if (!res.connected) {
|
|
||||||
wx.stopBluetoothDevicesDiscovery();
|
|
||||||
setTimeout(() => {
|
|
||||||
wx.showToast({
|
|
||||||
title: '连接已断开',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
}, 500)
|
|
||||||
this.setData({
|
|
||||||
connected: false,
|
|
||||||
devices: [],
|
|
||||||
weight: "",
|
|
||||||
height: "",
|
|
||||||
text: "",
|
|
||||||
imp: ""
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 断开蓝牙模块
|
|
||||||
*/
|
|
||||||
closeBluetoothAdapter() {
|
|
||||||
wx.stopBluetoothDevicesDiscovery();
|
|
||||||
wx.closeBluetoothAdapter()
|
|
||||||
this._discoveryStarted = false
|
|
||||||
wx.showToast({
|
|
||||||
title: '结束流程',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
devices: [],
|
|
||||||
weight: "",
|
|
||||||
height: "",
|
|
||||||
text: "",
|
|
||||||
imp: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,41 +0,0 @@
|
||||||
<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>
|
|
||||||
<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 +0,0 @@
|
||||||
/* pages/PCD01PRO/index.wxss */
|
|
||||||
|
|
@ -1,164 +0,0 @@
|
||||||
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: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,40 +0,0 @@
|
||||||
<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 +0,0 @@
|
||||||
/* pages/PCD01PRO/index.wxss */
|
|
||||||
|
|
@ -1,164 +0,0 @@
|
||||||
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: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,40 +0,0 @@
|
||||||
<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 +0,0 @@
|
||||||
/* pages/PCD01PRO/index.wxss */
|
|
||||||
|
|
@ -1,280 +0,0 @@
|
||||||
const util = require("../../utils/util");
|
|
||||||
const {
|
|
||||||
inArray,
|
|
||||||
ab2hex
|
|
||||||
} = util
|
|
||||||
const plugin = requirePlugin("sdkPlugin").AiLink;
|
|
||||||
Page({
|
|
||||||
data: {
|
|
||||||
devices: [],
|
|
||||||
connected: false,
|
|
||||||
cmd: '',
|
|
||||||
name: '',
|
|
||||||
weight: "",
|
|
||||||
text: "",
|
|
||||||
imp: "",
|
|
||||||
uuid1: "",
|
|
||||||
uuid2: "",
|
|
||||||
uuid3: "",
|
|
||||||
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: 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.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
|
|
||||||
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) {
|
|
||||||
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
|
|
||||||
this._deviceId = deviceId
|
|
||||||
this._serviceId = serviceId
|
|
||||||
this._device.serviceId = serviceId
|
|
||||||
wx.getBLEDeviceCharacteristics({
|
|
||||||
deviceId,
|
|
||||||
serviceId,
|
|
||||||
success: (res) => {
|
|
||||||
console.log("特征值:", res)
|
|
||||||
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('FE2') != -1) {
|
|
||||||
that.data.uuid2 = item.uuid //写入设置
|
|
||||||
} else if (item.uuid.indexOf('FFE3') != -1) {
|
|
||||||
that.data.uuid3 = item.uuid //监听数据
|
|
||||||
}
|
|
||||||
}
|
|
||||||
wx.notifyBLECharacteristicValueChange({
|
|
||||||
deviceId,
|
|
||||||
serviceId,
|
|
||||||
characteristicId: that.data.uuid2,
|
|
||||||
state: true,
|
|
||||||
})
|
|
||||||
wx.notifyBLECharacteristicValueChange({
|
|
||||||
deviceId,
|
|
||||||
serviceId,
|
|
||||||
characteristicId: that.data.uuid3,
|
|
||||||
state: true,
|
|
||||||
})
|
|
||||||
plugin.initPlugin(res.characteristics, this._device)
|
|
||||||
wx.onBLECharacteristicValueChange((characteristic) => {
|
|
||||||
let bleData = plugin.parseBleData(characteristic.value)
|
|
||||||
console.log("bleData", bleData)
|
|
||||||
if (bleData.status == 0) {
|
|
||||||
console.log("握手成功")
|
|
||||||
} else if (bleData.status == 1) {
|
|
||||||
let payload = ab2hex(bleData.data, '')
|
|
||||||
let type = payload.substring(0, 2)
|
|
||||||
let typeInfo = payload.substring(4, 6)
|
|
||||||
console.log("A7-----^", payload)
|
|
||||||
} else if (bleData.status == 2) {
|
|
||||||
let payload = ab2hex(bleData.completeData, '')
|
|
||||||
let type = payload.substring(0, 2)
|
|
||||||
let typeInfo = payload.substring(4, 6)
|
|
||||||
console.log('A6-----^', payload)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
},
|
|
||||||
fail(res) {
|
|
||||||
console.error('getBLEDeviceCharacteristics', res)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
submitCmd() {
|
|
||||||
let str = [0xA6, 0x02, 0x1A, 0x01, 0x1D, 0x6A]
|
|
||||||
// let str = [0xA6, 0x01, 0x26, 0x27, 0x6A]
|
|
||||||
console.log("唤醒指令", str)
|
|
||||||
plugin.sendDataOfA6(str)
|
|
||||||
},
|
|
||||||
/**
|
|
||||||
* 断开蓝牙模块
|
|
||||||
*/
|
|
||||||
closeBluetoothAdapter() {
|
|
||||||
wx.closeBluetoothAdapter()
|
|
||||||
this._discoveryStarted = false
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙模块',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
devices: [],
|
|
||||||
weight: "",
|
|
||||||
text: "",
|
|
||||||
imp: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
// 断开与低功耗蓝牙设备的连接
|
|
||||||
closeBLEConnection() {
|
|
||||||
wx.closeBLEConnection({
|
|
||||||
deviceId: this._deviceId
|
|
||||||
})
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙连接',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
connected: false,
|
|
||||||
devices: [],
|
|
||||||
text: "",
|
|
||||||
weight: "",
|
|
||||||
imp: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,42 +0,0 @@
|
||||||
<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>{{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>
|
|
||||||
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
/* pages/PCD01PRO/index.wxss */
|
|
||||||
|
|
@ -1,413 +0,0 @@
|
||||||
const util = require("../../utils/util");
|
|
||||||
const {
|
|
||||||
inArray,
|
|
||||||
ab2hex
|
|
||||||
} = util
|
|
||||||
|
|
||||||
Page({
|
|
||||||
data: {
|
|
||||||
devices: [],
|
|
||||||
connected: false,
|
|
||||||
cmd: '',
|
|
||||||
name: '',
|
|
||||||
weight: "",
|
|
||||||
text: "",
|
|
||||||
imp: "",
|
|
||||||
deviceId: null,
|
|
||||||
},
|
|
||||||
onLoad: function() {},
|
|
||||||
// 初始化蓝牙模块
|
|
||||||
editclick: function(e) {
|
|
||||||
let type = e.currentTarget.dataset.name
|
|
||||||
if (type == 'PCD01PRO') {
|
|
||||||
wx.navigateTo({
|
|
||||||
url: `/pages/PCD01PRO/index`
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (type == 'H01PRO') {
|
|
||||||
wx.navigateTo({
|
|
||||||
url: `/pages/H01PRO/index`
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type == 'PCH0809') {
|
|
||||||
wx.navigateTo({
|
|
||||||
url: `/pages/PCH0809/index`
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (type == 'PCF01pro') {
|
|
||||||
wx.navigateTo({
|
|
||||||
url: `/pages/PCF01pro/index`
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (type == 'PCF01proFRK') {
|
|
||||||
wx.navigateTo({
|
|
||||||
url: `/pages/PCF01proFRK/index`
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (type == 'PCF08') {
|
|
||||||
wx.navigateTo({
|
|
||||||
url: `/pages/PCF08/index`
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (type == 'PCJ02') {
|
|
||||||
wx.navigateTo({
|
|
||||||
url: `/pages/PCJ02/index`
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (type == 'FB03') {
|
|
||||||
wx.navigateTo({
|
|
||||||
url: `/pages/FB03/index`
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (type == 'G01') {
|
|
||||||
wx.navigateTo({
|
|
||||||
url: `/pages/G01/index`
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (type == 'h018') {
|
|
||||||
wx.navigateTo({
|
|
||||||
url: `/pages/h018/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({
|
|
||||||
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, //是否允许重复上报同一设备
|
|
||||||
services: [ //要搜索蓝牙设备主 service 的 uuid 列表
|
|
||||||
"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.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
|
|
||||||
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) {
|
|
||||||
wx.showLoading({
|
|
||||||
title: '获取设备的UUID成功',
|
|
||||||
})
|
|
||||||
this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
// 获取蓝牙设备某个服务中所有特征值(characteristic)
|
|
||||||
/**
|
|
||||||
* read: true读,write: true写,notify: true广播
|
|
||||||
*/
|
|
||||||
getBLEDeviceCharacteristics(deviceId, serviceId) {
|
|
||||||
this._deviceId = deviceId
|
|
||||||
this._serviceId = serviceId
|
|
||||||
this._device.serviceId = serviceId
|
|
||||||
wx.getBLEDeviceCharacteristics({
|
|
||||||
deviceId,
|
|
||||||
serviceId,
|
|
||||||
success: (res) => {
|
|
||||||
console.log('getBLEDeviceCharacteristics success', res.characteristics)
|
|
||||||
wx.showLoading({
|
|
||||||
title: '获取特征值成功',
|
|
||||||
})
|
|
||||||
//FFE1:write: true, FFE2:notify: true,FFE3:read: 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({
|
|
||||||
text: "测量完成"
|
|
||||||
})
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
fail(res) {
|
|
||||||
console.log("测量失败", res.value);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 断开蓝牙模块
|
|
||||||
*/
|
|
||||||
closeBluetoothAdapter() {
|
|
||||||
wx.closeBluetoothAdapter()
|
|
||||||
this._discoveryStarted = false
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙模块',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
devices: [],
|
|
||||||
weight: "",
|
|
||||||
text: "",
|
|
||||||
imp: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
// 断开与低功耗蓝牙设备的连接
|
|
||||||
closeBLEConnection() {
|
|
||||||
wx.closeBLEConnection({
|
|
||||||
deviceId: this._deviceId
|
|
||||||
})
|
|
||||||
wx.showToast({
|
|
||||||
title: '断开蓝牙连接',
|
|
||||||
icon: 'none'
|
|
||||||
})
|
|
||||||
this.setData({
|
|
||||||
connected: false,
|
|
||||||
devices: [],
|
|
||||||
text: "",
|
|
||||||
weight: "",
|
|
||||||
imp: ""
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"usingComponents": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
<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="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="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="G01">G01</view>
|
|
||||||
<!-- <view class="item" bindtap="editclick" data-name="PCF01pro">PCF01pro(新)</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>
|
|
||||||
|
|
@ -1,56 +0,0 @@
|
||||||
{
|
|
||||||
"description": "项目配置文件,详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html",
|
|
||||||
"packOptions": {
|
|
||||||
"ignore": [],
|
|
||||||
"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": 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": {}
|
|
||||||
}
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
{
|
|
||||||
"desc": "关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html",
|
|
||||||
"rules": [{
|
|
||||||
"action": "allow",
|
|
||||||
"page": "*"
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
||||||
function inArray(arr, key, val) {
|
|
||||||
if (!arr || !arr.length || typeof arr != 'object' || !Array.isArray(arr)) {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
for (let i = 0; i < arr.length; i++) {
|
|
||||||
if (!key) {
|
|
||||||
if (arr[i] == val) {
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
} else if (arr[i][key] === val) {
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ArrayBuffer转16进度字符串示例
|
|
||||||
function ab2hex(buffer, split) {
|
|
||||||
var hexArr = Array.prototype.map.call(
|
|
||||||
new Uint8Array(buffer),
|
|
||||||
function(bit) {
|
|
||||||
return ('00' + bit.toString(16)).slice(-2)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
return hexArr.join(split);
|
|
||||||
}
|
|
||||||
module.exports = {
|
|
||||||
inArray,
|
|
||||||
ab2hex,
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue