650全局蓝牙优化
This commit is contained in:
@ -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
38
utils/loading.js
Normal 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)
|
||||
|
||||
}
|
Reference in New Issue
Block a user