630 lines
20 KiB
JavaScript
630 lines
20 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("../../common/vendor.js");
|
|
const _sfc_main = {
|
|
data() {
|
|
return {
|
|
config: {
|
|
color: "#333",
|
|
backgroundColor: [1, "#fff"],
|
|
title: "多设备蓝牙连接",
|
|
back: false
|
|
},
|
|
title: "Hello",
|
|
bleDevs: [],
|
|
status: -2,
|
|
//-2未连接 -1已连接 0连接成功
|
|
deviceId: "",
|
|
serviceId: "",
|
|
characteristicId: "",
|
|
sendData: "",
|
|
getData: [],
|
|
deviceIds: [],
|
|
totalList: [],
|
|
// 全部已连接的设备
|
|
timeIndex: 0,
|
|
// 默认是列表的第一个
|
|
timeout: null,
|
|
shows: false,
|
|
testItems: [
|
|
{
|
|
index: 1,
|
|
typeNums: 1,
|
|
min: 0,
|
|
max: 150,
|
|
name: "设定频率",
|
|
value: "F"
|
|
},
|
|
{
|
|
index: 2,
|
|
typeNums: 250,
|
|
min: 50,
|
|
max: 250,
|
|
name: "设定脉宽",
|
|
value: "W"
|
|
},
|
|
{ index: 3, typeNums: 3, min: 0, max: 3, name: "设定类型", value: "C" },
|
|
{
|
|
index: 4,
|
|
typeNums: 0,
|
|
min: 0,
|
|
max: 120,
|
|
name: "设定电流",
|
|
value: "I"
|
|
},
|
|
{
|
|
index: 5,
|
|
typeNums: 0,
|
|
min: 1,
|
|
max: 100,
|
|
name: "设定方案",
|
|
value: "M"
|
|
}
|
|
],
|
|
titleTime: "00:00:00",
|
|
timer: "",
|
|
hour: 0,
|
|
minutes: 0,
|
|
seconds: 0,
|
|
input1: "B",
|
|
input2: ""
|
|
};
|
|
},
|
|
destroyed() {
|
|
clearInterval(this.timer);
|
|
},
|
|
onLoad() {
|
|
},
|
|
mounted() {
|
|
this.onBLEConnectionStateChange();
|
|
},
|
|
methods: {
|
|
// 开始计时
|
|
begin() {
|
|
if (this.start) {
|
|
return;
|
|
}
|
|
this.sendData = "BS1\r";
|
|
this.start = true;
|
|
this.timer = setInterval(this.startTimer, 1e3);
|
|
this.send();
|
|
},
|
|
startTimer() {
|
|
this.seconds += 1;
|
|
if (this.seconds >= 60) {
|
|
this.seconds = 0;
|
|
this.minute = this.minute + 1;
|
|
}
|
|
if (this.minute >= 60) {
|
|
this.minute = 0;
|
|
this.hour = this.hour + 1;
|
|
}
|
|
this.titleTime = (this.hour < 10 ? "0" + this.hour : this.hour) + ":" + (this.minutes < 10 ? "0" + this.minutes : this.minutes) + ":" + (this.seconds < 10 ? "0" + this.seconds : this.seconds);
|
|
},
|
|
// 暂停倒计时
|
|
pause() {
|
|
if (this.timer) {
|
|
clearInterval(this.timer);
|
|
this.start = false;
|
|
this.sendData = "BS2\r";
|
|
this.send();
|
|
}
|
|
},
|
|
stop() {
|
|
if (this.timer) {
|
|
clearInterval(this.timer);
|
|
this.sendData = "BS3\r";
|
|
this.send();
|
|
this.titleTime = "00:00:00";
|
|
this.timer = "";
|
|
this.hour = 0;
|
|
this.minutes = 0;
|
|
this.seconds = 0;
|
|
this.start = false;
|
|
}
|
|
},
|
|
changNums(index, item) {
|
|
if (index == 1) {
|
|
if (item.typeNums <= item.min) {
|
|
common_vendor.index.showToast({
|
|
title: "已经不能再减少了",
|
|
icon: "none"
|
|
});
|
|
return;
|
|
}
|
|
item.typeNums--;
|
|
} else if (index == 2) {
|
|
if (item.typeNums >= item.max) {
|
|
common_vendor.index.showToast({
|
|
title: "已经不能再增加了",
|
|
icon: "none"
|
|
});
|
|
return;
|
|
}
|
|
item.typeNums++;
|
|
}
|
|
this.changeBar(item);
|
|
},
|
|
changeBar(item) {
|
|
if (this.timeout) {
|
|
clearTimeout(this.timeout);
|
|
}
|
|
this.timeout = setTimeout(() => {
|
|
if (item.typeNums < item.min) {
|
|
common_vendor.index.showToast({
|
|
title: "低于最小值,已变更为最小值发送",
|
|
icon: "none"
|
|
});
|
|
item.typeNums = item.min;
|
|
} else if (item.typeNums > item.max) {
|
|
common_vendor.index.showToast({
|
|
title: "超过最大值,已变更为最大值发送",
|
|
icon: "none"
|
|
});
|
|
item.typeNums = item.max;
|
|
}
|
|
this.sendData = "B" + item.value + item.typeNums + "\r";
|
|
for (let i = 0; i < this.deviceIds.length; i++) {
|
|
this.getBLEDeviceServices(1, this.deviceIds[i]);
|
|
}
|
|
}, 500);
|
|
},
|
|
checkboxChange(e) {
|
|
if (e.target.value[0] && e.target.dataset.name) {
|
|
let item = {
|
|
deviceId: e.target.value[0],
|
|
name: e.target.dataset.name
|
|
};
|
|
this.deviceIds.push(item);
|
|
} else {
|
|
for (let index = 0; index < this.deviceIds.length; index++) {
|
|
let item = this.deviceIds[index];
|
|
if (item.deviceId == e.target.dataset.deviceid) {
|
|
this.deviceIds.splice(index, 1);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
hextoString(hex) {
|
|
var arr = hex.split("");
|
|
var out = "";
|
|
for (var i = 0; i < arr.length / 2; i++) {
|
|
var tmp = "0x" + arr[i * 2] + arr[i * 2 + 1];
|
|
var charValue = String.fromCharCode(tmp);
|
|
out += charValue;
|
|
}
|
|
return out;
|
|
},
|
|
send(index) {
|
|
let that = this;
|
|
if (index == 1) {
|
|
that.sendData = that.input1 + that.input2 + "\r";
|
|
}
|
|
if (!that.sendData) {
|
|
return common_vendor.index.showToast({
|
|
title: "发送数据不可为空",
|
|
icon: "none"
|
|
});
|
|
}
|
|
common_vendor.index.showLoading({
|
|
title: "发送中,请稍等",
|
|
mask: true
|
|
});
|
|
for (let i = 0; i < that.deviceIds.length; i++) {
|
|
that.getBLEDeviceServices(1, that.deviceIds[i]);
|
|
}
|
|
},
|
|
// ArrayBuffer转16进度字符串示例
|
|
ab2hex(buffer) {
|
|
const hexArr = Array.prototype.map.call(
|
|
new Uint8Array(buffer),
|
|
function(bit) {
|
|
return ("00" + bit.toString(16)).slice(-2);
|
|
}
|
|
);
|
|
return hexArr.join("");
|
|
},
|
|
onBLEConnectionStateChange() {
|
|
common_vendor.index.onBLEConnectionStateChange((res) => {
|
|
if (res.connected == false) {
|
|
common_vendor.index.hideLoading();
|
|
for (let i = 0; i < this.deviceIds.length; i++) {
|
|
if (res.deviceId == this.deviceIds[i].deviceId) {
|
|
common_vendor.index.showToast({
|
|
title: this.deviceIds[i].name + " 蓝牙设备断开连接",
|
|
icon: "none"
|
|
});
|
|
}
|
|
}
|
|
}
|
|
});
|
|
},
|
|
//初始化蓝牙
|
|
initBle() {
|
|
this.bleDevs = [];
|
|
this.deviceIds = [];
|
|
common_vendor.index.openBluetoothAdapter({
|
|
success: (res) => {
|
|
common_vendor.index.getBluetoothAdapterState({
|
|
//蓝牙的匹配状态
|
|
success: (res1) => {
|
|
this.startBluetoothDeviceDiscovery();
|
|
},
|
|
fail(error) {
|
|
common_vendor.index.showToast({ icon: "none", title: "查看手机蓝牙是否打开" });
|
|
}
|
|
});
|
|
},
|
|
fail: (err) => {
|
|
common_vendor.index.showToast({ icon: "none", title: "查看手机蓝牙是否打开" });
|
|
}
|
|
});
|
|
},
|
|
// 开始搜索蓝牙设备
|
|
startBluetoothDeviceDiscovery() {
|
|
common_vendor.index.startBluetoothDevicesDiscovery({
|
|
success: (res) => {
|
|
this.onBluetoothDeviceFound();
|
|
},
|
|
fail: (err) => {
|
|
}
|
|
});
|
|
},
|
|
// 发现外围设备
|
|
onBluetoothDeviceFound() {
|
|
common_vendor.index.onBluetoothDeviceFound((res) => {
|
|
if (this.bleDevs.indexOf(res.devices[0]) == -1) {
|
|
this.bleDevs.push(res.devices[0]);
|
|
}
|
|
});
|
|
},
|
|
// 多选然后连接
|
|
connectBle() {
|
|
if (this.deviceIds.length == 0) {
|
|
common_vendor.index.showToast({ title: "请选择连接的设备", icon: "none" });
|
|
return;
|
|
}
|
|
this.getData = [];
|
|
this.deviceIds.forEach((item) => {
|
|
this.nowLinkLis(item);
|
|
});
|
|
},
|
|
//选择设备连接吧deviceId传进来
|
|
createBLEConnection(item) {
|
|
common_vendor.index.showLoading({
|
|
title: "连接中,请稍等",
|
|
mask: true
|
|
});
|
|
let that = this;
|
|
common_vendor.index.createBLEConnection({
|
|
deviceId: item.deviceId,
|
|
success(res) {
|
|
that.shows = true;
|
|
that.stopBluetoothDevicesDiscovery();
|
|
that.getBLEDeviceServices(2, item);
|
|
},
|
|
fail(res) {
|
|
console.log("蓝牙连接失败", res);
|
|
common_vendor.index.showToast({
|
|
title: items.name + "蓝牙连接失败",
|
|
icon: "none"
|
|
});
|
|
}
|
|
});
|
|
},
|
|
// 停止搜寻蓝牙设备
|
|
stopBluetoothDevicesDiscovery() {
|
|
common_vendor.index.stopBluetoothDevicesDiscovery({
|
|
success: (e) => {
|
|
this.loading = false;
|
|
},
|
|
fail: (e) => {
|
|
console.log("停止搜索蓝牙设备失败,错误码:" + e.errCode);
|
|
}
|
|
});
|
|
},
|
|
//获取蓝牙的所有服务
|
|
getBLEDeviceServices(index, items2) {
|
|
setTimeout(() => {
|
|
common_vendor.index.getBLEDeviceServices({
|
|
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
|
|
deviceId: items2.deviceId,
|
|
success: (res) => {
|
|
console.log("services", res.services);
|
|
res.services.forEach((item) => {
|
|
if (item.uuid.indexOf("0000FFE0-0000-1000-8000-00805F9B34FB") != -1) {
|
|
items2["serviceId"] = item.uuid;
|
|
this.getBLEDeviceCharacteristics(index, items2);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}, 1e3);
|
|
},
|
|
//获取蓝牙特征
|
|
getBLEDeviceCharacteristics(index, items2) {
|
|
setTimeout(() => {
|
|
common_vendor.index.getBLEDeviceCharacteristics({
|
|
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
|
|
deviceId: items2.deviceId,
|
|
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
|
|
serviceId: items2.serviceId,
|
|
success: (res) => {
|
|
console.log("characteristics", res);
|
|
res.characteristics.forEach((item) => {
|
|
if (
|
|
// 2 支持监听 1 支持写入
|
|
item.uuid.indexOf(
|
|
index == 1 ? "0000FFE1-0000-1000-8000-00805F9B34FB" : "0000FFE2-0000-1000-8000-00805F9B34FB"
|
|
) != -1
|
|
) {
|
|
items2["characteristicId"] = item.uuid;
|
|
if (index == 2) {
|
|
this.notifyBLECharacteristicValueChange(items2);
|
|
}
|
|
}
|
|
});
|
|
if (index == 1) {
|
|
this.writeString(this.sendData, items2);
|
|
}
|
|
},
|
|
fail: (res) => {
|
|
console.log(res);
|
|
}
|
|
});
|
|
}, 0);
|
|
},
|
|
// 启用 notify 功能
|
|
notifyBLECharacteristicValueChange(items2) {
|
|
let that = this;
|
|
common_vendor.index.notifyBLECharacteristicValueChange({
|
|
state: true,
|
|
// 启用 notify 功能
|
|
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
|
|
deviceId: items2.deviceId,
|
|
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
|
|
serviceId: items2.serviceId,
|
|
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
|
|
characteristicId: items2.characteristicId,
|
|
success: (res) => {
|
|
console.log("启用 notify 功能成功", res);
|
|
common_vendor.index.hideLoading();
|
|
items2["status"] = true;
|
|
items2["text"] = "";
|
|
that.totalList.push(items2);
|
|
common_vendor.index.onBLECharacteristicValueChange((res2) => {
|
|
for (let i = 0; i < that.deviceIds.length; i++) {
|
|
if (res2.deviceId == that.deviceIds[i].deviceId) {
|
|
let item = {
|
|
name: that.deviceIds[i].name,
|
|
txt: "接收到:" + that.hextoString(that.ab2hex(res2.value))
|
|
};
|
|
that.getData.unshift(item);
|
|
}
|
|
}
|
|
for (let i = 0; i < that.totalList.length; i++) {
|
|
if (res2.deviceId == that.totalList[i].deviceId) {
|
|
that.totalList[i].text = that.hextoString(
|
|
that.ab2hex(res2.value)
|
|
);
|
|
}
|
|
}
|
|
that.totalList = JSON.stringify(that.totalList);
|
|
that.totalList = JSON.parse(that.totalList);
|
|
});
|
|
},
|
|
fail: (res) => {
|
|
console.log("启用 notify 功能失败", res);
|
|
}
|
|
});
|
|
},
|
|
close() {
|
|
let that = this;
|
|
common_vendor.index.showModal({
|
|
title: "提示",
|
|
content: "将断开全部蓝牙连接",
|
|
success: function(res) {
|
|
if (res.confirm) {
|
|
for (let index = 0; index < that.deviceIds.length; index++) {
|
|
let item = that.deviceIds[index];
|
|
common_vendor.index.closeBLEConnection({
|
|
deviceId: item.deviceId,
|
|
success(res2) {
|
|
console.log("断开蓝牙成功", res2);
|
|
that.shows = false;
|
|
that.totalList = [];
|
|
common_vendor.index.showToast({
|
|
title: "断开蓝牙成功"
|
|
});
|
|
},
|
|
fail(res2) {
|
|
console.log("断开蓝牙失败", res2);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
});
|
|
},
|
|
// 向蓝牙设备发送字符串数据 writeBLECharacteristicValueString
|
|
writeString(str, items2) {
|
|
let that = this;
|
|
let buffer = new ArrayBuffer(str.length);
|
|
let dataView = new DataView(buffer);
|
|
for (let i in str) {
|
|
dataView.setUint8(i, str[i].charCodeAt() | 0);
|
|
}
|
|
setTimeout(() => {
|
|
common_vendor.index.writeBLECharacteristicValue({
|
|
deviceId: items2.deviceId,
|
|
serviceId: items2.serviceId,
|
|
characteristicId: items2.characteristicId,
|
|
value: buffer,
|
|
writeType: "write",
|
|
success: function(res) {
|
|
common_vendor.index.hideLoading();
|
|
let item = {
|
|
name: items2.name,
|
|
txt: "已发送:" + str
|
|
};
|
|
that.getData.unshift(item);
|
|
},
|
|
fail: function(res) {
|
|
common_vendor.index.hideLoading();
|
|
common_vendor.index.showToast({
|
|
title: "发送失败,可能蓝牙目前不支持写入",
|
|
icon: "none"
|
|
});
|
|
}
|
|
});
|
|
}, 0);
|
|
},
|
|
// 直接启用监听功能
|
|
nowLinkLis(items2) {
|
|
let that = this;
|
|
console.log("items", items2);
|
|
common_vendor.index.showLoading({
|
|
title: "连接中,请稍等",
|
|
mask: true
|
|
});
|
|
common_vendor.index.createBLEConnection({
|
|
deviceId: items2.deviceId,
|
|
success(res) {
|
|
that.stopBluetoothDevicesDiscovery();
|
|
setTimeout(() => {
|
|
common_vendor.index.notifyBLECharacteristicValueChange({
|
|
state: true,
|
|
// 启用 notify 功能
|
|
deviceId: items2.deviceId,
|
|
serviceId: "0000FFE0-0000-1000-8000-00805F9B34FB",
|
|
characteristicId: "0000FFE2-0000-1000-8000-00805F9B34FB",
|
|
success: (res2) => {
|
|
console.log("启用监听了", res2);
|
|
that.shows = true;
|
|
common_vendor.index.hideLoading();
|
|
items2["status"] = true;
|
|
items2["text"] = "";
|
|
that.totalList.push(items2);
|
|
common_vendor.index.onBLECharacteristicValueChange((res3) => {
|
|
for (let i = 0; i < that.deviceIds.length; i++) {
|
|
if (res3.deviceId == that.deviceIds[i].deviceId) {
|
|
let item = {
|
|
name: that.deviceIds[i].name,
|
|
txt: "接收到:" + that.hextoString(that.ab2hex(res3.value))
|
|
};
|
|
that.getData.unshift(item);
|
|
}
|
|
}
|
|
for (let i = 0; i < that.totalList.length; i++) {
|
|
if (res3.deviceId == that.totalList[i].deviceId) {
|
|
that.totalList[i].text = that.hextoString(
|
|
that.ab2hex(res3.value)
|
|
);
|
|
}
|
|
}
|
|
that.totalList = JSON.stringify(that.totalList);
|
|
that.totalList = JSON.parse(that.totalList);
|
|
});
|
|
},
|
|
fail: (res2) => {
|
|
console.log("启用 notify 功能失败", res2);
|
|
common_vendor.index.hideLoading();
|
|
common_vendor.index.showToast({ title: "连接失败", icon: "none" });
|
|
}
|
|
});
|
|
}, 800);
|
|
},
|
|
fail(res) {
|
|
console.log("蓝牙连接失败", res);
|
|
common_vendor.index.showToast({
|
|
title: items2.name + "连接失败",
|
|
icon: "none"
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|
|
};
|
|
if (!Array) {
|
|
const _component_viwe = common_vendor.resolveComponent("viwe");
|
|
_component_viwe();
|
|
}
|
|
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
|
|
return common_vendor.e({
|
|
a: !$data.shows,
|
|
b: common_vendor.o((...args) => $options.initBle && $options.initBle(...args)),
|
|
c: Math.max(100 + _ctx.item.RSSI, 0) >= 30
|
|
}, Math.max(100 + _ctx.item.RSSI, 0) >= 30 ? {
|
|
d: common_vendor.f($data.bleDevs, (item, index, i0) => {
|
|
return {
|
|
a: common_vendor.t(item.name),
|
|
b: item.deviceId,
|
|
c: item.name,
|
|
d: item.deviceId,
|
|
e: common_vendor.t(item.deviceId),
|
|
f: common_vendor.t(item.RSSI),
|
|
g: common_vendor.t(Math.max(100 + item.RSSI, 0)),
|
|
h: index,
|
|
i: item.name.length > 0 && !$data.shows
|
|
};
|
|
}),
|
|
e: common_vendor.o((...args) => $options.checkboxChange && $options.checkboxChange(...args))
|
|
} : {}, {
|
|
f: !$data.shows
|
|
}, !$data.shows ? {
|
|
g: common_vendor.o((...args) => $options.connectBle && $options.connectBle(...args))
|
|
} : {}, {
|
|
h: $data.shows
|
|
}, $data.shows ? {
|
|
i: common_vendor.o((...args) => $options.close && $options.close(...args))
|
|
} : {}, {
|
|
j: $data.shows
|
|
}, $data.shows ? {
|
|
k: common_vendor.f($data.testItems, (item, index, i0) => {
|
|
return {
|
|
a: common_vendor.t(item.name),
|
|
b: common_vendor.o(($event) => $options.changNums(1, item)),
|
|
c: common_vendor.o([($event) => item.typeNums = $event.detail.value, ($event) => $options.changeBar(item)]),
|
|
d: item.typeNums,
|
|
e: common_vendor.o(($event) => $options.changNums(2, item)),
|
|
f: index
|
|
};
|
|
})
|
|
} : {}, {
|
|
l: $data.shows
|
|
}, $data.shows ? {
|
|
m: common_vendor.t($data.titleTime),
|
|
n: common_vendor.o((...args) => $options.begin && $options.begin(...args)),
|
|
o: common_vendor.o((...args) => $options.pause && $options.pause(...args)),
|
|
p: common_vendor.o((...args) => $options.stop && $options.stop(...args))
|
|
} : {}, {
|
|
q: $data.shows
|
|
}, $data.shows ? {
|
|
r: $data.input1,
|
|
s: common_vendor.o(($event) => $data.input1 = $event.detail.value),
|
|
t: $data.input2,
|
|
v: common_vendor.o(($event) => $data.input2 = $event.detail.value),
|
|
w: common_vendor.o(($event) => $options.send(1))
|
|
} : {}, {
|
|
x: common_vendor.f($data.totalList, (item, index, i0) => {
|
|
return {
|
|
a: common_vendor.t(item.text),
|
|
b: common_vendor.t(item.name),
|
|
c: common_vendor.n(item.status ? "item bakBlue" : "item"),
|
|
d: index,
|
|
e: "cb7d404f-0-" + i0
|
|
};
|
|
}),
|
|
y: $data.shows
|
|
}, $data.shows ? {
|
|
z: common_vendor.f($data.getData, (item, index, i0) => {
|
|
return {
|
|
a: common_vendor.t(item.name),
|
|
b: common_vendor.t(item.txt),
|
|
c: index
|
|
};
|
|
})
|
|
} : {});
|
|
}
|
|
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-cb7d404f"], ["__file", "D:/111111000000/uniapp04/pages/blueTooth/blueTooth.vue"]]);
|
|
my.createPage(MiniProgramPage);
|