1
0
forked from dyf/APP

650全局蓝牙优化

This commit is contained in:
liub
2025-08-14 17:25:53 +08:00
parent 1ed6ca4828
commit 958a74101e
8 changed files with 776 additions and 405 deletions

View File

@ -5,7 +5,7 @@
export default {
onLaunch: function() {
uni.clearStorageSync();
var ble = bleTool.getBleTool();
},
onShow: function() {

View File

@ -0,0 +1,198 @@
<template>
<view v-if="visible" class="loading-container" @touchmove.stop.prevent="handleTouchMove">
<view class="loading-content">
<!-- 刻度点容器 -->
<view class="clock-container">
<view v-for="(dot, index) in dots" :key="index" class="clock-dot" :style="{
transform: `rotate(${index * angle}deg) translateY(-${radius}px)`,
backgroundColor: getDotColor(index),
animationDuration: `${duration}ms`,
animationDelay: `${index * (duration / dotsCount)}ms`
}"></view>
</view>
<!-- 提示文本 -->
<text class="loading-text" :class="text?'':'displayNone'" :style="{ color: textColor }">{{ text }}</text>
</view>
</view>
</template>
<script>
export default {
data() {
return {
visible: false,
dotsCount: 12,
dotColors: ['#CEF231c2'],
text: '请稍候...',//文本文字
textColor: '#FFFFFFde',//文本颜色
radius: 50,//圆的半径
duration: 1200,//动画的播放速度
colorIndex: 0
}
},
computed: {
// 计算每个点之间的角度
angle() {
return 360 / this.dotsCount
},
// 生成刻度点数组
dots() {
return Array.from({
length: this.dotsCount
})
}
},
methods: {
// 获取刻度点颜色(实现无限循环变色)
getDotColor(index) {
// 根据当前颜色索引和刻度点位置计算颜色
const colorIndex = (index + this.colorIndex) % this.dotColors.length
return this.dotColors[colorIndex]
},
// 更新颜色索引(实现循环变色)
updateColorIndex() {
this.colorIndex = (this.colorIndex + 1) % this.dotColors.length
},
// 显示loading
show(options) {
if(!options){
options={};
}
this.update(options)
this.visible = true
// 启动颜色循环
if (!this.colorTimer) {
this.colorTimer = setInterval(() => {
this.updateColorIndex()
}, this.duration / this.dotColors.length)
}
},
// 隐藏loading
hide() {
this.visible = false
// 清除颜色循环定时器
if (this.colorTimer) {
clearInterval(this.colorTimer)
this.colorTimer = null
}
},
// 更新loading配置
update(options) {
if(!options){
options={a:1};
}
Object.keys(options).forEach(key => {
if (this[key] !== undefined) {
this[key] = options[key]
}
})
// 如果更新了颜色数组,重置颜色索引
if (options.dotColors) {
this.colorIndex = 0
}
},
// 阻止触摸移动事件
handleTouchMove() {}
},
beforeDestroy() {
// 组件销毁前清除定时器
if (this.colorTimer) {
clearInterval(this.colorTimer)
this.colorTimer = null
}
}
}
</script>
<style scoped>
/* 全屏遮罩层 */
.loading-container {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #000000c2;
display: flex;
justify-content: center;
align-items: center;
z-index: 99999999999;
pointer-events: auto;
}
/* 内容居中 */
.loading-content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
transform: translateZ(0);
/* 启用GPU加速 */
margin-top: -150rpx;
}
/* 刻度容器 */
.clock-container {
position: relative;
width: 200rpx;
height: 200rpx;
margin-bottom: 30rpx;
}
/* 单个刻度点 */
.clock-dot {
position: absolute;
top: 50%;
left: 50%;
width: 14rpx;
height: 52rpx;
border-radius: 12rpx;
/* margin-top: -4rpx;
margin-left: -4rpx; */
transform-origin: 0 0;
animation: colorScale infinite ease-in-out;
box-shadow: 0 0 8rpx rgba(0, 0, 0, 0.2);
}
/* 刻度点动画 - 颜色和大小变化 */
@keyframes colorScale {
0% {
opacity: 0.05;
}
100% {
opacity: 1;
}
}
/* 提示文本 */
.loading-text {
font-size: 32rpx;
font-weight: 400;
letter-spacing: 1.4rpx;
text-align: center;
max-width: 80vw;
padding: 0 20px;
line-height: 1.5;
font-family: 'PingFang SC';
}
.displayNone{
display: none !important;
}
</style>

