蓝牙模块添加在web平台默认成功方便调试功能,4877功能完成
This commit is contained in:
@ -33,15 +33,18 @@ class BleHelper {
|
||||
if (linkedDevices && linkedDevices.length && linkedDevices.length > 0) {
|
||||
// console.log("111111", linkedDevices);
|
||||
linkedDevices = linkedDevices.filter((v) => {
|
||||
if(v){
|
||||
if (v) {
|
||||
v.Linked = false;
|
||||
v.notifyState = false;
|
||||
}
|
||||
return v?true:false;
|
||||
}
|
||||
return v ? true : false;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const systemInfo = uni.getSystemInfoSync();
|
||||
|
||||
|
||||
|
||||
setTimeout(() => {
|
||||
this.OpenBlue().then(() => {
|
||||
@ -55,8 +58,8 @@ class BleHelper {
|
||||
discovering: false, //蓝牙是否正在搜索
|
||||
searchList: [], //已搜索到的设备列表,
|
||||
isSubscribe: false, //是否开启了订阅
|
||||
LinkedList: linkedDevices //已连接的设备列表
|
||||
|
||||
LinkedList: linkedDevices, //已连接的设备列表
|
||||
platform: systemInfo.uniPlatform
|
||||
}
|
||||
this.cfg = {
|
||||
onDeviceFound: [], //发现新设备的事件
|
||||
@ -169,7 +172,9 @@ class BleHelper {
|
||||
//连接所有已连接过的设备
|
||||
linkAllDevices() {
|
||||
// console.log("模块启动时,自动连接已连接过的设备", this.data.LinkedList);
|
||||
|
||||
if(this.data.platform=='web'){
|
||||
return;
|
||||
}
|
||||
if (this.data.LinkedList && this.data.LinkedList.length > 0) {
|
||||
let flag = false;
|
||||
for (var i = 0; i < this.data.LinkedList.length; i++) {
|
||||
@ -424,7 +429,12 @@ class BleHelper {
|
||||
|
||||
//获取蓝牙适配器状态
|
||||
CheckBlue() {
|
||||
|
||||
if (this.data.platform == 'web') {
|
||||
return Promise.resolve({
|
||||
available: true,
|
||||
discovering: false
|
||||
});
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.data.available) {
|
||||
// console.log("蓝牙模块是可用状态");
|
||||
@ -463,6 +473,12 @@ class BleHelper {
|
||||
//打开蓝牙适配器
|
||||
OpenBlue() {
|
||||
|
||||
if (this.data.platform == 'web') {
|
||||
this.data.isOpenBlue = true;
|
||||
this.data.available = true;
|
||||
this.data.isSubscribe = true;
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
var init = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.data.isOpenBlue) {
|
||||
@ -678,6 +694,7 @@ class BleHelper {
|
||||
}
|
||||
|
||||
let header = "mac address:";
|
||||
let isUpdate = false;
|
||||
if (str.indexOf(header) == 0) { //650以文本传输mac
|
||||
console.log("str=", str);
|
||||
this.data.LinkedList.find((v) => {
|
||||
@ -698,10 +715,11 @@ class BleHelper {
|
||||
.macAddress)
|
||||
}
|
||||
});
|
||||
this.updateCache();
|
||||
isUpdate = true;
|
||||
|
||||
}
|
||||
|
||||
if (str.indexOf('imei:') == 0) {
|
||||
if (str.indexOf('imei:') == 0) { //670以此方式传输imei
|
||||
let imei = str.split(':')[1];
|
||||
|
||||
|
||||
@ -709,9 +727,10 @@ class BleHelper {
|
||||
if (v.deviceId == receive
|
||||
.deviceId) {
|
||||
v.imei = imei;
|
||||
isUpdate = true;
|
||||
}
|
||||
});
|
||||
this.updateCache();
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -725,17 +744,48 @@ class BleHelper {
|
||||
if (v.deviceId == receive
|
||||
.deviceId) {
|
||||
v.macAddress = mac;
|
||||
// console.log("收到mac地址:", str)
|
||||
isUpdate = true;
|
||||
}
|
||||
});
|
||||
this.updateCache();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
let receivJson = JSON.parse(str);
|
||||
let key = "sta_address"; //HBY100以此方式上传mac地址
|
||||
if (key in receivJson) {
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == receive
|
||||
.deviceId) {
|
||||
let macStr = receivJson[
|
||||
key];
|
||||
if (macStr.includes(':')) {
|
||||
v.macAddress = macStr;
|
||||
} else {
|
||||
v.macAddress = macStr
|
||||
.replace(/(.{2})/g,
|
||||
'$1:').slice(0,
|
||||
-1)
|
||||
}
|
||||
|
||||
isUpdate = true;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
} catch (convertException) {
|
||||
console.error("文本无法转json", convertException)
|
||||
}
|
||||
|
||||
if (isUpdate) {
|
||||
this.updateCache();
|
||||
}
|
||||
// console.log("str1=", str);
|
||||
} catch (ex) {
|
||||
console.error("将数据转文本失败", ex);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
console.log("11111");
|
||||
let recData = {
|
||||
@ -746,7 +796,7 @@ class BleHelper {
|
||||
str: str,
|
||||
hexs: hexs
|
||||
};
|
||||
console.log("监听到特征值:" + JSON.stringify(recData));
|
||||
console.log("监听到特征值:" + JSON.stringify(recData));
|
||||
if (this.cfg.receivDataCallback) {
|
||||
|
||||
if (this.cfg.receivDataCallback.length > 0) {
|
||||
@ -771,10 +821,10 @@ class BleHelper {
|
||||
}
|
||||
})
|
||||
} else {
|
||||
console.log("无人订阅消息");
|
||||
console.log("无人订阅消息");
|
||||
}
|
||||
} else {
|
||||
console.log("无人订阅receivDataCallback,不处理数据");
|
||||
console.log("无人订阅receivDataCallback,不处理数据");
|
||||
}
|
||||
} catch (ex) {
|
||||
console.error("处理订阅消息失败,ex=", ex);
|
||||
@ -811,6 +861,14 @@ class BleHelper {
|
||||
}
|
||||
//关闭蓝牙适配器
|
||||
CloseBlue() {
|
||||
if (this.data.platform == 'web') {
|
||||
this.data.available = false;
|
||||
this.data.discovering = false;
|
||||
this.data.isOpenBlue = false;
|
||||
this.data.searchList = [];
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
this.data.available = false;
|
||||
this.data.discovering = false;
|
||||
@ -842,6 +900,9 @@ class BleHelper {
|
||||
|
||||
//开始搜索新设备
|
||||
StartSearch() {
|
||||
if (this.data.platform == 'web') {
|
||||
return Promise.resolve({});
|
||||
}
|
||||
this.data.searchList = [];
|
||||
var these = this;
|
||||
//开始搜索
|
||||
@ -902,6 +963,9 @@ class BleHelper {
|
||||
|
||||
//停止搜索
|
||||
StopSearch() {
|
||||
if (this.data.platform == 'web') {
|
||||
return Promise.resolve();
|
||||
}
|
||||
let p1 = new Promise((resolve, reject) => {
|
||||
uni.stopBluetoothDevicesDiscovery({
|
||||
success: (res) => {
|
||||
@ -1225,8 +1289,8 @@ class BleHelper {
|
||||
}
|
||||
setTimeout(function() {
|
||||
startgetService(id);
|
||||
|
||||
}, 100+repeatCnt*300);
|
||||
|
||||
}, 100 + repeatCnt * 300);
|
||||
}
|
||||
},
|
||||
fail: (ex) => {
|
||||
@ -1341,7 +1405,9 @@ class BleHelper {
|
||||
|
||||
//连接某个设备
|
||||
LinkBlue(deviceId, targetServiceId, writeCharId, notifyCharId, maxRetries) {
|
||||
|
||||
if(this.data.platform=='web'){
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
if (maxRetries === undefined) {
|
||||
maxRetries = 0; // 最大重试次数
|
||||
}
|
||||
@ -1414,11 +1480,11 @@ class BleHelper {
|
||||
// .LinkedList);
|
||||
|
||||
// 处理 MTU 设置
|
||||
if (plus.os.name === 'Android') {
|
||||
if (plus.os.name === 'Android') {
|
||||
this.setMtu(deviceId);
|
||||
}
|
||||
resolve(true);
|
||||
|
||||
}
|
||||
resolve(true);
|
||||
|
||||
}).catch((ex) => {
|
||||
reject(this.getError(ex));
|
||||
});
|
||||
@ -1502,9 +1568,9 @@ class BleHelper {
|
||||
|
||||
|
||||
}
|
||||
|
||||
setMtu(deviceId){
|
||||
let prom=new Promise((_succ,_err)=>{
|
||||
|
||||
setMtu(deviceId) {
|
||||
let prom = new Promise((_succ, _err) => {
|
||||
uni.setBLEMTU({
|
||||
deviceId: deviceId,
|
||||
mtu: 512,
|
||||
@ -1514,12 +1580,13 @@ class BleHelper {
|
||||
},
|
||||
fail: (ex) => {
|
||||
console.error(
|
||||
"mtu设置失败",ex);
|
||||
ex=this.getError(ex);
|
||||
"mtu设置失败", ex);
|
||||
ex = this.getError(ex);
|
||||
_err(
|
||||
ex
|
||||
); // MTU设置失败不影响连接成功
|
||||
},complete(){
|
||||
},
|
||||
complete() {
|
||||
console.log("设置mtu完毕");
|
||||
}
|
||||
});
|
||||
@ -1528,6 +1595,9 @@ class BleHelper {
|
||||
}
|
||||
//断开连接
|
||||
disconnectDevice(deviceId) {
|
||||
if(this.data.platform=='web'){
|
||||
return Promise.resolve();
|
||||
}
|
||||
var disconnect = (id) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.closeBLEConnection({
|
||||
@ -1587,11 +1657,11 @@ class BleHelper {
|
||||
}
|
||||
//向蓝牙设备发送一个字符串的ASCII码
|
||||
sendString(deviceid, str, writeServiceId, wirteCharactId, ms) {
|
||||
if(str && typeof(str)=='object'){
|
||||
str=JSON.stringify(str);
|
||||
if (str && typeof(str) == 'object') {
|
||||
str = JSON.stringify(str);
|
||||
}
|
||||
if (str) {
|
||||
|
||||
|
||||
let buffer = new ArrayBuffer(str.length);
|
||||
let dataView = new DataView(buffer);
|
||||
|
||||
@ -1631,7 +1701,9 @@ class BleHelper {
|
||||
}
|
||||
//向蓝牙设备发送数据,如果没连接将自动连接后再发
|
||||
sendData(deviceid, buffer, writeServiceId, wirteCharactId, ms) {
|
||||
|
||||
if (this.data.platform == 'web') {
|
||||
return Promise.resolve("h5平台默认成功");
|
||||
}
|
||||
// console.log("deviceid=" + deviceid + ",writeServiceId=" + writeServiceId + ",wirteCharactId=" +
|
||||
// wirteCharactId + ",timeout=" + ms)
|
||||
if (ms === undefined) {
|
||||
|
||||
Reference in New Issue
Block a user