wujingjing
2024-03-19 f009788a408214b630149e995389807ef259226c
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<template>
    <view class="content search_box">
        <view style="position: absolute;width: 100%;height: 100%;background: #010029;">
        <image src="../../static/background.png" style="width: 100%;height: 100%;"></image>
        </view>
        <view class="search_btn_box" @click="foundDevice">
            <view :class="foundLodaing?'search_animation':'search_img'">
                <img src="../../static/w_icon.png" alt="">
            </view>
            <view class="search_title">搜索蓝牙</view>
        </view>
        <!-- 蓝牙设备列表对话框 -->
        <view class="cu-modal" :class="productListModal==true?'show':''">
            <view class="cu-dialog" style="height:500upx;width: 100%;">
                <view class="padding-sm flex" style="width:100%;justify-content: space-between;">
                    <text class="text-gray">设备列表</text>
                    <text class="text-gray">设备数量:{{bluetoothDeviceFoundList.length}}</text>
                </view>
                <!-- 设备列表部分 -->
                <scroll-view scroll-y="true" style="width:100%;height:calc(100vh - 370rpx)">
                    <view class="cu-list menu sm-border">
                        <view class="cu-item" style="min-height: 100rpx;"
                            v-for=" (device_item,device_index) in bluetoothDeviceFoundList" :key="device_index">
                            <view class="content" @click="stepLodaing(device_item)">
                                <view style="width: 100%;">
                                    <text class="text-grey iconfont icon-huaban" style="font-size: 60rpx;"></text>
                                    <text class="text-gray"
                                        style="font-size: 36rpx; margin: 30rpx;">{{device_item.name}}</text>
                                </view>
                            </view>
                        </view>
                    </view>
                </scroll-view>
            </view>
        </view>
 
    </view>
</template>
 
