fix:修改蓝牙功能以及增加维修扫码页面添加
This commit is contained in:
parent
8987c3a4ae
commit
4dcb8ecf26
File diff suppressed because it is too large
Load Diff
@ -162,7 +162,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref, onMounted } from 'vue';
|
import { ref, onMounted ,reactive } from 'vue';
|
||||||
import {
|
import {
|
||||||
showModelMessage
|
showModelMessage
|
||||||
} from '@/utils/tools.js';
|
} from '@/utils/tools.js';
|
||||||
@ -170,8 +170,14 @@
|
|||||||
import { AES128ECBEncrypt } from '@/utils/aes_util.js';
|
import { AES128ECBEncrypt } from '@/utils/aes_util.js';
|
||||||
const bluetoothForm = ref(null);
|
const bluetoothForm = ref(null);
|
||||||
const bikeCodeScanRef = ref(null);
|
const bikeCodeScanRef = ref(null);
|
||||||
const serviceId = '0000FFF0-0000-1000-8000-00805F9B34FB';
|
// const serviceId = '0000FFF0-0000-1000-8000-00805F9B34FB';
|
||||||
const characteristicId = '0000FFF6-0000-1000-8000-00805F9B34FB';
|
// const characteristicId = '0000FFF6-0000-1000-8000-00805F9B34FB';
|
||||||
|
// 这里的 serviceId 和 characteristicId 需要根据实际设备的蓝牙服务和特征进行替换
|
||||||
|
const ids = reactive({
|
||||||
|
serviceId:'',
|
||||||
|
characteristicId:'',//读取characteristicId
|
||||||
|
writeCharacteristicId:''//写入characteristicId
|
||||||
|
})
|
||||||
|
|
||||||
const blueTooth = ref({
|
const blueTooth = ref({
|
||||||
blueConn:false,
|
blueConn:false,
|
||||||
@ -414,7 +420,7 @@
|
|||||||
createBLEConnection();
|
createBLEConnection();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
startNotify();
|
getBLEDeviceServices()
|
||||||
},
|
},
|
||||||
fail(err) {
|
fail(err) {
|
||||||
console.log("createBLEConnection err",err);
|
console.log("createBLEConnection err",err);
|
||||||
@ -431,12 +437,67 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getBLEDeviceServices =() => {
|
||||||
|
//获取蓝牙设备所有服务(service)。
|
||||||
|
uni.getBLEDeviceServices({
|
||||||
|
deviceId: blueTooth.value.deviceId,
|
||||||
|
success: function (res) {
|
||||||
|
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);
|
||||||
|
if (subServiceId == '0000fff0' || subServiceId == '0000FFF0' || subServiceId == '6e400001' || subServiceId == '6E400001') {
|
||||||
|
ids.serviceId = serviceId;
|
||||||
|
//获取蓝牙设备某个服务中所有特征值(characteristic)。
|
||||||
|
uni.getBLEDeviceCharacteristics({
|
||||||
|
deviceId: blueTooth.value.deviceId,
|
||||||
|
serviceId: serviceId.value,
|
||||||
|
success: function (res) {
|
||||||
|
console.log('获取到' + res.characteristics.length + '个特征值');
|
||||||
|
console.log('获取特征值:', res.characteristics);
|
||||||
|
for (var i = 0; i < res.characteristics.length; i++) {
|
||||||
|
var characteristicId = res.characteristics[i].uuid;
|
||||||
|
var subCharacteristic = characteristicId.substring(0, 8);
|
||||||
|
//读
|
||||||
|
if (subCharacteristic == '0000fff6' || subCharacteristic == '0000FFF6' || subCharacteristic == '6e400003' || subCharacteristic == '6E400003') {
|
||||||
|
ids.characteristicId = characteristicId;
|
||||||
|
}
|
||||||
|
//写
|
||||||
|
else if (subCharacteristic == '6e400002' || subCharacteristic == '6E400002') {
|
||||||
|
ids.writeCharacteristicId = characteristicId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
startNotify();
|
||||||
|
},
|
||||||
|
fail: function () {
|
||||||
|
console.log('获取特征值失败');
|
||||||
|
uni.showToast({
|
||||||
|
title: '连接失败,请重启蓝牙',
|
||||||
|
duration: 3000,
|
||||||
|
mask: true,
|
||||||
|
icon: 'none',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}else{
|
||||||
|
console.log('不符合的subServiceId =' + subServiceId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fail: function (res) {
|
||||||
|
console.log('获取服务失败');
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const startNotify = () => {
|
const startNotify = () => {
|
||||||
uni.notifyBLECharacteristicValueChange({
|
uni.notifyBLECharacteristicValueChange({
|
||||||
state:true,
|
state:true,
|
||||||
deviceId: blueTooth.value.deviceId,
|
deviceId: blueTooth.value.deviceId,
|
||||||
serviceId: serviceId,
|
serviceId: ids.serviceId,
|
||||||
characteristicId: characteristicId,
|
characteristicId: ids.characteristicId,
|
||||||
success(res) {
|
success(res) {
|
||||||
console.log("notifyBLECharacteristicValueChange success",res);
|
console.log("notifyBLECharacteristicValueChange success",res);
|
||||||
//stopDiscovery();
|
//stopDiscovery();
|
||||||
@ -511,9 +572,9 @@
|
|||||||
const writeBLECharacteristicValueWithBuff = (buffer) => {
|
const writeBLECharacteristicValueWithBuff = (buffer) => {
|
||||||
uni.writeBLECharacteristicValue({
|
uni.writeBLECharacteristicValue({
|
||||||
deviceId: blueTooth.value.deviceId,
|
deviceId: blueTooth.value.deviceId,
|
||||||
serviceId: serviceId,
|
serviceId: ids.serviceId,
|
||||||
characteristicId: characteristicId,
|
characteristicId: ids.writeCharacteristicId,
|
||||||
value: buffer,
|
value: buffer,
|
||||||
success: function (res) {
|
success: function (res) {
|
||||||
console.log('writeBLECharacteristicValue success', res.errMsg)
|
console.log('writeBLECharacteristicValue success', res.errMsg)
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user