|
@@ -1,18 +1,103 @@
|
|
|
// 引入必要的微信小程序 API
|
|
|
// const wx = require('wx');
|
|
|
-
|
|
|
class bleManager {
|
|
|
constructor() {
|
|
|
var that = this;
|
|
|
- that.hasPermission = false;
|
|
|
that.isAvailable = false;
|
|
|
+ that.hasPermission = false;
|
|
|
that.scanDevices = [];
|
|
|
that.device = null;
|
|
|
}
|
|
|
|
|
|
+ // 初始化蓝牙适配器
|
|
|
+ async initBluetoothAdapter(callback) {
|
|
|
+ var that = this
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ wx.openBluetoothAdapter({
|
|
|
+ success: (res) => {
|
|
|
+ that.isAvailable = true;
|
|
|
+ if (callback) {
|
|
|
+ callback(true);
|
|
|
+ }
|
|
|
+ resolve(true);
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ that.isAvailable = false;
|
|
|
+ console.log('adapterState fail, now is', err)
|
|
|
+ if (callback) {
|
|
|
+ callback(false);
|
|
|
+ }
|
|
|
+ reject(false);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ wx.onBluetoothAdapterStateChange(function (res) {
|
|
|
+ // console.log('adapterState changed, now is', res)
|
|
|
+ // if (callback) {
|
|
|
+ // callback(res.available ?? false);
|
|
|
+ // }
|
|
|
+ that.isAvailable = res.available ?? false
|
|
|
+ })
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 开始搜索蓝牙设备
|
|
|
+ async startScan(success, fail) {
|
|
|
+ var that = this;
|
|
|
+ var open = await that.getLocalSetting();
|
|
|
+ if (!open) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (!that.isAvailable) {
|
|
|
+ wx.showToast({
|
|
|
+ title: '请打开蓝牙',
|
|
|
+ icon: 'none',
|
|
|
+ duration: 2000
|
|
|
+ });
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!that.hasPermission) {
|
|
|
+ wx.showToast({
|
|
|
+ title: '请打开蓝牙权限',
|
|
|
+ icon: 'none',
|
|
|
+ duration: 2000
|
|
|
+ });
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ wx.startBluetoothDevicesDiscovery({
|
|
|
+ allowDuplicatesKey: false,
|
|
|
+ // services: ["ffc0", "ab00", "ffe5"],
|
|
|
+ success: (res) => {
|
|
|
+ console.log('蓝牙设备搜索已启动:', res);
|
|
|
+ if (res) {
|
|
|
+ wx.onBluetoothDeviceFound((devicesRes) => {
|
|
|
+ devicesRes.devices.forEach(device => {
|
|
|
+ if (device.name) {
|
|
|
+ console.log("gadsfadfqwerqwerqr==aaa====" + device.name + "===" + device.deviceId);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ if (fail) {
|
|
|
+ if (err.errno == 1509008) {
|
|
|
+ wx.showToast({
|
|
|
+ title: '搜索蓝牙需要先开启定位权限',
|
|
|
+ icon: 'none',
|
|
|
+ duration: 2000
|
|
|
+ });
|
|
|
+ }
|
|
|
+ fail(err);
|
|
|
+ }
|
|
|
+ console.log('启动蓝牙设备搜索失败', err ?? "err is null");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
// 检查蓝牙权限
|
|
|
async checkBluetoothPermission(callback) {
|
|
|
- // return new Promise((resolve, reject) => {
|
|
|
// 获取蓝牙权限
|
|
|
var that = this;
|
|
|
const _app = getApp();
|
|
@@ -21,12 +106,9 @@ class bleManager {
|
|
|
if (res.authSetting["scope.bluetooth"]) {
|
|
|
_app.globalData.scopeBluetooth = true;
|
|
|
that.hasPermission = true;
|
|
|
- // console.log('checkBluetoothPermission success, now is', res)
|
|
|
if (callback) {
|
|
|
callback(true);
|
|
|
}
|
|
|
- // resolve(true);
|
|
|
-
|
|
|
} else if (res.authSetting["scope.bluetooth"] === undefined) {
|
|
|
_app.globalData.scopeBluetooth = false;
|
|
|
that.hasPermission = false;
|
|
@@ -37,11 +119,8 @@ class bleManager {
|
|
|
scope: "scope.bluetooth",
|
|
|
complete() {
|
|
|
that.checkBluetoothPermission()
|
|
|
- // resolve(that.checkBluetoothPermission());
|
|
|
-
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
} else {
|
|
|
that.globalData.scopeBluetooth = false;
|
|
|
that.hasPermission = false;
|
|
@@ -55,53 +134,16 @@ class bleManager {
|
|
|
if (res.confirm) {
|
|
|
console.log('用户点击确定')
|
|
|
wx.openSetting({
|
|
|
- complete() {
|
|
|
- // that.getBluetoothStatus();
|
|
|
- }
|
|
|
+ complete() {}
|
|
|
})
|
|
|
} else if (res.cancel) {
|
|
|
console.log('用户点击取消')
|
|
|
}
|
|
|
}
|
|
|
- })
|
|
|
- // resolve(false);
|
|
|
+ });
|
|
|
};
|
|
|
}
|
|
|
})
|
|
|
- // })
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- // 初始化蓝牙适配器
|
|
|
- async initBluetoothAdapter(callback) {
|
|
|
- var that = this
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- wx.openBluetoothAdapter({
|
|
|
- success: (res) => {
|
|
|
- that.isAvailable = true;
|
|
|
- // console.log('adapterState success, now is', res)
|
|
|
- if (callback) {
|
|
|
- callback(true);
|
|
|
- }
|
|
|
- resolve(true);
|
|
|
- },
|
|
|
- fail: (err) => {
|
|
|
- that.isAvailable = false;
|
|
|
- console.log('adapterState fail, now is', err)
|
|
|
- if (callback) {
|
|
|
- callback(false);
|
|
|
- }
|
|
|
- reject(false);
|
|
|
- }
|
|
|
- });
|
|
|
- wx.onBluetoothAdapterStateChange(function (res) {
|
|
|
- console.log('adapterState changed, now is', res)
|
|
|
- if (callback) {
|
|
|
- callback(res.available ?? false);
|
|
|
- }
|
|
|
- that.isAvailable = res.available ?? false
|
|
|
- })
|
|
|
- });
|
|
|
}
|
|
|
|
|
|
closeBle() {
|
|
@@ -154,7 +196,6 @@ class bleManager {
|
|
|
// name: device.name || device.localName
|
|
|
// }));
|
|
|
let newDevices = that.fiterDevice(res)
|
|
|
-
|
|
|
resolve(newDevices);
|
|
|
},
|
|
|
fail: (err) => {
|
|
@@ -224,94 +265,39 @@ class bleManager {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- async getSetting() {
|
|
|
+ ///获取定位权限
|
|
|
+ async getLocalSetting() {
|
|
|
var that = this;
|
|
|
return new Promise((resolve, reject) => {
|
|
|
wx.getSetting({
|
|
|
success(res) {
|
|
|
- if (res.authSetting["scope.userFuzzyLocation"]) {
|
|
|
- // 成功
|
|
|
- // that.getBluetoothStatus();
|
|
|
+ if (res.authSetting["scope.userLocation"]) {
|
|
|
console.log("有定位权限")
|
|
|
resolve(true);
|
|
|
- } else if (res.authSetting["scope.userFuzzyLocation"] === undefined) {
|
|
|
-
|
|
|
+ } else if (res.authSetting["scope.userLocation"] === undefined) {
|
|
|
wx.authorize({
|
|
|
- scope: "scope.userFuzzyLocation",
|
|
|
+ scope: "scope.userLocation",
|
|
|
success() {
|
|
|
- console.log("再次获取定位权限")
|
|
|
- resolve(that.getSetting());
|
|
|
-
|
|
|
+ resolve(that.getLocalSetting());
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
-
|
|
|
wx.showModal({
|
|
|
title: '请打开系统位置获取定位权限',
|
|
|
success(res) {
|
|
|
if (res.confirm) {
|
|
|
- console.log('用户点击确定')
|
|
|
wx.openSetting({
|
|
|
- complete() {
|
|
|
- // that.getSetting();
|
|
|
- // resolve(that.getSetting());
|
|
|
-
|
|
|
- }
|
|
|
+ complete() {}
|
|
|
})
|
|
|
- } else if (res.cancel) {
|
|
|
- console.log('用户点击取消');
|
|
|
- }
|
|
|
+ } else if (res.cancel) {}
|
|
|
}
|
|
|
})
|
|
|
console.log("没有有定位权限")
|
|
|
reject(false);
|
|
|
-
|
|
|
}
|
|
|
- }
|
|
|
- })
|
|
|
-
|
|
|
- });
|
|
|
-
|
|
|
- }
|
|
|
- // 开始搜索蓝牙设备
|
|
|
- async startScan(success, fail) {
|
|
|
- var that = this;
|
|
|
- let open = await that.getSetting()
|
|
|
- if (!open) {
|
|
|
- if (fail) {
|
|
|
- fail("需要先开启定位");
|
|
|
- }
|
|
|
- console.log('需要先开启定位');
|
|
|
- }
|
|
|
- if (!that.isAvailable && !that.hasPermission) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- wx.startBluetoothDevicesDiscovery({
|
|
|
- // services: ["ffc0", "ab00", "ffe5"],
|
|
|
- success: (res) => {
|
|
|
- // that.onBluetoothDeviceFound();
|
|
|
- console.log('蓝牙设备搜索已启动:', res);
|
|
|
- if (success) {
|
|
|
- success(res);
|
|
|
- }
|
|
|
- // resolve(res);
|
|
|
- },
|
|
|
- fail: (err) => {
|
|
|
- if (fail) {
|
|
|
- if (err.errno == 1509008) {
|
|
|
- wx.showToast({
|
|
|
- title: '搜索蓝牙需要先开启定位权限',
|
|
|
- icon: 'none',
|
|
|
- duration: 2000
|
|
|
- })
|
|
|
- }
|
|
|
- fail(err);
|
|
|
- }
|
|
|
- console.log('启动蓝牙设备搜索失败', err ?? "err is null");
|
|
|
-
|
|
|
- // reject(new Error('启动蓝牙设备搜索失败', err ?? "err is null"));
|
|
|
- }
|
|
|
+ },
|
|
|
+ fail(err) {}
|
|
|
+ });
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -564,4 +550,42 @@ class bleManager {
|
|
|
// const ble = new bleManager();
|
|
|
|
|
|
// 导出 bleManager 类
|
|
|
-module.exports = bleManager;
|
|
|
+module.exports = bleManager;
|
|
|
+
|
|
|
+// wx.getSetting({
|
|
|
+// success(res) {
|
|
|
+// if (res.authSetting["scope.userFuzzyLocation"]) {
|
|
|
+// // 成功
|
|
|
+// // that.getBluetoothStatus();
|
|
|
+// console.log("有定位权限")
|
|
|
+// resolve(true);
|
|
|
+// } else if (res.authSetting["scope.userFuzzyLocation"] === undefined) {
|
|
|
+// wx.authorize({
|
|
|
+// scope: "scope.userFuzzyLocation",
|
|
|
+// success() {
|
|
|
+// console.log("再次获取定位权限")
|
|
|
+// resolve(that.getSetting());
|
|
|
+// }
|
|
|
+// });
|
|
|
+// } else {
|
|
|
+// wx.showModal({
|
|
|
+// title: '请打开系统位置获取定位权限',
|
|
|
+// success(res) {
|
|
|
+// if (res.confirm) {
|
|
|
+// console.log('用户点击确定')
|
|
|
+// wx.openSetting({
|
|
|
+// complete() {
|
|
|
+// // that.getSetting();
|
|
|
+// // resolve(that.getSetting());
|
|
|
+// }
|
|
|
+// })
|
|
|
+// } else if (res.cancel) {
|
|
|
+// console.log('用户点击取消');
|
|
|
+// }
|
|
|
+// }
|
|
|
+// })
|
|
|
+// console.log("没有有定位权限")
|
|
|
+// reject(false);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// })
|