2026-09-02 16:10:57 +08:00

98 lines
2.4 KiB
Plaintext

<script lang="uts">
import {BleLib,MyApiResult,BleScanResult,BleServices} from "@/uni_modules/android-ble"
import * as Ble from "@/src/BluetoothManager.uts"
import * as McmChannel from "@/src/mcm/McmChannel.uts"
// #ifdef APP-ANDROID || APP-HARMONY
let firstBackTime = 0
// #endif
export default {
globalData: {
ble: new BleLib(), // 蓝牙全局变量
// 接收蓝牙数据的回调
setBleNotity(){
getApp().globalData.ble.onNotityReadBleData(getApp().globalData.ble.getSericUUID(),
getApp().globalData.ble.getNotityUUID(),
true,
function(res:MyApiResult){
// console.log(res)
uni.$emit("bleData", res)
}
)
},
// 连接蓝牙
ConnectBle(address: string, autoReconnect: boolean){
getApp().globalData.ble.connect(address, autoReconnect, function(res:MyApiResult){
// console.log(res)
if(res.type==0){
getApp().globalData.ble.scanServices(function(resSevice:MyApiResult){
// console.log(resSevice);
// 显式选中 CAIIC-MCM 的 CLS 服务组合(自动识别可能选错)
Ble.selectService("0000CA10")
getApp().globalData.setBleNotity()
// getApp().globalData.ble.readRssi(function(b:MyApiResult){
// console.log(b)
// })
// console.log(getApp().globalData.ble.getwriteUUID());
// console.log(getApp().globalData.ble.getNotityUUID());
uni.$emit("bleState",res.type)
});
}else{
uni.$emit("bleState",res.type)
}
})
},
},
onLaunch: function () {
console.log('App Launch')
// 注册 CAIIC-MCM 协议通道的全局蓝牙事件监听
McmChannel.attach()
},
onShow: function () {
console.log('App Show')
},
onHide: function () {
console.log('App Hide')
},
// #ifdef APP-ANDROID || APP-HARMONY
onLastPageBackPress: function () {
console.log('App LastPageBackPress')
if (firstBackTime == 0) {
uni.showToast({
title: 'Press Exit Application again',
position: 'bottom',
})
firstBackTime = Date.now()
setTimeout(() => {
firstBackTime = 0
}, 2000)
} else if (Date.now() - firstBackTime < 2000) {
firstBackTime = Date.now()
uni.exit()
}
},
// #endif
onExit: function () {
console.log('App Exit')
},
methods: {
}
}
</script>
<style>
/*每个页面公共css */
.uni-row {
flex-direction: row;
}
.uni-column {
flex-direction: column;
}
</style>