6155完成部分协议对接
This commit is contained in:
@ -123,18 +123,18 @@ class BleHelper {
|
||||
}
|
||||
|
||||
const currentPage = pages[pages.length - 1];
|
||||
console.log("currentPage=",currentPage.route);
|
||||
console.log("currentPage=", currentPage.route);
|
||||
return currentPage.route;
|
||||
}
|
||||
|
||||
//设置发现新设备的回调
|
||||
addDeviceFound(callback) {
|
||||
let key = this.getCurrentPagePath();
|
||||
if(!key){
|
||||
key=new Date().getTime();
|
||||
if (!key) {
|
||||
key = new Date().getTime();
|
||||
}
|
||||
if (key) {
|
||||
console.log("key="+key);
|
||||
console.log("key=" + key);
|
||||
let f = this.cfg.onDeviceFound.findIndex((v) => {
|
||||
return v.key == key;
|
||||
});
|
||||
@ -152,28 +152,28 @@ class BleHelper {
|
||||
//移除发现新设备的回调
|
||||
removeDeviceFound() {
|
||||
let key = this.getCurrentPagePath();
|
||||
|
||||
|
||||
if (key) {
|
||||
console.log("key="+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);
|
||||
} 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) {
|
||||
key = new Date().getTime();
|
||||
}
|
||||
if (key) {
|
||||
let f = this.cfg.receivDataCallback.findIndex((v) => {
|
||||
@ -196,7 +196,7 @@ class BleHelper {
|
||||
removeReceiveCallback(ev) {
|
||||
let key = this.getCurrentPagePath();
|
||||
if (key) {
|
||||
console.log("key="+key);
|
||||
console.log("key=" + key);
|
||||
let f = this.cfg.receivDataCallback.findIndex((v) => {
|
||||
return v.key == key;
|
||||
});
|
||||
@ -205,10 +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);
|
||||
}
|
||||
} else {
|
||||
if (this.cfg.receivDataCallback.length > 0) {
|
||||
this.cfg.receivDataCallback.splice(this.cfg.receivDataCallback.length - 1, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -445,20 +445,35 @@ class BleHelper {
|
||||
// 将每个字节转换为对应的字符
|
||||
str += String.fromCharCode(uint8Array[i]);
|
||||
}
|
||||
let header="mac address:";
|
||||
if (str.indexOf(header) == 0) {
|
||||
|
||||
let header = "mac address:";
|
||||
if (str.indexOf(header) == 0) { //650以文本传输mac
|
||||
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == receive.deviceId) {
|
||||
v.macAddress = str.replace(header,"");
|
||||
if (v.deviceId == receive
|
||||
.deviceId) {
|
||||
v.macAddress = str.replace(
|
||||
header, "");
|
||||
console.log("收到mac地址:", str)
|
||||
}
|
||||
});
|
||||
uni.setStorageSync(this.StorageKey, this.data
|
||||
.LinkedList);
|
||||
}
|
||||
|
||||
if (bytes[0] == 0xFC) { //6155以0xFC开头代表mac地址
|
||||
if (arr.length >= 7) {
|
||||
let mac = arr.slice(1, 7).join(":");
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == receive
|
||||
.deviceId) {
|
||||
v.macAddress = mac;
|
||||
console.log("收到mac地址:", str)
|
||||
}
|
||||
});
|
||||
uni.setStorageSync(this.StorageKey, this
|
||||
.data.LinkedList);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} catch (ex) {
|
||||
////console.log("将数据转文本失败", ex);
|
||||
}
|
||||
@ -546,7 +561,7 @@ class BleHelper {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.startBluetoothDevicesDiscovery({
|
||||
services: [],
|
||||
allowDuplicatesKey: false,
|
||||
allowDuplicatesKey: true,
|
||||
success: (res) => {
|
||||
//console.log('开始搜索蓝牙设备成功');
|
||||
resolve(res);
|
||||
@ -629,8 +644,7 @@ class BleHelper {
|
||||
}
|
||||
console.log("c=", c);
|
||||
let startSubScribe = (id, serviceId, characteristicId) => {
|
||||
console.log("id, serviceId, characteristicId=" + id + "," + serviceId +
|
||||
"," + characteristicId);
|
||||
|
||||
return new Promise((succ, err) => {
|
||||
uni.notifyBLECharacteristicValueChange({
|
||||
deviceId: id,
|
||||
@ -652,10 +666,7 @@ class BleHelper {
|
||||
succ();
|
||||
},
|
||||
fail: (ex) => {
|
||||
console.log("deviceId=", id);
|
||||
console.log("serviceId=", serviceId);
|
||||
console.log("characteristicId=",
|
||||
characteristicId);
|
||||
|
||||
err(this.getError(ex));
|
||||
}
|
||||
});
|
||||
@ -676,10 +687,10 @@ class BleHelper {
|
||||
|
||||
results.forEach((result, index) => {
|
||||
if (result.status === "fulfilled") {
|
||||
console.log(`操作${index + 1}成功:`, result.value);
|
||||
//console.log(`操作${index + 1}成功:`, result.value);
|
||||
} else {
|
||||
console.log(`操作${index + 1}失败:`, result.reason
|
||||
.message);
|
||||
// console.log(`操作${index + 1}失败:`, result.reason
|
||||
// .message);
|
||||
}
|
||||
});
|
||||
|
||||
@ -752,7 +763,7 @@ class BleHelper {
|
||||
return this.subScribe(id, true);
|
||||
})
|
||||
.then((res) => {
|
||||
console.log('所有操作成功完成', res);
|
||||
console.log('设备连接成功,初始化完成', res);
|
||||
console.log("LinkedList=", this.data
|
||||
.LinkedList);
|
||||
resolve();
|
||||
@ -843,7 +854,7 @@ class BleHelper {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (writeChar) {
|
||||
v.writeServiceId = serviceId;
|
||||
v.wirteCharactId = writeChar.uuid;
|
||||
@ -874,7 +885,7 @@ class BleHelper {
|
||||
|
||||
//连接某个设备
|
||||
LinkBlue(deviceId, targetServiceId, writeCharId, notifyCharId) {
|
||||
|
||||
|
||||
|
||||
if (!writeCharId) {
|
||||
writeCharId = "xxxx"; // "FFE1";
|
||||
@ -975,7 +986,7 @@ class BleHelper {
|
||||
}).then((res) => {
|
||||
////console.log("11111111");
|
||||
if (res) { //新连接
|
||||
console.log("开始获取服务",targetServiceId)
|
||||
console.log("开始获取服务", targetServiceId)
|
||||
return this.getService(deviceId, targetServiceId, writeCharId,
|
||||
notifyCharId); //获取服务
|
||||
} else { //已连接过,直接订阅消息
|
||||
@ -989,9 +1000,9 @@ class BleHelper {
|
||||
}).then(() => {
|
||||
|
||||
|
||||
setTimeout(()=>{
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
},500);
|
||||
}, 500);
|
||||
}).catch((ex) => {
|
||||
////console.log("出现异常", ex);
|
||||
reject(ex);
|
||||
@ -1102,7 +1113,7 @@ class BleHelper {
|
||||
})); //没有找到指定设备
|
||||
return;
|
||||
}
|
||||
console.log("device=",device);
|
||||
console.log("device=", device);
|
||||
uni.writeBLECharacteristicValue({
|
||||
deviceId: device.deviceId,
|
||||
serviceId: device.writeServiceId,
|
||||
@ -1156,14 +1167,18 @@ class BleHelper {
|
||||
} else {
|
||||
// console.log("先连接蓝牙再发送");
|
||||
return new Promise((resolve, reject) => {
|
||||
let f=this.data.LinkedList.find((v)=>{
|
||||
return v.deviceId==deviceid;
|
||||
let f = this.data.LinkedList.find((v) => {
|
||||
return v.deviceId == deviceid;
|
||||
});
|
||||
if(!f){
|
||||
reject({code:'-9',msg:'蓝牙未连接过此设备,请重新使用蓝牙添加该设备'});
|
||||
if (!f) {
|
||||
reject({
|
||||
code: '-9',
|
||||
msg: '蓝牙未连接过此设备,请重新使用蓝牙添加该设备'
|
||||
});
|
||||
retrn;
|
||||
}
|
||||
this.LinkBlue(f.deviceId,f.writeServiceId,f.wirteCharactId,f.notifyCharactId).then((res) => {
|
||||
this.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then((
|
||||
res) => {
|
||||
console.log("连接成功");
|
||||
return sendBuffer();
|
||||
}).then(() => {
|
||||
@ -1207,7 +1222,7 @@ export default {
|
||||
getBleTool: function(found, receive) {
|
||||
if (!instance) {
|
||||
instance = new BleHelper();
|
||||
|
||||
|
||||
} else {
|
||||
////console.log("调用现有实例");
|
||||
}
|
||||
|
Reference in New Issue
Block a user