<script>
    var Constant = require('../../utils/constant.js');
    export default {
        data() {
            return {
                initBlueToothStatus: false, //蓝牙的初始化状态
                selectActiveIndex: 1, //顶部选择激活
                uuidListByService: [], //存放 开始搜索蓝牙外围设备时,对应uuid的设备
                bluetoothDeviceFoundList: [], //存放寻找到的附加蓝牙设备的列表
                foundLodaing: false, //点击搜索蓝牙按钮的时候触发等待
                productListModal: false, //设备列表对话框显示隐藏
                startBluetoothDevicesDiscoveryRecord: false, //记录是否搜索过蓝牙设备 默认没有为false
                historyConnectDeviceList: [], //历史连接的设备的记录
                filterName: 'XinLi-XL11_', //筛选搜索固定的设备
                isHideHistoryList: false, //显示或者隐藏历史连接设备列表
                backgroundURL: Constant.backgroundURL
            }
        },
        onLoad() {
            this.openBluetoothAdapter()
            let historyConnectDeviceList = uni.getStorageSync("productListInfo")
            console.log(historyConnectDeviceList)
            if (historyConnectDeviceList.length > 0) {
                this.isHideHistoryList = true
            }
            // console.log(historyConnectDeviceList)
            this.historyConnectDeviceList = historyConnectDeviceList
 
        },
        onUnload() {
            this.StopBluetoothDevicesDiscovery()
        },
        onShareAppMessage: function() {
            return Constant.Share
        },
        methods: {
            //初始化蓝牙
            openBluetoothAdapter: function() {
                let that = this
                uni.openBluetoothAdapter({
                    mode: 'central',
                    success: (res) => {
                        console.log('初始化蓝牙成功', res)
                        this.initBlueToothStatus = true
                    },
                    fail: (err) => {
                        console.log('初始化蓝牙失败', err)
                        this.initBlueToothStatus = false
                        that.errMsgDetail(err)
                        uni.showModal({
                            title: '提示',
                            content: err.errMsg,
                            showCancel: false
                        })
                    },
                    complete: (res) => {
                        console.log('openBluetoothAdapter complete', res);
                        that.onBluetoothAdapterStateChange() //监听蓝牙开启状态
                    }
                })
            },
            //监听蓝牙适配器的状态
            onBluetoothAdapterStateChange: function() {
                uni.onBluetoothAdapterStateChange((res) => {
                    // console.log('监听蓝牙适配器的状态', res)
                    let status = res.available //蓝牙适配器是否可用
                    let isFound = res.discovering //蓝牙是否在搜索中
                    let Record = this.startBluetoothDevicesDiscoveryRecord
 
                    if (!status) {
                        uni.showModal({
                            title: '提示',
                            content: '请打开蓝牙',
                            showCancel: false
                        })
                        this.bluetoothDeviceFoundList = []
                    }
                })
            },
 
            //开始搜索蓝牙外围设备
            startBluetoothDevicesDiscovery: function() {
                let that = this
                uni.startBluetoothDevicesDiscovery({
                    services: that.uuidListByService,
                    allowDuplicatesKey: false,
                    powerLevel: 'high',
                    success: res => {
                        // console.log('开始搜索蓝牙设备', res)
 
                        that.onBluetoothDeviceFound() //成功搜索后,监听寻找到的蓝牙设备
                        setTimeout(function() {
                            that.getBluetoothDevices()
                        }, 1000) // 延时是为了让蓝牙搜索一会,获取寻找到的所有蓝牙设备
                    },
                    fail: err => {
                        console.log('搜索蓝牙设备失败', err)
                        that.foundLodaing = false
                        uni.showToast({
                            title: '蓝牙未打开',
                            icon: "none",
                            duration: 1000,
                        })
                    }
                })
            },
            //停止搜索附近蓝牙设备
            StopBluetoothDevicesDiscovery: function() {
                uni.stopBluetoothDevicesDiscovery({
                    success: (res) => {
                        console.log('成功停止搜索设备', res)
                    },
                    fail: (err) => {
                        console.log('停止搜索设备失败', err)
                    }
                })
            },
            //监听寻找蓝牙新设备
            onBluetoothDeviceFound: function() {
                uni.onBluetoothDeviceFound((result) => {
                    //console.log(result)
                    // for (let i = 0; i < this.bluetoothDeviceFoundList.length; i++) {
                    //     if (this.bluetoothDeviceFoundList[i].deviceId.indexOf(result.devices[0].deviceId) != -
                    //         1) {
                    //         this.bluetoothDeviceFoundList.push(result.devices[0])
                    //     }
                    // }
                    // if (result.devices[0].name == "") {
                    //     result.devices[0].name = '未知设备'
                    //     this.bluetoothDeviceFoundList.push(result.devices[0])
                    //     // console.log(this.bluetoothDeviceFoundList)
                    // }
                })
            },
 
            //获取在蓝牙开启有效期内搜索到的所有蓝牙设备
            getBluetoothDevices: function() {
                let that = this
                let foundList = []
                let filterName = "XinLi-XL11_"
                uni.getBluetoothDevices({
                    success: (result) => {
                        console.log('打印蓝牙有效期内搜索的所有蓝牙设备', result)
                        for (let i = 0; i < result.devices.length; i++) {
                            let allInfo = result.devices[i]
                            if (allInfo.name.indexOf("XinLi-XL11") != -1) {
                                allInfo.name = allInfo.name.replace("XinLi-XL11", "Eventech") //文字替换
                                // console.log('123', allInfo.name)
                            }
                            if (allInfo.name.indexOf("Eventech") != -1) {
                                let serviceInfo = {
                                    RSSI: allInfo.RSSI,
                                    deviceId: allInfo.deviceId,
                                    name: allInfo.name
                                }
                                foundList.push(serviceInfo)
                            }
                        }
                        if (foundList.length == 0) {
                            uni.showToast({
                                title: '暂未发现设备,请重新搜索',
                                icon: 'none',
                                duration: 1000
                            })
                            setTimeout(function() {
                                that.foundLodaing = false
                            }, 1000);
                            return;
                        }
                        if (foundList.length >= 2) {
                            that.productListModal = true
                        } else {
                            that.stepLodaing(foundList[0])
                        }
                        that.bluetoothDeviceFoundList = foundList
                        setTimeout(function() {
                            that.foundLodaing = false
                        }, 1000);
 
                        // console.log(that.data.bluetoothDeviceFoundList)
                    },
                    fail: (fail) => {
                        console.log('获取蓝牙有效期内搜索的所有蓝牙设备失败', fail)
                        setTimeout(function() {
                            that.foundLodaing = false
                        }, 1000)
                    }
                })
            },
            //错误代码处理
            errMsgDetail: function(err) {
                // console.log(err.errCode)
                if (err.errCode == 0) {
                    err.errMsg = '正常'
                } else if (err.errCode == -1) {
                    err.errMsg = '已连接'
                } else if (err.errCode == 10000) {
                    err.errMsg = '未初始化蓝牙'
                } else if (err.errCode = 10001) {
                    err.errMsg = '当前蓝牙适配器不可用,请打开蓝牙再搜索'
                } else if (err.errCode == 10002) {
                    err.errMsg = '没有找到指定设备'
                } else if (err.errCode == 10003) {
                    err.errMsg = '连接失败'
                } else if (err.errCode == 10004) {
                    err.errMsg = '没有找到指定服务'
                } else if (err.errCode == 10005) {
                    err.errMsg = '没有找到特征指'
                } else if (err.errCode == 10006) {
                    err.errMsg = '当前连接已断开'
                } else if (err.errCode == 10007) {
                    err.errMsg = '当前特征值不支持此操作'
                } else if (err.errCode == 10008) {
                    err.errMsg = '其余所有系统上报的异常'
                } else if (err.errCode == 10009) {
                    err.errMsg = '系统版本低于 4.3 不支持 蓝牙'
                } else if (err.errCode == 10012) {
                    err.errMsg = '连接超时'
                } else if (err.errCode == 10013) {
                    err.errMsg = '连接 deviceId 为空或者是格式不正确'
                } else {
                    err.errMsg = err.errMsg
                }
            },
 
 
            //搜索蓝牙设备
            foundDevice: function() {
                this.foundLodaing = true
                this.startBluetoothDevicesDiscovery() //蓝牙初始化成功后开始搜索附加设备
            },
 
 
            //跳转到连接等待页面
            stepLodaing: function(device) {
                //连接蓝牙设备前先关闭蓝牙设备的搜索,防止资源占用过大
                this.StopBluetoothDevicesDiscovery()
                this.productListModal = false
                let info = JSON.stringify(device)
                console.log('打印连接设备的基本信息', info)
 
                // this.bluetoothDeviceFoundList = []
                uni.reLaunch({
                    url: '../DeviceDetail/index?device=' + info
                })
            }
        }
    }
