|
| 1 | +<template> |
| 2 | + <view> |
| 3 | + <page-head :title="title"></page-head> |
| 4 | + <view class="uni-padding-wrap uni-common-mt"> |
| 5 | + <view class="uni-btn-v"> |
| 6 | + <button type="primary" :disabled="isOpen" @click="openBluetoothAdapter">打开蓝牙模块</button> |
| 7 | + <button type="primary" :disabled="!isOpen" @click="closeBluetoothAdapter">关闭蓝牙模块</button> |
| 8 | + <button type="primary" :disabled="!isOpen || isStarted" @click="startBeaconDiscovery">开始搜索附近的iBeacon设备</button> |
| 9 | + <button type="primary" :disabled="!isStarted" @click="stopBeaconDiscovery">停止搜索附近的iBeacon设备</button> |
| 10 | + </view> |
| 11 | + </view> |
| 12 | + <scroll-view class="uni-scroll_box" scroll-y @touchmove.stop.prevent="moveHandle" @click.stop="moveHandle"> |
| 13 | + <view class="uni-title" v-if="isStarted || deviceList.length > 0">已经发现 {{ deviceList.length }} 台设备:</view> |
| 14 | + <view class="uni-list-box" v-for="(item, index) in deviceList" :key="item.uuid"> |
| 15 | + <view> |
| 16 | + <view class="uni-list_name">UUID: {{ item.uuid }}</view> |
| 17 | + <view class="uni-list_item">major: {{ item.major }}</view> |
| 18 | + <view class="uni-list_item">minor: {{ item.minor }}</view> |
| 19 | + <view class="uni-list_item">rssi: {{ item.rssi }} dBm</view> |
| 20 | + <view class="uni-list_item">accuracy: {{ item.accuracy }}</view> |
| 21 | + <view class="uni-list_item">heading: {{ item.heading }}</view> |
| 22 | + </view> |
| 23 | + </view> |
| 24 | + </scroll-view> |
| 25 | + </view> |
| 26 | +</template> |
| 27 | +<script> |
| 28 | + // 必须符合 UUID 格式 |
| 29 | + const DEVICE_UUID = 'A1B85ED2-D76F-4AE4-8F5D-6CCF53E4E228'; |
| 30 | + export default { |
| 31 | + data() { |
| 32 | + return { |
| 33 | + title: 'iBeacon', |
| 34 | + isOpen: false, |
| 35 | + isStarted: false, |
| 36 | + deviceList: [], |
| 37 | + isPageOpened: false |
| 38 | + }; |
| 39 | + }, |
| 40 | + onLoad() { |
| 41 | + this.onBeaconUpdate(); |
| 42 | + }, |
| 43 | + onShow() { |
| 44 | + this.isPageOpened = true; |
| 45 | + }, |
| 46 | + methods: { |
| 47 | + maskclose() { |
| 48 | + this.maskShow = false; |
| 49 | + }, |
| 50 | + openBluetoothAdapter() { |
| 51 | + uni.openBluetoothAdapter({ |
| 52 | + success: (res) => { |
| 53 | + console.log('初始化蓝牙成功:' + res.errMsg); |
| 54 | + console.log(res); |
| 55 | + this.isOpen = true; |
| 56 | + this.deviceList = []; |
| 57 | + }, |
| 58 | + fail: (err) => { |
| 59 | + console.log('初始化蓝牙失败,错误码:' + (err.errCode || err.errMsg)); |
| 60 | + if (err.errCode !== 0) { |
| 61 | + initTypes(err.errCode, err.errMsg); |
| 62 | + } |
| 63 | + } |
| 64 | + }); |
| 65 | + }, |
| 66 | + closeBluetoothAdapter(OBJECT) { |
| 67 | + uni.closeBluetoothAdapter({ |
| 68 | + success: (res) => { |
| 69 | + this.isOpen = false; |
| 70 | + console.log('断开蓝牙模块成功'); |
| 71 | + } |
| 72 | + }); |
| 73 | + }, |
| 74 | + onBeaconUpdate() { |
| 75 | + plus.ibeacon.onBeaconUpdate(res => { |
| 76 | + if (!this.isPageOpened || !this.isOpen || !this.isStarted) { |
| 77 | + return; |
| 78 | + } |
| 79 | + if (res.code !== 0) { |
| 80 | + console.log(res); |
| 81 | + return; |
| 82 | + } |
| 83 | + if (res.beacons && res.beacons.length > 0) { |
| 84 | + this.getBeacons(); |
| 85 | + } else if (!res.connected) { |
| 86 | + this.deviceList = []; |
| 87 | + } |
| 88 | + }); |
| 89 | + }, |
| 90 | + startBeaconDiscovery() { |
| 91 | + plus.ibeacon.startBeaconDiscovery({ |
| 92 | + uuids: [], |
| 93 | + success: (res) => { |
| 94 | + this.isStarted = true; |
| 95 | + console.log(res); |
| 96 | + }, |
| 97 | + fail: (err) => { |
| 98 | + console.error(err); |
| 99 | + } |
| 100 | + }); |
| 101 | + }, |
| 102 | + stopBeaconDiscovery(types) { |
| 103 | + uni.stopBeaconDiscovery({ |
| 104 | + success: (res) => { |
| 105 | + this.isStarted = false; |
| 106 | + }, |
| 107 | + fail: (err) => { |
| 108 | + console.error(err); |
| 109 | + } |
| 110 | + }); |
| 111 | + }, |
| 112 | + getBeacons() { |
| 113 | + uni.getBeacons({ |
| 114 | + success: (res) => { |
| 115 | + if (res.beacons && res.beacons.length > 0) { |
| 116 | + console.log(res.beacons); |
| 117 | + this.deviceList = res.beacons; |
| 118 | + } |
| 119 | + }, |
| 120 | + fail: (err) => { |
| 121 | + console.log('获取设备服务失败,错误码:' + err.errCode); |
| 122 | + if (errCode.errCode !== 0) { |
| 123 | + initTypes(errCode.errCode); |
| 124 | + } |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + }, |
| 129 | + onUnload() { |
| 130 | + this.isPageOpened = false; |
| 131 | + } |
| 132 | + }; |
| 133 | +
|
| 134 | + /** |
| 135 | + * 判断初始化蓝牙状态 |
| 136 | + */ |
| 137 | + function initTypes(code, errMsg) { |
| 138 | + switch (code) { |
| 139 | + case 10000: |
| 140 | + toast('未初始化蓝牙适配器'); |
| 141 | + break; |
| 142 | + case 10001: |
| 143 | + toast('未检测到蓝牙,请打开蓝牙重试!'); |
| 144 | + break; |
| 145 | + case 10002: |
| 146 | + toast('没有找到指定设备'); |
| 147 | + break; |
| 148 | + case 10003: |
| 149 | + toast('连接失败'); |
| 150 | + break; |
| 151 | + case 10004: |
| 152 | + toast('没有找到指定服务'); |
| 153 | + break; |
| 154 | + case 10005: |
| 155 | + toast('没有找到指定特征值'); |
| 156 | + break; |
| 157 | + case 10006: |
| 158 | + toast('当前连接已断开'); |
| 159 | + break; |
| 160 | + case 10007: |
| 161 | + toast('当前特征值不支持此操作'); |
| 162 | + break; |
| 163 | + case 10008: |
| 164 | + toast('其余所有系统上报的异常'); |
| 165 | + break; |
| 166 | + case 10009: |
| 167 | + toast('Android 系统特有,系统版本低于 4.3 不支持 BLE'); |
| 168 | + break; |
| 169 | + default: |
| 170 | + toast(errMsg); |
| 171 | + } |
| 172 | + } |
| 173 | +
|
| 174 | + /** |
| 175 | + * 弹出框封装 |
| 176 | + */ |
| 177 | + function toast(content, showCancel = false) { |
| 178 | + uni.showModal({ |
| 179 | + title: '提示', |
| 180 | + content, |
| 181 | + showCancel |
| 182 | + }); |
| 183 | + } |
| 184 | +</script> |
| 185 | + |
| 186 | +<style> |
| 187 | + .uni-title { |
| 188 | + /* width: 100%; */ |
| 189 | + /* height: 80upx; */ |
| 190 | + text-align: center; |
| 191 | + } |
| 192 | +
|
| 193 | + .uni-mask { |
| 194 | + position: fixed; |
| 195 | + top: 0; |
| 196 | + left: 0; |
| 197 | + bottom: 0; |
| 198 | + display: flex; |
| 199 | + align-items: center; |
| 200 | + width: 100%; |
| 201 | + background: rgba(0, 0, 0, 0.6); |
| 202 | + padding: 0 30upx; |
| 203 | + box-sizing: border-box; |
| 204 | + } |
| 205 | +
|
| 206 | + .uni-scroll_box { |
| 207 | + height: 70%; |
| 208 | + background: #fff; |
| 209 | + border-radius: 20upx; |
| 210 | + } |
| 211 | +
|
| 212 | + .uni-list-box { |
| 213 | + margin: 0 20upx; |
| 214 | + padding: 15upx 0; |
| 215 | + border-bottom: 1px #f5f5f5 solid; |
| 216 | + box-sizing: border-box; |
| 217 | + } |
| 218 | +
|
| 219 | + .uni-list:last-child { |
| 220 | + border: none; |
| 221 | + } |
| 222 | +
|
| 223 | + .uni-list_name { |
| 224 | + font-size: 30upx; |
| 225 | + color: #333; |
| 226 | + } |
| 227 | +
|
| 228 | + .uni-list_item { |
| 229 | + font-size: 24upx; |
| 230 | + color: #555; |
| 231 | + line-height: 1.5; |
| 232 | + } |
| 233 | +
|
| 234 | + .uni-success_box { |
| 235 | + position: absolute; |
| 236 | + left: 0; |
| 237 | + bottom: 0; |
| 238 | + min-height: 100upx; |
| 239 | + width: 100%; |
| 240 | + background: #fff; |
| 241 | + box-sizing: border-box; |
| 242 | + border-top: 1px #eee solid; |
| 243 | + } |
| 244 | +
|
| 245 | + .uni-success_sub { |
| 246 | + /* width: 100%%; */ |
| 247 | + height: 100upx; |
| 248 | + display: flex; |
| 249 | + justify-content: space-between; |
| 250 | + align-items: center; |
| 251 | + padding: 0 30upx; |
| 252 | + } |
| 253 | +
|
| 254 | + .uni-close_button { |
| 255 | + padding: 0 20upx; |
| 256 | + height: 60upx; |
| 257 | + line-height: 60upx; |
| 258 | + background: #ce3c39; |
| 259 | + color: #ffffff; |
| 260 | + border-radius: 10upx; |
| 261 | + } |
| 262 | +
|
| 263 | + .uni-success_content { |
| 264 | + height: 600upx; |
| 265 | + margin: 30upx; |
| 266 | + margin-top: 0; |
| 267 | + border: 1px #eee solid; |
| 268 | + padding: 30upx; |
| 269 | + } |
| 270 | +
|
| 271 | + .uni-content_list { |
| 272 | + padding-bottom: 10upx; |
| 273 | + border-bottom: 1px #f5f5f5 solid; |
| 274 | + } |
| 275 | +
|
| 276 | + .uni-tips { |
| 277 | + text-align: center; |
| 278 | + font-size: 24upx; |
| 279 | + color: #666; |
| 280 | + } |
| 281 | +</style> |
0 commit comments