94 lines
2.3 KiB
Vue
94 lines
2.3 KiB
Vue
<script lang="ts">
|
||
import { MyApiResult } from "@/src/BluetoothManager"
|
||
import * as Ble from "@/src/BluetoothManager"
|
||
import * as McmChannel from "@/src/mcm/McmChannel"
|
||
|
||
// #ifdef APP-PLUS
|
||
let firstBackTime = 0
|
||
// #endif
|
||
export default {
|
||
|
||
globalData: {
|
||
// 接收蓝牙数据的回调(显式指定 CAIIC-MCM 服务/notify UUID,基于 uni 内置 BLE API)
|
||
setBleNotity(){
|
||
Ble.onNotityReadBleData(true,
|
||
function(res: MyApiResult){
|
||
if (res.type != 0) {
|
||
// 1000=订阅成功 1001=订阅失败(type=0 的 notify 数据由 McmChannel 打印)
|
||
console.log("[MCM] notify subscribe result: type=" + res.type + " msg=" + res.message)
|
||
}
|
||
uni.$emit("bleData", res)
|
||
}
|
||
)
|
||
|
||
},
|
||
// 连接蓝牙
|
||
ConnectBle(address: string, autoReconnect: boolean){
|
||
Ble.connect(address, autoReconnect, function(res: MyApiResult){
|
||
console.log("[MCM] connect " + address + ": type=" + res.type + " msg=" + res.message)
|
||
if(res.type==0){
|
||
Ble.scanServices(function(resSevice: MyApiResult){
|
||
console.log("[MCM] scanServices: type=" + resSevice.type + " msg=" + resSevice.message)
|
||
if (resSevice.type == 0) {
|
||
(getApp().globalData as any).setBleNotity()
|
||
uni.$emit("bleState", res.type)
|
||
} else {
|
||
uni.$emit("bleState", 10000)
|
||
}
|
||
});
|
||
|
||
}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-PLUS
|
||
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()
|
||
plus.runtime.quit()
|
||
}
|
||
},
|
||
// #endif
|
||
onExit: function () {
|
||
console.log('App Exit')
|
||
},
|
||
methods: {
|
||
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style>
|
||
/*每个页面公共css */
|
||
.uni-row {
|
||
flex-direction: row;
|
||
}
|
||
|
||
.uni-column {
|
||
flex-direction: column;
|
||
}
|
||
</style>
|