ibeacon.vue
6.42 KB
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
<template>
<view>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-btn-v">
<button type="primary" :disabled="isOpen" @click="openBluetoothAdapter">打开蓝牙模块</button>
<button type="primary" :disabled="!isOpen" @click="closeBluetoothAdapter">关闭蓝牙模块</button>
<button type="primary" :disabled="!isOpen || isStarted" :loading="isStarted" @click="startBeaconDiscovery">开始搜索附近的iBeacon设备</button>
<button type="primary" :disabled="!isStarted" @click="stopBeaconDiscovery">停止搜索附近的iBeacon设备</button>
</view>
</view>
<scroll-view class="uni-scroll_box">
<view class="uni-title" v-if="isStarted || deviceList.length > 0">已经发现 {{ deviceList.length }} 台设备:</view>
<view class="uni-list-box" v-for="(item, index) in deviceList" :key="item.uuid">
<view>
<view class="uni-list_name">UUID: {{ item.uuid }}</view>
<view class="uni-list_item">major: {{ item.major }}</view>
<view class="uni-list_item">minor: {{ item.minor }}</view>
<view class="uni-list_item">rssi: {{ item.rssi }} dBm</view>
<view class="uni-list_item">accuracy: {{ item.accuracy }}</view>
<view class="uni-list_item">heading: {{ item.heading }}</view>
</view>
</view>
</scroll-view>
</view>
</template>
<script>
const DEVICE_UUID_WEICHAT = 'FDA50693-A4E2-4FB1-AFCF-C6EB07647825';
export default {
data() {
return {
title: 'iBeacon',
isOpen: false,
isStarted: false,
deviceList: [],
isPageOpened: false
};
},
onLoad() {
this.onBeaconUpdate();
},
onShow() {
this.isPageOpened = true;
},
methods: {
maskclose() {
this.maskShow = false;
},
openBluetoothAdapter() {
uni.openBluetoothAdapter({
success: (res) => {
console.log('初始化蓝牙成功:' + res.errMsg);
console.log(res);
this.isOpen = true;
this.deviceList = [];
},
fail: (err) => {
console.log('初始化蓝牙失败,错误码:' + (err.errCode || err.errMsg));
if (err.errCode !== 0) {
initTypes(err.errCode, err.errMsg);
}
}
});
},
closeBluetoothAdapter(OBJECT) {
this.stopBeaconDiscovery();
wx.closeBluetoothAdapter({
success: (res) => {
this.isOpen = false;
console.log('断开蓝牙模块成功');
}
});
},
onBeaconUpdate() {
uni.onBeaconUpdate(res => {
if (!this.isPageOpened || !this.isOpen || !this.isStarted) {
return;
}
console.log(res);
// if (res.code !== 0) {
// return;
// }
if (res.beacons && res.beacons.length > 0) {
this.getBeacons();
} else if (!res.connected) {
this.deviceList = [];
}
});
},
startBeaconDiscovery() {
uni.startBeaconDiscovery({
uuids: this.getUUIDList(),
success: (res) => {
this.isStarted = true;
console.log(res);
},
fail: (err) => {
console.error(err);
}
});
},
stopBeaconDiscovery(types) {
if(this.isStarted) {
uni.stopBeaconDiscovery({
success: (res) => {
this.isStarted = false;
},
fail: (err) => {
console.error(err);
}
});
}
},
getBeacons() {
uni.getBeacons({
success: (res) => {
if (res.beacons && res.beacons.length > 0) {
console.log(res.beacons);
this.deviceList = res.beacons;
}
},
fail: (err) => {
console.log('获取设备服务失败,错误码:' + err.errCode);
if (errCode.errCode !== 0) {
initTypes(errCode.errCode);
}
}
});
},
getUUIDList() {
// #ifdef APP-PLUS
const sysInfo = uni.getSystemInfoSync();
if (!sysInfo) {
return [];
}
let isIOS = sysInfo.platform ? (sysInfo.platform.toLowerCase() === "ios") : false;
if (isIOS) {
return [DEVICE_UUID_WEICHAT];
}
return [];
// #endif
// #ifndef APP-PLUS
return [DEVICE_UUID_WEICHAT];
// #endif
}
},
onUnload() {
this.isPageOpened = false;
}
};
/**
* 判断初始化蓝牙状态
*/
function initTypes(code, errMsg) {
switch (code) {
case 10000:
toast('未初始化蓝牙适配器');
break;
case 10001:
toast('未检测到蓝牙,请打开蓝牙重试!');
break;
case 10002:
toast('没有找到指定设备');
break;
case 10003:
toast('连接失败');
break;
case 10004:
toast('没有找到指定服务');
break;
case 10005:
toast('没有找到指定特征值');
break;
case 10006:
toast('当前连接已断开');
break;
case 10007:
toast('当前特征值不支持此操作');
break;
case 10008:
toast('其余所有系统上报的异常');
break;
case 10009:
toast('Android 系统特有,系统版本低于 4.3 不支持 BLE');
break;
default:
toast(errMsg);
}
}
/**
* 弹出框封装
*/
function toast(content, showCancel = false) {
uni.showModal({
title: '提示',
content,
showCancel
});
}
</script>
<style>
.uni-title {
/* width: 100%; */
/* height: 80rpx; */
text-align: center;
}
.uni-mask {
position: fixed;
top: 0;
left: 0;
bottom: 0;
display: flex;
align-items: center;
width: 100%;
background: rgba(0, 0, 0, 0.6);
padding: 0 30rpx;
box-sizing: border-box;
}
.uni-scroll_box {
height: 70%;
background: #fff;
border-radius: 20rpx;
}
.uni-list-box {
margin: 0 20rpx;
padding: 15rpx 0;
border-bottom: 1px #f5f5f5 solid;
box-sizing: border-box;
}
.uni-list:last-child {
border: none;
}
.uni-list_name {
font-size: 30rpx;
color: #333;
}
.uni-list_item {
font-size: 24rpx;
color: #555;
line-height: 1.5;
}
.uni-success_box {
position: absolute;
left: 0;
bottom: 0;
min-height: 100rpx;
width: 100%;
background: #fff;
box-sizing: border-box;
border-top: 1px #eee solid;
}
.uni-success_sub {
/* width: 100%%; */
height: 100rpx;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 30rpx;
}
.uni-close_button {
padding: 0 20rpx;
height: 60rpx;
line-height: 60rpx;
background: #ce3c39;
color: #ffffff;
border-radius: 10rpx;
}
.uni-success_content {
height: 600rpx;
margin: 30rpx;
margin-top: 0;
border: 1px #eee solid;
padding: 30rpx;
}
.uni-content_list {
padding-bottom: 10rpx;
border-bottom: 1px #f5f5f5 solid;
}
.uni-tips {
text-align: center;
font-size: 24rpx;
color: #666;
}
</style>