View File

@ -247,12 +247,14 @@
}
}
},
onUnload() {
ble.removeReceiveCallback(this);
},
onLoad: function() {
var these = this;
let eventChannel = this.getOpenerEventChannel();
//eventChannel.on('detailData', function(data) {
//eventChannel.on('deviceControl', function(data) {
let data = {
data: {

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 867 B

View File

@ -53,23 +53,23 @@ class BleHelper {
},
{
key: '10003',
remark: '连接失败'
remark: '蓝牙设备连接失败'
},
{
key: '10004',
remark: '没有找到指定服务'
remark: '蓝牙设备没有找到指定服务'
},
{
key: '10005',
remark: '没有找到指定特征值'
remark: '蓝牙设备没有找到指定特征值'
},
{
key: '10006',
remark: '当前连接已断开'
remark: '蓝牙连接已断开'
},
{
key: '10007',
remark: '当前特征值不支持此操作'
remark: '蓝牙设备当前特征值不支持此操作'
},
{
key: '10008',
@ -77,7 +77,7 @@ class BleHelper {
},
{
key: '10009',
remark: '系统不支持 BLE低功耗蓝牙'
remark: '蓝牙设备不支持BLE低功耗蓝牙'
},
{
key: '10010',
@ -85,11 +85,11 @@ class BleHelper {
},
{
key: '10011',
remark: '配对设备需要配对码'
remark: '蓝牙设备配对设备需要配对码'
},
{
key: '10012',
remark: '连接超时'
remark: '蓝牙设备连接超时'
},
{
key: '10013',
@ -119,18 +119,22 @@ class BleHelper {
const pages = getCurrentPages();
if (pages.length === 0) {
return '';
return "";
}
const currentPage = pages[pages.length - 1];
console.log("currentPage=",currentPage.route);
return currentPage.route;
}
//设置发现新设备的回调
addDeviceFound(callback) {
let key = this.getCurrentPagePath();
if(!key){
key=new Date().getTime();
}
if (key) {
console.log("key="+key);
let f = this.cfg.onDeviceFound.findIndex((v) => {
return v.key == key;
});
@ -148,19 +152,29 @@ class BleHelper {
//移除发现新设备的回调
removeDeviceFound() {
let key = this.getCurrentPagePath();
if (key) {
console.log("key="+key);
let f = this.cfg.onDeviceFound.findIndex((v) => {
return v.key == key;
});
if (f > -1) {
this.cfg.onDeviceFound.splice(f, 1);
}
}else{
if(this.cfg.onDeviceFound.length>0){
this.cfg.onDeviceFound.splice(this.cfg.onDeviceFound.length-1, 1);
}
}
}
//添加接收到数据的回调
addReceiveCallback(callback) {
let key = this.getCurrentPagePath();
if(!key){
key=new Date().getTime();
}
if (key) {
let f = this.cfg.receivDataCallback.findIndex((v) => {
return v.key == key;
@ -179,9 +193,10 @@ class BleHelper {
//设置接收到数据的回调
removeReceiveCallback() {
removeReceiveCallback(ev) {
let key = this.getCurrentPagePath();
if (key) {
console.log("key="+key);
let f = this.cfg.receivDataCallback.findIndex((v) => {
return v.key == key;
});
@ -190,6 +205,10 @@ class BleHelper {
this.cfg.receivDataCallback.splice(f, 1);
}
}else{
if(this.cfg.receivDataCallback.length>0){
this.cfg.receivDataCallback.splice(this.cfg.receivDataCallback.length-1, 1);
}
}
}
@ -432,12 +451,13 @@ class BleHelper {
this.data.LinkedList.find((v) => {
if (v.deviceId == receive.deviceId) {
v.macAddress = str.replace(header,"");
console.log("收到mac地址:", str)
}
});
uni.setStorageSync(this.StorageKey, this.data
.LinkedList);
}
//////console.log("收到的字符串:", str)
} catch (ex) {
////console.log("将数据转文本失败", ex);
@ -854,7 +874,7 @@ class BleHelper {
//连接某个设备
LinkBlue(deviceId, targetServiceId, writeCharId, notifyCharId) {
this.StopSearch();
if (!writeCharId) {
writeCharId = "xxxx"; // "FFE1";
@ -890,7 +910,7 @@ class BleHelper {
console.log("正在连接" + deviceId);
uni.createBLEConnection({
deviceId: deviceId,
timeout: 5000,
timeout: 3000,
success: (info) => {
console.log("新连接成功", this.data.LinkedList);
@ -955,7 +975,7 @@ class BleHelper {
}).then((res) => {
////console.log("11111111");
if (res) { //新连接
console.log("开始获取服务")
console.log("开始获取服务",targetServiceId)
return this.getService(deviceId, targetServiceId, writeCharId,
notifyCharId); //获取服务
} else { //已连接过,直接订阅消息
@ -969,7 +989,9 @@ class BleHelper {
}).then(() => {
return resolve();
setTimeout(()=>{
resolve();
},500);
}).catch((ex) => {
////console.log("出现异常", ex);
reject(ex);
@ -1041,8 +1063,8 @@ class BleHelper {
//向蓝牙设备发送数据,如果没连接将自动连接后再发
sendData(deviceid, buffer, writeServiceId, wirteCharactId, ms) {
console.log("deviceid=" + deviceid + ",writeServiceId=" + writeServiceId + ",wirteCharactId=" +
wirteCharactId + ",timeout=" + ms)
// console.log("deviceid=" + deviceid + ",writeServiceId=" + writeServiceId + ",wirteCharactId=" +
// wirteCharactId + ",timeout=" + ms)
if (ms === undefined) {
ms = 50;
}
@ -1080,7 +1102,7 @@ class BleHelper {
})); //没有找到指定设备
return;
}
console.log("device=",device);
uni.writeBLECharacteristicValue({
deviceId: device.deviceId,
serviceId: device.writeServiceId,
@ -1114,7 +1136,7 @@ class BleHelper {
Promise.race([timeOut(ms), promise]).then(resolve).catch((ex) => {
console.log("ex=", ex);
if (ex.code == -1) {
resolve();
resolve(ex);
} else {
reject(ex);
}
@ -1129,12 +1151,19 @@ class BleHelper {
}
if (c.Linked) {
console.log("蓝牙已连接,直接发送");
// console.log("蓝牙已连接,直接发送");
return sendBuffer();
} else {
console.log("先连接蓝牙再发送");
// console.log("先连接蓝牙再发送");
return new Promise((resolve, reject) => {
this.LinkBlue(deviceid).then((res) => {
let f=this.data.LinkedList.find((v)=>{
return v.deviceId==deviceid;
});
if(!f){
reject({code:'-9',msg:'蓝牙未连接过此设备,请重新使用蓝牙添加该设备'});
retrn;
}
this.LinkBlue(f.deviceId,f.writeServiceId,f.wirteCharactId,f.notifyCharactId).then((res) => {
console.log("连接成功");
return sendBuffer();
}).then(() => {
@ -1178,7 +1207,7 @@ export default {
getBleTool: function(found, receive) {
if (!instance) {
instance = new BleHelper();
////console.log("第一次初始化");
} else {
////console.log("调用现有实例");
}

38
utils/loading.js Normal file
View File

@ -0,0 +1,38 @@
// utils/loading.js
// 显示loading
export const showLoading = (ev,options) => {
if(!ev){
return;
}
if(!options){
options={a:1};
}
ev.$refs.loading.show(options)
}
// 隐藏loading
export const hideLoading = (ev) => {
if(!ev){
return;
}
console.log("hide.....")
ev.$refs.loading.hide();
}
// 更新loading配置
export const updateLoading = (ev,options) => {
if(!ev){
return;
}
if(!options){
options={a:1};
}
ev.$refs.loading.update(options)
}