- uni-app x官方无内置蓝牙API(文档明确见插件市场),为实现去插件化,工程整体迁移为uni-app vue3(vue/ts),App端内置完整BLE API,标准基座即可运行 - 新建src/BluetoothManager.ts:基于uni内置BLE API(createBLEConnection/notifyBLECharacteristicValueChange/writeBLECharacteristicValue(writeNoResponse)/setBLEMTU/getBLEDeviceRSSI等),保持原插件type约定,页面业务逻辑不变 - 删除uni_modules/android-ble及未使用的lime-*、uni-wifi;保留uni-icons、uni-scss - uts模块全部移植为ts(src/mcm、api、common):UTSJSONObject改Record<string,any>,适配vue3 getStorageSync空值返回空串的差异 - 7个页面uvue转vue:模板不变,显式import ref与生命周期钩子;OTA固件读取由java.io改为plus.io+uni.base64ToArrayBuffer - manifest.json去uni-app-x改app-plus结构,补Android 12+蓝牙权限;删除platformConfig.json;运行改用标准基座
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>
|