|
@@ -10,82 +10,159 @@ class bleManager {
|
|
}
|
|
}
|
|
|
|
|
|
// 初始化蓝牙适配器
|
|
// 初始化蓝牙适配器
|
|
- async initBluetoothAdapter(callback) {
|
|
|
|
|
|
+ initBluetoothAdapter() {
|
|
var that = this
|
|
var that = this
|
|
|
|
+ wx.openBluetoothAdapter({
|
|
|
|
+ success: (res) => {
|
|
|
|
+ that.isAvailable = true;
|
|
|
|
+ },
|
|
|
|
+ fail: (err) => {
|
|
|
|
+ that.isAvailable = false;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ wx.onBluetoothAdapterStateChange(function (res) {
|
|
|
|
+ that.isAvailable = res.available;
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 获取蓝牙权限
|
|
|
|
+ async getBluetoothPermission() {
|
|
|
|
+ var that = this;
|
|
|
|
+ console.log("gadsfasdfqwwerqewrqr");
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
- wx.openBluetoothAdapter({
|
|
|
|
- success: (res) => {
|
|
|
|
- that.isAvailable = true;
|
|
|
|
- if (callback) {
|
|
|
|
- callback(true);
|
|
|
|
- }
|
|
|
|
- resolve(true);
|
|
|
|
|
|
+ wx.getSetting({
|
|
|
|
+ success(res) {
|
|
|
|
+ if (res.authSetting["scope.bluetooth"]) {
|
|
|
|
+ that.hasPermission = true;
|
|
|
|
+ getApp().globalData.scopeBluetooth = true;
|
|
|
|
+ resolve(true);
|
|
|
|
+ } else if (res.authSetting["scope.bluetooth"] === undefined) {
|
|
|
|
+ that.hasPermission = false;
|
|
|
|
+ getApp().globalData.scopeBluetooth = false;
|
|
|
|
+ resolve(false);
|
|
|
|
+ wx.authorize({
|
|
|
|
+ scope: "scope.bluetooth",
|
|
|
|
+ complete() {
|
|
|
|
+ that.getBluetoothPermission()
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ that.hasPermission = false;
|
|
|
|
+ that.globalData.scopeBluetooth = false;
|
|
|
|
+ wx.showModal({
|
|
|
|
+ title: '请打开系统蓝牙进行配网',
|
|
|
|
+ content: '如已打开蓝牙仍然弹框,请尝试重启小程序',
|
|
|
|
+ success(res) {
|
|
|
|
+ if (res.confirm) {
|
|
|
|
+ console.log('用户点击确定')
|
|
|
|
+ wx.openSetting({
|
|
|
|
+ complete() {}
|
|
|
|
+ })
|
|
|
|
+ } else if (res.cancel) {
|
|
|
|
+ console.log('用户点击取消')
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ resolve(false);
|
|
|
|
+ };
|
|
},
|
|
},
|
|
- fail: (err) => {
|
|
|
|
- that.isAvailable = false;
|
|
|
|
- console.log('adapterState fail, now is', err)
|
|
|
|
- if (callback) {
|
|
|
|
- callback(false);
|
|
|
|
|
|
+ fail(err) {
|
|
|
|
+ resolve(false);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ///获取定位权限
|
|
|
|
+ async getLocalSetting() {
|
|
|
|
+ var that = this;
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ wx.getSetting({
|
|
|
|
+ success(res) {
|
|
|
|
+ if (res.authSetting["scope.userLocation"]) {
|
|
|
|
+ resolve(true);
|
|
|
|
+ } else if (res.authSetting["scope.userLocation"] === undefined) {
|
|
|
|
+ wx.authorize({
|
|
|
|
+ scope: "scope.userLocation",
|
|
|
|
+ success() {
|
|
|
|
+ resolve(that.getLocalSetting());
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ wx.showModal({
|
|
|
|
+ title: '请打开系统位置获取定位权限',
|
|
|
|
+ success(res) {
|
|
|
|
+ if (res.confirm) {
|
|
|
|
+ wx.openSetting({
|
|
|
|
+ complete() {}
|
|
|
|
+ })
|
|
|
|
+ } else if (res.cancel) {}
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ reject(false);
|
|
}
|
|
}
|
|
|
|
+ },
|
|
|
|
+ fail(err) {
|
|
reject(false);
|
|
reject(false);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
- wx.onBluetoothAdapterStateChange(function (res) {
|
|
|
|
- console.log('adapterState changed, now is', res)
|
|
|
|
- // if (callback) {
|
|
|
|
- // callback(res.available ?? false);
|
|
|
|
- // }
|
|
|
|
- that.isAvailable = res.available;
|
|
|
|
- })
|
|
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
// 开始搜索蓝牙设备
|
|
// 开始搜索蓝牙设备
|
|
- async startScan(callBack, succeed) {
|
|
|
|
|
|
+ async startScan(callBack, boolean) {
|
|
var that = this;
|
|
var that = this;
|
|
- var open = await that.getLocalSetting();
|
|
|
|
- if (!open) {
|
|
|
|
- succeed(false);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
if (!that.isAvailable) {
|
|
if (!that.isAvailable) {
|
|
wx.showToast({
|
|
wx.showToast({
|
|
title: '请打开蓝牙',
|
|
title: '请打开蓝牙',
|
|
icon: 'none',
|
|
icon: 'none',
|
|
duration: 2000
|
|
duration: 2000
|
|
});
|
|
});
|
|
- succeed(false);
|
|
|
|
|
|
+ boolean(false);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- if (!that.hasPermission) {
|
|
|
|
|
|
+ var open = await that.getLocalSetting();
|
|
|
|
+ if (!open) {
|
|
wx.showToast({
|
|
wx.showToast({
|
|
- title: '请打开蓝牙权限',
|
|
|
|
|
|
+ title: '请打开定位权限',
|
|
icon: 'none',
|
|
icon: 'none',
|
|
duration: 2000
|
|
duration: 2000
|
|
});
|
|
});
|
|
- succeed(false);
|
|
|
|
|
|
+ boolean(false);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ ///没有权限再获取
|
|
|
|
+ if (!that.hasPermission) {
|
|
|
|
+ var per = await that.getBluetoothPermission();
|
|
|
|
+ if (!per) {
|
|
|
|
+ wx.showToast({
|
|
|
|
+ title: '请打开蓝牙权限',
|
|
|
|
+ icon: 'none',
|
|
|
|
+ duration: 2000
|
|
|
|
+ });
|
|
|
|
+ boolean(false);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
// services: ["ffc0", "ab00", "ffe5"],
|
|
// services: ["ffc0", "ab00", "ffe5"],
|
|
wx.startBluetoothDevicesDiscovery({
|
|
wx.startBluetoothDevicesDiscovery({
|
|
allowDuplicatesKey: true,
|
|
allowDuplicatesKey: true,
|
|
success: (res) => {
|
|
success: (res) => {
|
|
if (res) {
|
|
if (res) {
|
|
|
|
+ console.log("gadsfasdfqwerqwerdfasdfqr==000===");
|
|
wx.onBluetoothDeviceFound((devicesRes) => {
|
|
wx.onBluetoothDeviceFound((devicesRes) => {
|
|
for (var i = 0; i < devicesRes.devices.length; i++) {
|
|
for (var i = 0; i < devicesRes.devices.length; i++) {
|
|
if (devicesRes.devices[i].name != "") {
|
|
if (devicesRes.devices[i].name != "") {
|
|
if (devicesRes.devices[i].name === getApp().globalData.connectWillDevice.clientType) {
|
|
if (devicesRes.devices[i].name === getApp().globalData.connectWillDevice.clientType) {
|
|
|
|
+ console.log("gadsfasdfqwerqwerdfasdfqr=====" + devicesRes.devices[i].name);
|
|
callBack(devicesRes.devices[i]);
|
|
callBack(devicesRes.devices[i]);
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|
|
- } else {
|
|
|
|
- succeed(false);
|
|
|
|
}
|
|
}
|
|
},
|
|
},
|
|
fail: (err) => {
|
|
fail: (err) => {
|
|
@@ -96,21 +173,56 @@ class bleManager {
|
|
duration: 2000
|
|
duration: 2000
|
|
});
|
|
});
|
|
}
|
|
}
|
|
- succeed(false);
|
|
|
|
|
|
+ boolean(false);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 停止搜索
|
|
|
|
+ stopSearch() {
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ wx.stopBluetoothDevicesDiscovery({
|
|
|
|
+ success: (res) => {
|
|
|
|
+ console.log('停止搜索成功:', res);
|
|
|
|
+ resolve(res);
|
|
|
|
+ },
|
|
|
|
+ fail: (err) => {
|
|
|
|
+ console.error('停止搜索失败:', err);
|
|
|
|
+ reject(new Error('停止搜索失败'));
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
///获取已经连接过手机的设备
|
|
///获取已经连接过手机的设备
|
|
- getAllOnlineDevices(callBack) {
|
|
|
|
|
|
+ async getAllOnlineDevices(callBack) {
|
|
var that = this;
|
|
var that = this;
|
|
|
|
+ if (!that.isAvailable) {
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ that.getAllOnlineDevices(callBack);
|
|
|
|
+ }, 3 * 1000);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!that.hasPermission) {
|
|
|
|
+ var per = await that.getBluetoothPermission();
|
|
|
|
+ if (!per) {
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ that.getAllOnlineDevices(callBack);
|
|
|
|
+ }, 3 * 1000);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ that.getConnectedDevices();
|
|
wx.getBluetoothDevices({
|
|
wx.getBluetoothDevices({
|
|
success: (res) => {
|
|
success: (res) => {
|
|
- if (res && res.devices.length > 0) {
|
|
|
|
|
|
+ // && res.devices.length > 0
|
|
|
|
+ if (res) {
|
|
callBack(res.devices);
|
|
callBack(res.devices);
|
|
}
|
|
}
|
|
setTimeout(() => {
|
|
setTimeout(() => {
|
|
- that.getAllOnlineDevices();
|
|
|
|
|
|
+ that.getAllOnlineDevices(callBack);
|
|
}, 3 * 1000);
|
|
}, 3 * 1000);
|
|
},
|
|
},
|
|
fail: (err) => {
|
|
fail: (err) => {
|
|
@@ -121,129 +233,65 @@ class bleManager {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- // 获取获取在蓝牙模块生效期间所有搜索到的蓝牙设备。包括已经和本机处于连接状态的设备。
|
|
|
|
- getAllConnectedDevices() {
|
|
|
|
|
|
+ // 获取已连接的蓝牙设备
|
|
|
|
+ getConnectedDevices() {
|
|
var that = this;
|
|
var that = this;
|
|
if (!that.isAvailable && !that.hasPermission) {
|
|
if (!that.isAvailable && !that.hasPermission) {
|
|
return [];
|
|
return [];
|
|
}
|
|
}
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
- wx.getBluetoothDevices({
|
|
|
|
|
|
+ wx.getConnectedBluetoothDevices({
|
|
|
|
+ // services: ["FFC0", "ffc0", "FFC1", "FFC2", "ffc1", "ffc2", "AB00", "ab00", "AB01", "AB02", "FFF1", "fff1", "FFE2", "FFE5",],
|
|
|
|
+ // services: ["ab00", "ffe5", "1111", "FFC0", "FFC1", "FFF1", ],
|
|
|
|
+ // services: [],
|
|
|
|
+ // services: [
|
|
|
|
+ // "0000ab00-0000-1000-8000-00805f9b34fb",
|
|
|
|
+ // "0000ffc0-0000-1000-8000-00805f9b34fb",
|
|
|
|
+ // "0000FFF0-0000-1000-8000-00805F9B34FB",
|
|
|
|
+ // "0000FFF1-0000-1000-8000-00805F9B34FB",
|
|
|
|
+ // "0000FFE5-0000-1000-8000-00805F9B34FB",
|
|
|
|
+ // ],
|
|
success: (res) => {
|
|
success: (res) => {
|
|
|
|
+ console.log('已连接的蓝牙设备==11==:', newDevices);
|
|
let newDevices = that.fiterDevice(res)
|
|
let newDevices = that.fiterDevice(res)
|
|
|
|
+ console.log('已连接的蓝牙设备==22==:', newDevices);
|
|
resolve(newDevices);
|
|
resolve(newDevices);
|
|
},
|
|
},
|
|
fail: (err) => {
|
|
fail: (err) => {
|
|
- console.error('已扫描过的蓝牙设备失败:', err);
|
|
|
|
|
|
+ console.error('获取已连接的蓝牙设备失败:', err);
|
|
reject([]);
|
|
reject([]);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- // 停止搜索
|
|
|
|
- stopScan() {
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
|
- wx.stopBluetoothDevicesDiscovery({
|
|
|
|
- success: (res) => {
|
|
|
|
- console.log('停止搜索成功:', res);
|
|
|
|
- resolve(res);
|
|
|
|
- },
|
|
|
|
- fail: (err) => {
|
|
|
|
- console.error('停止搜索失败:', err);
|
|
|
|
- reject(new Error('停止搜索失败'));
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 检查蓝牙权限
|
|
|
|
- async checkBluetoothPermission(callback) {
|
|
|
|
- // 获取蓝牙权限
|
|
|
|
- var that = this;
|
|
|
|
- const _app = getApp();
|
|
|
|
- wx.getSetting({
|
|
|
|
- success(res) {
|
|
|
|
- if (res.authSetting["scope.bluetooth"]) {
|
|
|
|
- _app.globalData.scopeBluetooth = true;
|
|
|
|
- that.hasPermission = true;
|
|
|
|
- if (callback) {
|
|
|
|
- callback(true);
|
|
|
|
- }
|
|
|
|
- } else if (res.authSetting["scope.bluetooth"] === undefined) {
|
|
|
|
- _app.globalData.scopeBluetooth = false;
|
|
|
|
- that.hasPermission = false;
|
|
|
|
- if (callback) {
|
|
|
|
- callback(false);
|
|
|
|
- }
|
|
|
|
- wx.authorize({
|
|
|
|
- scope: "scope.bluetooth",
|
|
|
|
- complete() {
|
|
|
|
- that.checkBluetoothPermission()
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- that.globalData.scopeBluetooth = false;
|
|
|
|
- that.hasPermission = false;
|
|
|
|
- if (callback) {
|
|
|
|
- callback(false);
|
|
|
|
- }
|
|
|
|
- wx.showModal({
|
|
|
|
- title: '请打开系统蓝牙进行配网',
|
|
|
|
- content: '如已打开蓝牙仍然弹框,请尝试重启小程序',
|
|
|
|
- success(res) {
|
|
|
|
- if (res.confirm) {
|
|
|
|
- console.log('用户点击确定')
|
|
|
|
- wx.openSetting({
|
|
|
|
- complete() {}
|
|
|
|
- })
|
|
|
|
- } else if (res.cancel) {
|
|
|
|
- console.log('用户点击取消')
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- };
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- closeBle() {
|
|
|
|
- console.log('关闭蓝牙了')
|
|
|
|
- wx.closeBluetoothAdapter();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 获取已连接的蓝牙设备
|
|
|
|
- getConnectedDevices() {
|
|
|
|
|
|
+ // 获取获取在蓝牙模块生效期间所有搜索到的蓝牙设备。包括已经和本机处于连接状态的设备。
|
|
|
|
+ getAllConnectedDevices() {
|
|
var that = this;
|
|
var that = this;
|
|
if (!that.isAvailable && !that.hasPermission) {
|
|
if (!that.isAvailable && !that.hasPermission) {
|
|
return [];
|
|
return [];
|
|
}
|
|
}
|
|
|
|
+
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
- wx.getConnectedBluetoothDevices({
|
|
|
|
- // services: ["FFC0", "ffc0", "FFC1", "FFC2", "ffc1", "ffc2", "AB00", "ab00", "AB01", "AB02", "FFF1", "fff1", "FFE2", "FFE5",],
|
|
|
|
- services: ["ab00", "ffe5", "1111", "FFC0", "FFC1", "FFF1", ],
|
|
|
|
- // services: [],
|
|
|
|
- // services: [
|
|
|
|
- // "0000ab00-0000-1000-8000-00805f9b34fb",
|
|
|
|
- // "0000ffc0-0000-1000-8000-00805f9b34fb",
|
|
|
|
- // "0000FFF0-0000-1000-8000-00805F9B34FB",
|
|
|
|
- // "0000FFF1-0000-1000-8000-00805F9B34FB",
|
|
|
|
- // "0000FFE5-0000-1000-8000-00805F9B34FB",
|
|
|
|
- // ],
|
|
|
|
|
|
+ wx.getBluetoothDevices({
|
|
success: (res) => {
|
|
success: (res) => {
|
|
let newDevices = that.fiterDevice(res)
|
|
let newDevices = that.fiterDevice(res)
|
|
- console.log('已连接的蓝牙设备:', newDevices);
|
|
|
|
resolve(newDevices);
|
|
resolve(newDevices);
|
|
},
|
|
},
|
|
fail: (err) => {
|
|
fail: (err) => {
|
|
- console.error('获取已连接的蓝牙设备失败:', err);
|
|
|
|
|
|
+ console.error('已扫描过的蓝牙设备失败:', err);
|
|
reject([]);
|
|
reject([]);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ closeBle() {
|
|
|
|
+ console.log('关闭蓝牙了')
|
|
|
|
+ wx.closeBluetoothAdapter();
|
|
|
|
+ }
|
|
|
|
+
|
|
// 断开与指定设备的连接
|
|
// 断开与指定设备的连接
|
|
disconnect() {
|
|
disconnect() {
|
|
var that = this;
|
|
var that = this;
|
|
@@ -303,42 +351,6 @@ class bleManager {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- ///获取定位权限
|
|
|
|
- async getLocalSetting() {
|
|
|
|
- var that = this;
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
|
- wx.getSetting({
|
|
|
|
- success(res) {
|
|
|
|
- if (res.authSetting["scope.userLocation"]) {
|
|
|
|
- console.log("有定位权限")
|
|
|
|
- resolve(true);
|
|
|
|
- } else if (res.authSetting["scope.userLocation"] === undefined) {
|
|
|
|
- wx.authorize({
|
|
|
|
- scope: "scope.userLocation",
|
|
|
|
- success() {
|
|
|
|
- resolve(that.getLocalSetting());
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- wx.showModal({
|
|
|
|
- title: '请打开系统位置获取定位权限',
|
|
|
|
- success(res) {
|
|
|
|
- if (res.confirm) {
|
|
|
|
- wx.openSetting({
|
|
|
|
- complete() {}
|
|
|
|
- })
|
|
|
|
- } else if (res.cancel) {}
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
- console.log("没有有定位权限")
|
|
|
|
- reject(false);
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- fail(err) {}
|
|
|
|
- });
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
// 监听发现新设备的事件
|
|
// 监听发现新设备的事件
|
|
onBluetoothDeviceFound(callback) {
|
|
onBluetoothDeviceFound(callback) {
|
|
var that = this;
|
|
var that = this;
|