100 lines
2.7 KiB
Plaintext
100 lines
2.7 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(), // 蓝牙全局变量
|
||
// 接收蓝牙数据的回调(显式指定 CAIIC-MCM 服务/notify UUID,不依赖插件自动识别;
|
||
// 用当前接口 onNotityBleData,旧接口 onNotityReadBleData 插件已标记弃用)
|
||
setBleNotity(){
|
||
getApp().globalData.ble.onNotityBleData(Ble.MCM_SERVICE_UUID,
|
||
Ble.MCM_NOTIFY_UUID,
|
||
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){
|
||
getApp().globalData.ble.connect(address, autoReconnect, function(res:MyApiResult){
|
||
console.log("[MCM] connect " + address + ": type=" + res.type + " msg=" + res.message)
|
||
if(res.type==0){
|
||
getApp().globalData.ble.scanServices(function(resSevice:MyApiResult){
|
||
console.log("[MCM] scanServices: type=" + resSevice.type + " msg=" + resSevice.message)
|
||
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> |