添加全局蓝牙
This commit is contained in:
@ -12,18 +12,16 @@ class BleHelper {
|
||||
});
|
||||
var linkedDevices = [];
|
||||
if (f) {
|
||||
var str = uni.getStorageSync(key);
|
||||
////console.log(str);
|
||||
if (str) {
|
||||
linkedDevices = str;
|
||||
} else {
|
||||
linkedDevices = [];
|
||||
}
|
||||
linkedDevices = uni.getStorageSync(key);
|
||||
}
|
||||
linkedDevices.filter((v) => {
|
||||
v.Linked = false;
|
||||
v.notifyState = false;
|
||||
});
|
||||
if (linkedDevices && linkedDevices.length && linkedDevices.length > 0) {
|
||||
console.log("111111", linkedDevices);
|
||||
linkedDevices.filter((v) => {
|
||||
v.Linked = false;
|
||||
v.notifyState = false;
|
||||
});
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
this.linkAllDevices();
|
||||
}, 10);
|
||||
@ -349,8 +347,8 @@ class BleHelper {
|
||||
|
||||
|
||||
var bytesToHexString = (bytes) => {
|
||||
return bytes.map(byte => byte.toString(16).padStart(2, '0'))
|
||||
.join(' ');
|
||||
return bytes.map(byte => byte.toString(16).padStart(2,
|
||||
'0'));
|
||||
}
|
||||
|
||||
uni.onBluetoothAdapterStateChange((state) => {
|
||||
@ -428,7 +426,17 @@ class BleHelper {
|
||||
// 将每个字节转换为对应的字符
|
||||
str += String.fromCharCode(uint8Array[i]);
|
||||
}
|
||||
|
||||
let header="mac address:";
|
||||
if (str.indexOf(header) == 0) {
|
||||
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == receive.deviceId) {
|
||||
v.macAddress = str.replace(header,"");
|
||||
}
|
||||
});
|
||||
uni.setStorageSync(this.StorageKey, this.data
|
||||
.LinkedList);
|
||||
}
|
||||
//////console.log("收到的字符串:", str)
|
||||
|
||||
} catch (ex) {
|
||||
@ -514,6 +522,7 @@ class BleHelper {
|
||||
var these = this;
|
||||
//开始搜索
|
||||
var Search = () => {
|
||||
console.log("Search........");
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.startBluetoothDevicesDiscovery({
|
||||
services: [],
|
||||
@ -524,7 +533,7 @@ class BleHelper {
|
||||
|
||||
},
|
||||
fail: (err) => {
|
||||
//console.log(`搜索蓝牙设备失败:`, err);
|
||||
console.log(`搜索蓝牙设备失败:`, err);
|
||||
|
||||
reject(this.getError(err));
|
||||
}
|
||||
@ -537,8 +546,8 @@ class BleHelper {
|
||||
|
||||
|
||||
return this.OpenBlue().then((res) => {
|
||||
//console.log("蓝牙适配器状态", res)
|
||||
this.StopSearch();
|
||||
console.log("蓝牙适配器状态", res)
|
||||
|
||||
return Search();
|
||||
});
|
||||
|
||||
@ -598,31 +607,71 @@ class BleHelper {
|
||||
return;
|
||||
}
|
||||
}
|
||||
//console.log("开始订阅消息");
|
||||
uni.notifyBLECharacteristicValueChange({
|
||||
deviceId: deviceId,
|
||||
serviceId: c.notifyServiceid,
|
||||
characteristicId: c.notifyCharactId,
|
||||
state: state,
|
||||
success: (res) => {
|
||||
if (state) {
|
||||
console.log("订阅消息成功", res);
|
||||
} else {
|
||||
console.log("取消订阅成功", res);
|
||||
}
|
||||
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,
|
||||
serviceId: serviceId,
|
||||
characteristicId: characteristicId,
|
||||
state: state,
|
||||
success: (res) => {
|
||||
if (state) {
|
||||
console.log("订阅消息成功", res);
|
||||
} else {
|
||||
console.log("取消订阅成功", res);
|
||||
}
|
||||
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == deviceId) {
|
||||
v.notifyState = state;
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == deviceId) {
|
||||
v.notifyState = state;
|
||||
}
|
||||
});
|
||||
succ();
|
||||
},
|
||||
fail: (ex) => {
|
||||
console.log("deviceId=", id);
|
||||
console.log("serviceId=", serviceId);
|
||||
console.log("characteristicId=",
|
||||
characteristicId);
|
||||
err(this.getError(ex));
|
||||
}
|
||||
});
|
||||
resolve();
|
||||
},
|
||||
fail: (ex) => {
|
||||
});
|
||||
}
|
||||
let promies = new Array();
|
||||
for (var i = 0; i < c.Characteristics.length; i++) {
|
||||
let item = c.Characteristics[i];
|
||||
let serviceId = item.serviceId;
|
||||
let characteristicId = item.uuid;
|
||||
|
||||
reject(this.getError(ex));
|
||||
if (item.properties.notify) {
|
||||
promies.push(startSubScribe(deviceId, serviceId, characteristicId));
|
||||
}
|
||||
});
|
||||
}
|
||||
if (promies.length > 0) {
|
||||
Promise.allSettled(promies).then((results) => {
|
||||
|
||||
results.forEach((result, index) => {
|
||||
if (result.status === "fulfilled") {
|
||||
console.log(`操作${index + 1}成功:`, result.value);
|
||||
} else {
|
||||
console.log(`操作${index + 1}失败:`, result.reason
|
||||
.message);
|
||||
}
|
||||
});
|
||||
|
||||
resolve();
|
||||
}).catch((ex) => {
|
||||
reject(ex);
|
||||
});
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}, 20);
|
||||
|
||||
@ -631,17 +680,19 @@ class BleHelper {
|
||||
|
||||
//获取服务
|
||||
getService(id, targetServiceId, writeCharId, notifyCharId) {
|
||||
|
||||
if (!targetServiceId) {
|
||||
targetServiceId = "FFE0";
|
||||
targetServiceId = "xx"; // "FFE0";
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
var repeatCnt = 0;
|
||||
var startgetService = () => {
|
||||
|
||||
uni.getBLEDeviceServices({
|
||||
deviceId: id,
|
||||
success: (res) => {
|
||||
if (res.services && res.services.length > 0) {
|
||||
////console.log("获取到服务:" + JSON.stringify(res));
|
||||
console.log("获取到服务:" + JSON.stringify(res));
|
||||
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == id) {
|
||||
@ -650,27 +701,44 @@ class BleHelper {
|
||||
});
|
||||
uni.setStorageSync(this.StorageKey,
|
||||
this.data.LinkedList);
|
||||
|
||||
var promises = [];
|
||||
for (var i = 0; i < res.services.length; i++) {
|
||||
let service = res.services[i];
|
||||
if (service.uuid.indexOf(targetServiceId) > -1) {
|
||||
let se = res.services.find((v) => {
|
||||
return v.uuid.indexOf(targetServiceId) > -1;
|
||||
});
|
||||
if (se) {
|
||||
promises.push(this.getFeatrus(id, se.uuid, writeCharId,
|
||||
notifyCharId));
|
||||
} else {
|
||||
for (var i = 0; i < res.services.length; i++) {
|
||||
let service = res.services[i];
|
||||
promises.push(this.getFeatrus(id, service.uuid,
|
||||
writeCharId, notifyCharId));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (promises.length == 0) {
|
||||
console.log("未找到主服务");
|
||||
reject({
|
||||
msg: "未找到主服务",
|
||||
code: -1
|
||||
});
|
||||
} else {
|
||||
|
||||
Promise.all(promises)
|
||||
.then(results => {
|
||||
////console.log('所有操作成功完成', results);
|
||||
|
||||
return this.subScribe(id, true);
|
||||
})
|
||||
.then((res) => {
|
||||
console.log('所有操作成功完成', res);
|
||||
console.log("LinkedList=", this.data
|
||||
.LinkedList);
|
||||
resolve();
|
||||
})
|
||||
.catch(error => {
|
||||
console.log("errrr", error);
|
||||
reject(error);
|
||||
});
|
||||
}
|
||||
@ -712,25 +780,50 @@ class BleHelper {
|
||||
deviceId: id,
|
||||
serviceId: serviceId,
|
||||
success: (res) => {
|
||||
////console.log("获取到特征:" + JSON.stringify(res));
|
||||
|
||||
console.log("获取到特征:" + JSON.stringify(res));
|
||||
res.characteristics.forEach((v) => {
|
||||
v.serviceId = serviceId;
|
||||
});
|
||||
//写特征
|
||||
let writeChar = res.characteristics.find(char =>
|
||||
char.uuid.indexOf(writeCharId) > -1
|
||||
);
|
||||
let writeChar = res.characteristics.find(char => {
|
||||
return char.uuid.indexOf(writeCharId) > -1
|
||||
});
|
||||
if (!writeChar) {
|
||||
writeChar = res.characteristics.find(char => {
|
||||
return char.properties.write;
|
||||
});
|
||||
}
|
||||
//通知特征
|
||||
let notiChar = res.characteristics.find(char =>
|
||||
char.uuid.indexOf(notifyCharId) > -1
|
||||
);
|
||||
|
||||
let notiChar = res.characteristics.find(char => {
|
||||
return char.uuid.indexOf(notifyCharId) > -1;
|
||||
});
|
||||
if (!notiChar) {
|
||||
notiChar = res.characteristics.find(char => {
|
||||
return char.properties.notify;
|
||||
});
|
||||
}
|
||||
this.data.LinkedList.find(function(v) {
|
||||
if (v.deviceId == id) {
|
||||
if (!v.Characteristics) {
|
||||
v.Characteristics = [];
|
||||
}
|
||||
v.Characteristics = v.Characteristics.concat(res
|
||||
.characteristics);
|
||||
|
||||
for (var i = 0; i < res.characteristics.length; i++) {
|
||||
let item = res.characteristics[i];
|
||||
let flag = false;
|
||||
for (var j = 0; j < v.Characteristics.length; j++) {
|
||||
let charact = v.Characteristics[j];
|
||||
if (charact.uuid == item.uuid) {
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!flag) {
|
||||
v.Characteristics.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (writeChar) {
|
||||
v.writeServiceId = serviceId;
|
||||
v.wirteCharactId = writeChar.uuid;
|
||||
@ -740,10 +833,12 @@ class BleHelper {
|
||||
v.notifyServiceid = serviceId;
|
||||
v.notifyCharactId = notiChar.uuid;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
this.subScribe(id, true);
|
||||
|
||||
|
||||
uni.setStorageSync(this.StorageKey, this.data.LinkedList);
|
||||
resolve(res);
|
||||
@ -762,10 +857,10 @@ class BleHelper {
|
||||
this.StopSearch();
|
||||
|
||||
if (!writeCharId) {
|
||||
writeCharId = "FFE1";
|
||||
writeCharId = "xxxx"; // "FFE1";
|
||||
}
|
||||
if (!notifyCharId) {
|
||||
notifyCharId = "FFE2";
|
||||
notifyCharId = "xxxxx"; // "FFE2";
|
||||
}
|
||||
|
||||
|
||||
@ -788,7 +883,7 @@ class BleHelper {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
if (fIndex > -1 && f?.Linked) {
|
||||
////console.log("当前已连接,跳过其他步骤")
|
||||
console.log("当前已连接,跳过其他步骤")
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
@ -798,7 +893,7 @@ class BleHelper {
|
||||
timeout: 5000,
|
||||
success: (info) => {
|
||||
|
||||
console.log("新连接成功");
|
||||
console.log("新连接成功", this.data.LinkedList);
|
||||
this.getLinkBlue().then((arr) => {
|
||||
let cr = arr.devices.find(c => {
|
||||
if (c.deviceId == deviceId) {
|
||||
@ -860,7 +955,7 @@ class BleHelper {
|
||||
}).then((res) => {
|
||||
////console.log("11111111");
|
||||
if (res) { //新连接
|
||||
////console.log("开始获取服务")
|
||||
console.log("开始获取服务")
|
||||
return this.getService(deviceId, targetServiceId, writeCharId,
|
||||
notifyCharId); //获取服务
|
||||
} else { //已连接过,直接订阅消息
|
||||
@ -1058,20 +1153,20 @@ class BleHelper {
|
||||
//将点阵数据转换成RGB565
|
||||
convertToRGB565(pixels, type) {
|
||||
const result = new Uint16Array(pixels.length / 4);
|
||||
let index = 0;
|
||||
let index = 0;
|
||||
for (let i = 0; i < pixels.length; i += 4) {
|
||||
let r = pixels[i];
|
||||
let g = pixels[i + 1];
|
||||
let b = pixels[i + 2];
|
||||
|
||||
|
||||
let rgb565 = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
|
||||
let bgr565 = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
|
||||
if(type=='bgr'){
|
||||
if (type == 'bgr') {
|
||||
result[index++] = bgr565;
|
||||
}else{
|
||||
} else {
|
||||
result[index++] = rgb565;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -1,4 +1,4 @@
|
||||
const BASE_URL = 'http://192.168.110.54:8000';
|
||||
const BASE_URL = 'http://192.168.110.169:8000';
|
||||
const request = (options) => {
|
||||
console.log("options"+JSON.stringify(options))
|
||||
return new Promise((resolve, reject) => {
|
||||
|
Reference in New Issue
Block a user