|
@@ -50,31 +50,7 @@ class bleManager {
|
|
|
that.dissmissDevice = [];
|
|
|
getCurrentPages()[0].closeBlueResetOffline(false, true);
|
|
|
}
|
|
|
- })
|
|
|
- if (that.isAvailable) {
|
|
|
- return;
|
|
|
- }
|
|
|
- wx.openBluetoothAdapter({
|
|
|
- success: function (res) {
|
|
|
- that.isAvailable = res.available;
|
|
|
- },
|
|
|
- fail: function (res) {
|
|
|
- that.isAvailable = res.available;
|
|
|
- console.log("打开蓝牙成功2", res);
|
|
|
- // wx.showModal({
|
|
|
- // title: '提示',
|
|
|
- // content: '请检查手机蓝牙是否打开',
|
|
|
- // showCancel: false,
|
|
|
- // success: function (res) { }
|
|
|
- // });
|
|
|
-
|
|
|
- // that.doStartScaning = false;
|
|
|
- // that.requestBlueTime = that.getCurrentMills();
|
|
|
- // if (boolean != null) {
|
|
|
- // boolean(false);
|
|
|
- // }
|
|
|
- }
|
|
|
- })
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
///监听搜索设备列表
|
|
@@ -297,9 +273,17 @@ class bleManager {
|
|
|
var that = this;
|
|
|
|
|
|
///限制搜索没有打开蓝牙一直询问打开蓝牙
|
|
|
- var isAvailable = that.isAvailable;
|
|
|
- if (!isAvailable && callBackConnect == null) {
|
|
|
- return;
|
|
|
+ if (!that.isAvailable) {
|
|
|
+ if (callBackConnect == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ ///连接设备
|
|
|
+ var isAvailable = await that.openBluetoothAdapter();
|
|
|
+ if (!isAvailable) {
|
|
|
+ boolean(false);
|
|
|
+ return;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
const route_util = require('../utils/route_util');
|
|
@@ -337,6 +321,7 @@ class bleManager {
|
|
|
that.doStartScaning = false;
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
wx.stopBluetoothDevicesDiscovery({
|
|
|
success: (res) => {
|
|
|
console.log("停止搜索设备0", res);
|
|
@@ -351,26 +336,52 @@ class bleManager {
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
+ }
|
|
|
|
|
|
- // wx.openBluetoothAdapter({
|
|
|
- // success: function (res) {
|
|
|
-
|
|
|
- // },
|
|
|
- // fail: function (res) {
|
|
|
- // console.log("打开蓝牙成功2", res);
|
|
|
- // // wx.showModal({
|
|
|
- // // title: '提示',
|
|
|
- // // content: '请检查手机蓝牙是否打开',
|
|
|
- // // showCancel: false,
|
|
|
- // // success: function (res) { }
|
|
|
- // // });
|
|
|
- // that.doStartScaning = false;
|
|
|
- // that.requestBlueTime = that.getCurrentMills();
|
|
|
- // if (boolean != null) {
|
|
|
- // boolean(false);
|
|
|
- // }
|
|
|
- // }
|
|
|
- // })
|
|
|
+ ///打开蓝牙适配
|
|
|
+ openBluetoothAdapter() {
|
|
|
+ var that = this;
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ wx.openBluetoothAdapter({
|
|
|
+ success: function (res) {
|
|
|
+ that.isAvailable = res.available;
|
|
|
+ console.log("sdfasdfadsf==00==" + that.isAvailable + "===" + JSON.stringify(res));
|
|
|
+ resolve(that.isAvailable);
|
|
|
+ },
|
|
|
+ fail: function (res) {
|
|
|
+ console.log("sdfasdfadsf==11==" + that.isAvailable + "===" + JSON.stringify(res));
|
|
|
+ if (res.errno == 103) {
|
|
|
+ wx.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: '需要您的蓝牙权限才能使用此功能,是否前往设置授权?',
|
|
|
+ showCancel: true,
|
|
|
+ success: (modalRes) => {
|
|
|
+ if (modalRes.confirm) {
|
|
|
+ wx.openSetting({
|
|
|
+ success: (settingRes) => {
|
|
|
+ console.log("sdfasdfadsf==222==" + that.isAvailable + "===" + JSON.stringify(settingRes));
|
|
|
+ if (settingRes.authSetting['scope.bluetooth']) {
|
|
|
+ that.isAvailable = true;
|
|
|
+ resolve(that.isAvailable);
|
|
|
+ } else {
|
|
|
+ that.isAvailable = false;
|
|
|
+ resolve(that.isAvailable);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else if (modalRes.cancel) {
|
|
|
+ that.isAvailable = false;
|
|
|
+ resolve(that.isAvailable);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ that.isAvailable = false;
|
|
|
+ resolve(that.isAvailable);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
search(connectWillDevice, boolean, callBackConnect) {
|
|
@@ -624,7 +635,7 @@ class bleManager {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- connect() { }
|
|
|
+ connect() {}
|
|
|
|
|
|
// 发现特征值 read / write
|
|
|
discoverCharacteristics(deviceId, serviceId) {
|
|
@@ -724,4 +735,4 @@ class bleManager {
|
|
|
// const ble = new bleManager();
|
|
|
|
|
|
// 导出 bleManager 类
|
|
|
-module.exports = bleManager;
|
|
|
+module.exports = bleManager;
|