schangxiang@126.com
2025-09-17 ff43ddf18764629ff875478e4e47a7281cbd230a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<template>
    <view class="bluetooth-connection-page">
        <cu-custom bgColor="bg-white" :isBack="true">
            <block slot="backText">
            </block>
            <block slot="content">蓝牙连接</block>
        </cu-custom>
        <scroll-view scroll-y="true" style="height: 100vh;">
            <view class="page-content">
                <view v-for="(item,index) in myBluetoothes" :key="item.deviceId" class="bluetooth-btn" @tap="onConnect(item.deviceId)">
                    <view class="blue-btn-id">{{item.deviceId}}</view>
                    <view class="blue-btn-name">{{item.name}}</view>
                </view>
                <button v-if="myBluetoothes.length!==0" type="primary" @tap="research" style="width: 100%;">重新搜索</button>
            </view>
        </scroll-view>
    </view>
</template>
 
<script>
import BluePrint from './bluePrint.js'
export default {
    name:'bluetoothConnectionPage',
    data(){
        return {
            initAgain:false,
            myBluetoothes:[]
        }
    },
    onLoad(option) {
        if (option.again==='1') {
            this.initAgain = true
        } else {
            this.initAgain = false
        }
    },
    onReady(){
        this.search(this.initAgain)
    },
    methods:{
        search(re=false){
            let methodName = 'getBlueToothes'
            if (re) methodName = 'reGetBlueToothes'
            BluePrint[methodName](this.$store).then((res)=>{
                this.myBluetoothes = res
            }).catch((err)=>{
                if (typeof err !== 'string'){
                    err = '获取蓝牙设备列表失败'
                }
                BluePrint.alert(err)
            })
        },
        research(){
            this.myBluetoothes = [];
            this.search(true)
        },
        onConnect(id){
            BluePrint.connect(this.$store,id).then(()=>{
                uni.navigateBack()
            }).catch((err)=>{
                if (typeof err !== 'string'){
                    err = '连接蓝牙设备失败'
                }
                BluePrint.alert(err)
            })
        }
    }
}
</script>
 
<style lang="scss" scoped>
.bluetooth-connection-page{
    .page-content{
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 30upx 30upx 0 30upx;
        .bluetooth-btn{
            box-sizing: border-box;
            width: 100%;
            margin-bottom: 30upx;
            border: 1px solid #F18201;
            background-color: #f8c07f;
            color: #fff;
            border-radius: 20upx;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20upx;
            font-size: 30upx;
            .blue-btn-id,.blue-btn-name{
                word-wrap: break-word;
                word-break: break-all;
                white-space: pre-wrap;
                text-align: center;
            }
            .blue-btn-name{
                font-size: 1.5em;
                font-weight: bold;
            }
        }
    }
}
</style>