fix:修改蓝牙

This commit is contained in:
5g0Wp7Zy 2025-06-16 15:27:34 +08:00
parent 2fe3f68e2a
commit 1165735a11

View File

@ -162,7 +162,7 @@
</template>
<script setup>
import { ref, onMounted ,reactive } from 'vue';
import { ref, onMounted ,reactive, onBeforeUnmount } from 'vue';
import {
showModelMessage
} from '@/utils/tools.js';
@ -230,6 +230,7 @@
success(res) {
if(res.errno == 0){
blueTooth.value.blueConn = true;
blueTooth.value.bikeCode = '250306001002'
}
else
{
@ -256,6 +257,24 @@
}
});
});
onBeforeUnmount(() => {
//
if(blueTooth.value.deviceId != ""){
closeConn();
}
//
stopBluetoothDevicesDiscovery();
//
uni.closeBluetoothAdapter({
success(res) {
console.log("closeBluetoothAdapter success", res);
},
fail(err) {
console.log("closeBluetoothAdapter err", err);
}
});
})
let timer;
const bikeCodeScanChange = (data) => {
@ -445,8 +464,10 @@
console.log('蓝牙服务:', res.services);
for (var i = 0; i < res.services.length; i++) {
var serviceId = res.services[i].uuid;
var subServiceId = serviceId.value.substring(0, 8);
var subServiceId = serviceId.substring(0, 8);
if (subServiceId == '0000fff0' || subServiceId == '0000FFF0' || subServiceId == '6e400001' || subServiceId == '6E400001') {
console.log('符合的subServiceId =' + subServiceId);
ids.serviceId = serviceId;
//(characteristic)
uni.getBLEDeviceCharacteristics({
@ -460,10 +481,13 @@
var subCharacteristic = characteristicId.substring(0, 8);
//
if (subCharacteristic == '0000fff6' || subCharacteristic == '0000FFF6' || subCharacteristic == '6e400003' || subCharacteristic == '6E400003') {
console.log('characteristicId =' + characteristicId);
ids.characteristicId = characteristicId;
}
//
else if (subCharacteristic == '6e400002' || subCharacteristic == '6E400002') {
console.log('writeCharacteristicId =' + characteristicId);
ids.writeCharacteristicId = characteristicId;
}
}
@ -482,11 +506,12 @@
fail: function (res) {
console.log('获取服务失败');
},
})
})
}
const startNotify = () => {
console.log('ids----------',ids);
uni.notifyBLECharacteristicValueChange({
state:true,
deviceId: blueTooth.value.deviceId,
@ -564,6 +589,9 @@
}
const writeBLECharacteristicValueWithBuff = (buffer) => {
console.log('执行的命令--------',buffer);
console.log('characteristicId:' + ids.writeCharacteristicId);
uni.writeBLECharacteristicValue({
deviceId: blueTooth.value.deviceId,
serviceId: ids.serviceId,