113 lines
3.7 KiB
JavaScript
113 lines
3.7 KiB
JavaScript
"use strict";
|
|
var common_vendor = require("../../common/vendor.js");
|
|
const _sfc_main = {
|
|
setup(__props) {
|
|
common_vendor.onMounted(() => {
|
|
openBluetoothAdapter();
|
|
});
|
|
common_vendor.onUnmounted(() => {
|
|
if (discoveryStarted.value) {
|
|
stopBluetoothDevicesDiscovery();
|
|
}
|
|
});
|
|
const bluetoothDevices = common_vendor.reactive([]);
|
|
const ignoreDevices = common_vendor.reactive([]);
|
|
let discoveryStarted = common_vendor.ref(false);
|
|
let showLoading = common_vendor.ref(true);
|
|
const openBluetoothAdapter = () => {
|
|
wx.openBluetoothAdapter({
|
|
success: (res) => {
|
|
console.log("openBluetoothAdapter success", res);
|
|
startBluetoothDevicesDiscovery();
|
|
},
|
|
fail: (res) => {
|
|
console.log(res);
|
|
if (res.errCode === 10001) {
|
|
wx.onBluetoothAdapterStateChange(function(res2) {
|
|
console.log("onBluetoothAdapterStateChange", res2);
|
|
if (res2.available) {
|
|
startBluetoothDevicesDiscovery();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
};
|
|
const startBluetoothDevicesDiscovery = () => {
|
|
if (discoveryStarted.value) {
|
|
return;
|
|
}
|
|
discoveryStarted.value = true;
|
|
wx.startBluetoothDevicesDiscovery({
|
|
success: (res) => {
|
|
console.log("startBluetoothDevicesDiscovery success", res);
|
|
onBluetoothDeviceFound();
|
|
},
|
|
fail: (err) => {
|
|
discoveryStarted.value = false;
|
|
wx.showToast({
|
|
title: "\u5F00\u542F\u641C\u7D22\u5931\u8D25",
|
|
icon: "none",
|
|
duration: 2e3
|
|
});
|
|
console.error("\u5F00\u542F\u84DD\u7259\u8BBE\u5907\u641C\u7D22\u5931\u8D25", err);
|
|
}
|
|
});
|
|
};
|
|
const onBluetoothDeviceFound = () => {
|
|
wx.onBluetoothDeviceFound((res) => {
|
|
showLoading.value = false;
|
|
res.devices.forEach((device) => {
|
|
console.log(device);
|
|
if (device.RSSI > -80 && !bluetoothDevices.some((d) => d.deviceId === device.deviceId) && !ignoreDevices.includes(device.deviceId)) {
|
|
bluetoothDevices.push(device);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
const createBLEConnection = (device) => {
|
|
stopBluetoothDevicesDiscovery();
|
|
wx.showLoading({
|
|
title: "\u8FDE\u63A5\u4E2D..."
|
|
});
|
|
wx.createBLEConnection({
|
|
deviceId: device.deviceId,
|
|
success(res) {
|
|
wx.hideLoading();
|
|
common_vendor.index.navigateTo({
|
|
url: "/pages/bluetooth/device?deviceId=" + device.deviceId + "&name=" + device.name
|
|
});
|
|
},
|
|
fail() {
|
|
wx.hideLoading();
|
|
wx.showToast({
|
|
title: "\u84DD\u7259\u8FDE\u63A5\u5931\u8D25",
|
|
icon: "none",
|
|
duration: 2e3
|
|
});
|
|
return;
|
|
}
|
|
});
|
|
};
|
|
const stopBluetoothDevicesDiscovery = () => {
|
|
wx.stopBluetoothDevicesDiscovery();
|
|
};
|
|
return (_ctx, _cache) => {
|
|
return common_vendor.e({
|
|
a: common_vendor.unref(showLoading)
|
|
}, common_vendor.unref(showLoading) ? {} : {
|
|
b: common_vendor.o(startBluetoothDevicesDiscovery),
|
|
c: common_vendor.f(common_vendor.unref(bluetoothDevices), (device, index, i0) => {
|
|
return {
|
|
a: common_vendor.t(device.name || device.deviceId),
|
|
b: index,
|
|
c: common_vendor.o(($event) => createBLEConnection(device))
|
|
};
|
|
})
|
|
});
|
|
};
|
|
}
|
|
};
|
|
var MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-0e0dc370"], ["__file", "C:/Users/bzm15/Desktop/ix/mini_web-develop/pages/bluetooth/index.vue"]]);
|
|
wx.createPage(MiniProgramPage);
|