</script>
 
<style>
    @import url("/style/animation.css");
    @import url("/style/main.css");
    @import url("/style/icon.css");
    @import url("/style/iconfont.css");
 
    .search_box {
        position: relative;
        width: 100vw;
        height: 100vh;
    }
 
    .search_btn_box {
        width: 300rpx;
        height: 300rpx;
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        right: 0;
        margin: auto;
        background-color: #007AFF;
        border-radius: 100%;
    }
 
    .search_title {
        text-align: center;
        padding: 10rpx;
        color: #007AFF;
    }
 
    .search_img,
    .search_animation {
        width: 300rpx;
        height: 300rpx;
        text-align: center;
        line-height: 300rpx;
    }
 
    .search_img img,
    .search_animation img {
        width: 60%;
        height: 60%;
        margin-top: 20%;
    }
 
    .search_animation:before {
        content: "";
        position: absolute;
        top: 5px;
        left: 5px;
        right: 5px;
        bottom: 5px;
        border-radius: 50%;
        border: 3px solid transparent;
        border-top-color: #fff;
        animation: spin-reverse .6s linear infinite;
    }
 
    .search_animation:after {
        content: "";
        position: absolute;
        top: 15px;
        left: 15px;
        right: 15px;
        bottom: 15px;
        border-radius: 50%;
        border: 3px solid transparent;
        border-top-color: #fff;
        animation: spin 1s linear infinite;
    }
 
    @-webkit-keyframes spin {
        0% {
            -webkit-transform: rotate(0deg);
        }
 
        100% {
            -webkit-transform: rotate(360deg);
        }
    }
 
    @keyframes spin {
        0% {
            -webkit-transform: rotate(0deg);
            transform: rotate(0deg);
        }
 
        100% {
            -webkit-transform: rotate(360deg);
            transform: rotate(360deg);
        }
    }
 
    @-webkit-keyframes spin-reverse {
        0% {
            -webkit-transform: rotate(0deg);
        }
 
        100% {
            -webkit-transform: rotate(-360deg);
        }
    }
 
    @keyframes spin-reverse {
        0% {
            -webkit-transform: rotate(0deg);
            transform: rotate(0deg);
        }
 
        100% {
            -webkit-transform: rotate(-360deg);
            transform: rotate(-360deg);
        }
    }
 
    .logo {
        height: 200rpx;
        width: 200rpx;
        margin-top: 200rpx;
        margin-left: auto;
        margin-right: auto;
        margin-bottom: 50rpx;
    }
 
    .text-area {
        display: flex;
        justify-content: center;
    }
</style>