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

143 lines
3.1 KiB
Plaintext

<template>
<!-- #ifdef APP -->
<scroll-view style="flex:1">
<!-- #endif -->
<view class="page">
<view v-for="(item, index) in scanArray" :key="index" class="card">
<view class="cardBox" @tap="onLinkBluetooth(item)">
<view class="leftIcon">
<uni-icons type="paperclip" size="20"/>
</view>
<view class="rightContent">
<text>{{ item.device.name }}</text>
<text>{{ item.device.address }}</text>
</view>
</view>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script setup lang="uts">
import {BleLib,MyApiResult,BleScanResult,BleServices,ScanRssiBR,WNuuid} from "@/uni_modules/android-ble"
import * as Ble from "../../../src/BluetoothManager"
const scanArray = ref<BleScanResult[]>([]) // 蓝牙扫描结果
const refreshing = ref<boolean>(false)
// 连接蓝牙
function onLinkBluetooth(item: BleScanResult) {
console.log(item.device.name)
uni.navigateTo({
url: `/pages/bleTest/bluetoothOperation/bluetoothOperation?address=${encodeURIComponent(item.device.address)}`
})
}
function onBleStarScan() {
console.log("list")
if (refreshing.value) return
refreshing.value = true
const scanMs = 10*1000
scanArray.value = []
Ble.startScan(scanMs,function(res:MyApiResult){
// type 为0 扫描成功 10001 表示无蓝牙权限 10002 表示蓝牙未开启
// console.log(res.type)
if(res.type==0){
var scan = res.data as BleScanResult;
scanArray.value.push(scan);
}else {
// 处理扫描失败的情况(如没有蓝牙权限或蓝牙未开启)
if (res.type == 10003) {
uni.showToast({
title: 'No Bluetooth permission',
icon: 'none',
});
} else if (res.type == 10002) {
uni.showToast({
title: 'Bluetooth is not turned on',
icon: 'none',
});
}
}
})
// 只控制“动画”,不影响扫描继续
setTimeout(() => {
refreshing.value = false
uni.stopPullDownRefresh()
}, scanMs + 200)
}
// ======= 生命周期 ========
onLoad((options: UTSJSONObject | null) => {
if (options != null) {
}
})
onPageShow(() => {
onBleStarScan()
})
onPageHide(() => {
Ble.stopScan()
})
onUnload(() => {
console.log("stop")
Ble.stopScan()
})
// 下拉刷新
onPullDownRefresh(() => {
onBleStarScan()
})
</script>
<style scoped>
.page{
display:flex;
flex-direction:column;
margin-bottom: 16px;
}
/* 卡片 */
.card {
border-radius: 36rpx;
background-color: #ffffff;
padding: 20rpx;
margin-top: 20rpx;
/* 阴影效果 */
box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.08);
/* 横向布局:左图右文 */
display: flex;
flex-direction: column;
}
.cardBox {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.leftIcon {
width: 28px; /* 左侧固定宽度,避免挤压右侧 */
display: flex;
justify-content: center;
align-items: center;
margin-right: 10px;
}
.rightContent {
flex: 1; /* 右侧撑满剩余宽度 */
min-width: 0; /* 很关键:让 text-overflow 生效 */
}
.displyType {
color: #0000ff;
}
</style>