|
@@ -12,7 +12,9 @@
|
|
<view class="head">
|
|
<view class="head">
|
|
<view class="title">蓝牙设备列表</view>
|
|
<view class="title">蓝牙设备列表</view>
|
|
<u-loading-icon v-if="beSearching" size="21" />
|
|
<u-loading-icon v-if="beSearching" size="21" />
|
|
- <view v-else class="anew" @click="startBluetooth"> 继续搜索 </view>
|
|
|
|
|
|
+ <view v-else class="anew" @click="startBluetooth(services)">
|
|
|
|
+ 继续搜索
|
|
|
|
+ </view>
|
|
</view>
|
|
</view>
|
|
<My_listbox ref="List" :pullDown="false">
|
|
<My_listbox ref="List" :pullDown="false">
|
|
<view class="device" v-for="item in devices" :key="item.deviceId">
|
|
<view class="device" v-for="item in devices" :key="item.deviceId">
|
|
@@ -90,8 +92,13 @@ export default {
|
|
};
|
|
};
|
|
},
|
|
},
|
|
onLoad(options) {
|
|
onLoad(options) {
|
|
- if (options.services) this.services = JSON.parse(options.services);
|
|
|
|
- this.startBluetooth();
|
|
|
|
|
|
+ if (options.services) {
|
|
|
|
+ this.services = JSON.parse(options.services);
|
|
|
|
+ this.startBluetooth(["FW01"]);
|
|
|
|
+ } else {
|
|
|
|
+ this.startBluetooth();
|
|
|
|
+ }
|
|
|
|
+
|
|
this.$refs.List.setHeight();
|
|
this.$refs.List.setHeight();
|
|
},
|
|
},
|
|
onUnload() {
|
|
onUnload() {
|
|
@@ -102,22 +109,21 @@ export default {
|
|
this.services = [];
|
|
this.services = [];
|
|
this.stopBluetooth();
|
|
this.stopBluetooth();
|
|
setTimeout(() => {
|
|
setTimeout(() => {
|
|
- this.startBluetooth();
|
|
|
|
|
|
+ this.startBluetooth([]);
|
|
});
|
|
});
|
|
},
|
|
},
|
|
openSetting() {
|
|
openSetting() {
|
|
uni.openSetting();
|
|
uni.openSetting();
|
|
},
|
|
},
|
|
/* 开始搜索 */
|
|
/* 开始搜索 */
|
|
- startBluetooth() {
|
|
|
|
|
|
+ startBluetooth(services = []) {
|
|
let that = this;
|
|
let that = this;
|
|
- this.connected = "";
|
|
|
|
uni.onBluetoothDeviceFound(function ({ devices }) {
|
|
uni.onBluetoothDeviceFound(function ({ devices }) {
|
|
that.devices = devices;
|
|
that.devices = devices;
|
|
that.empty = devices.length == 0;
|
|
that.empty = devices.length == 0;
|
|
});
|
|
});
|
|
uni.startBluetoothDevicesDiscovery({
|
|
uni.startBluetoothDevicesDiscovery({
|
|
- services: this.services,
|
|
|
|
|
|
+ // services,
|
|
success(res) {
|
|
success(res) {
|
|
that.beSearching = true;
|
|
that.beSearching = true;
|
|
that.$refs.List.setHeight();
|
|
that.$refs.List.setHeight();
|
|
@@ -148,7 +154,6 @@ export default {
|
|
that.connected = item.deviceId;
|
|
that.connected = item.deviceId;
|
|
uni.createBLEConnection({
|
|
uni.createBLEConnection({
|
|
deviceId: item.deviceId,
|
|
deviceId: item.deviceId,
|
|
- services: that.services || [],
|
|
|
|
success(res) {
|
|
success(res) {
|
|
if (res.errCode == 0) {
|
|
if (res.errCode == 0) {
|
|
uni.getBLEDeviceServices({
|
|
uni.getBLEDeviceServices({
|
|
@@ -181,21 +186,6 @@ export default {
|
|
device.characteristic = res.characteristics[0];
|
|
device.characteristic = res.characteristics[0];
|
|
const { write, notify } =
|
|
const { write, notify } =
|
|
device.characteristic.properties;
|
|
device.characteristic.properties;
|
|
- return uni.showModal({
|
|
|
|
- content: `${
|
|
|
|
- item.name || item.deviceId
|
|
|
|
- }连接成功\n是否进入设备操作页`,
|
|
|
|
- confirmText: "确认",
|
|
|
|
- cancelText: "重选",
|
|
|
|
- success: ({ confirm }) => {
|
|
|
|
- if (confirm) {
|
|
|
|
- console.log("设备连接完成");
|
|
|
|
- that.$Http.setBluetooth(device);
|
|
|
|
- } else {
|
|
|
|
- that.closeBLEConnection();
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- });
|
|
|
|
if (write && notify) {
|
|
if (write && notify) {
|
|
uni.showModal({
|
|
uni.showModal({
|
|
content: `${
|
|
content: `${
|