|
@@ -13,6 +13,8 @@ class bleManager {
|
|
that.doStartScaning = false;
|
|
that.doStartScaning = false;
|
|
that.compareList = [];
|
|
that.compareList = [];
|
|
that.dissmissDevice = [];
|
|
that.dissmissDevice = [];
|
|
|
|
+ that.stateChangeCallback = null;
|
|
|
|
+ that.isConnecting = false;
|
|
}
|
|
}
|
|
|
|
|
|
///获取比较的数据
|
|
///获取比较的数据
|
|
@@ -50,18 +52,20 @@ class bleManager {
|
|
that.dissmissDevice = [];
|
|
that.dissmissDevice = [];
|
|
getCurrentPages()[0].closeBlueResetOffline(false, true);
|
|
getCurrentPages()[0].closeBlueResetOffline(false, true);
|
|
}
|
|
}
|
|
- })
|
|
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
- ///监听搜索设备列表
|
|
|
|
|
|
+ ///监听搜索设备列表 监听设备列表
|
|
getBluetoothDevices() {
|
|
getBluetoothDevices() {
|
|
var that = this;
|
|
var that = this;
|
|
|
|
+ const hexUtil = require('../utils/hexUtil');
|
|
wx.onBluetoothDeviceFound(function (res) {
|
|
wx.onBluetoothDeviceFound(function (res) {
|
|
|
|
+ // console.log("蓝牙设备列表", res.length);
|
|
///第一种情况
|
|
///第一种情况
|
|
if (res.deviceId) {
|
|
if (res.deviceId) {
|
|
if (that.callBackConnect != null) {
|
|
if (that.callBackConnect != null) {
|
|
if (that.connectWillDevice != null && res.name == that.connectWillDevice.clientType) {
|
|
if (that.connectWillDevice != null && res.name == that.connectWillDevice.clientType) {
|
|
- res.mac = res.advertisData ? that.buf2hex(res.advertisData) : '';
|
|
|
|
|
|
+ res.mac = res.advertisData ? hexUtil.buf2hex(res.advertisData) : '';
|
|
// if (that.callBackConnect != null) {
|
|
// if (that.callBackConnect != null) {
|
|
console.log("1111", res.mac);
|
|
console.log("1111", res.mac);
|
|
if (that.callBackConnect != null) {
|
|
if (that.callBackConnect != null) {
|
|
@@ -98,33 +102,39 @@ class bleManager {
|
|
// console.log("2222", temp.name);
|
|
// console.log("2222", temp.name);
|
|
// }
|
|
// }
|
|
if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
|
|
if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
|
|
- temp.mac = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
|
|
|
|
|
|
+ temp.mac = temp.advertisData ? hexUtil.buf2hex(temp.advertisData) : '';
|
|
temp.mac2 = that.ab2hex(temp.advertisData ?? "")
|
|
temp.mac2 = that.ab2hex(temp.advertisData ?? "")
|
|
- // if (that.callBackConnect != null) {
|
|
|
|
- // console.log("2222:", temp.mac, temp.mac2);
|
|
|
|
- if (that.callBackConnect != null) {
|
|
|
|
- that.callBackConnect(temp);
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ // if (that.testData(temp)) {
|
|
|
|
+ // return
|
|
|
|
+ // }
|
|
|
|
+ console.log("2222:", temp.mac, ":", temp.mac2, ":", temp.deviceId, temp, temp.connectable);
|
|
|
|
+
|
|
|
|
+ // if ((temp.deviceId.indexOf("1f:de") ) > -1) {
|
|
|
|
+ // that.callBackConnect(temp);
|
|
|
|
+ // }
|
|
|
|
+ that.callBackConnect(temp);
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
for (var i = 0; i < res.devices.length; i++) {
|
|
for (var i = 0; i < res.devices.length; i++) {
|
|
- if (that.compareList.length > 0) {
|
|
|
|
- var has = false;
|
|
|
|
- for (var j = 0; j < that.compareList.length; j++) {
|
|
|
|
- if (res.devices[i].name != "") {
|
|
|
|
|
|
+ if (res.devices[i].name != "") {
|
|
|
|
+
|
|
|
|
+ if (that.compareList.length > 0) {
|
|
|
|
+ var has = false;
|
|
|
|
+ for (var j = 0; j < that.compareList.length; j++) {
|
|
if (res.devices[i].deviceId == that.compareList[j].deviceId) {
|
|
if (res.devices[i].deviceId == that.compareList[j].deviceId) {
|
|
has = true;
|
|
has = true;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
- if (!has) {
|
|
|
|
|
|
+ if (!has) {
|
|
|
|
+ that.compareList.push(res.devices[i]);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
that.compareList.push(res.devices[i]);
|
|
that.compareList.push(res.devices[i]);
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- that.compareList.push(res.devices[i]);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -133,12 +143,10 @@ class bleManager {
|
|
else if (res[0]) {
|
|
else if (res[0]) {
|
|
if (that.callBackConnect != null) {
|
|
if (that.callBackConnect != null) {
|
|
if (that.connectWillDevice != null && res[0].name == that.connectWillDevice.clientType) {
|
|
if (that.connectWillDevice != null && res[0].name == that.connectWillDevice.clientType) {
|
|
- res[0].mac = res[0].advertisData ? that.buf2hex(res[0].advertisData) : '';
|
|
|
|
|
|
+ res[0].mac = res[0].advertisData ? hexUtil.buf2hex(res[0].advertisData) : '';
|
|
// if (that.callBackConnect != null) {
|
|
// if (that.callBackConnect != null) {
|
|
console.log("3333", res.mac);
|
|
console.log("3333", res.mac);
|
|
-
|
|
|
|
if (that.callBackConnect != null) {
|
|
if (that.callBackConnect != null) {
|
|
-
|
|
|
|
that.callBackConnect(res[0]);
|
|
that.callBackConnect(res[0]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -162,9 +170,11 @@ class bleManager {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|
|
-
|
|
|
|
- ///监听已连接或没有连接
|
|
|
|
|
|
+ }
|
|
|
|
+ onBLEConnectionStateChange() {
|
|
|
|
+ ///监听已连接或没有连接 连接和断开的时候会回调
|
|
// {"deviceId":"E4:9F:80:09:40:EC","connected":false}
|
|
// {"deviceId":"E4:9F:80:09:40:EC","connected":false}
|
|
|
|
+ let that = this;
|
|
wx.onBLEConnectionStateChange((result) => {
|
|
wx.onBLEConnectionStateChange((result) => {
|
|
if (result.connected) {
|
|
if (result.connected) {
|
|
for (var i = 0; i < that.dissmissDevice.length; i++) {
|
|
for (var i = 0; i < that.dissmissDevice.length; i++) {
|
|
@@ -184,7 +194,17 @@ class bleManager {
|
|
if (!has) {
|
|
if (!has) {
|
|
that.dissmissDevice.push(result);
|
|
that.dissmissDevice.push(result);
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
+ // that.disconnect(result)
|
|
|
|
+ //断开连接是失败的。
|
|
|
|
+ console.log("监听到设备状态变化0:", result.deviceId, JSON.stringify(result));
|
|
|
|
+ that.errorDisconnect({
|
|
|
|
+ "deviceId": result.deviceId,
|
|
|
|
+ "state": result.connected ? "online" : "offline"
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
// ///是否已配对
|
|
// ///是否已配对
|
|
// wx.isBluetoothDevicePaired({
|
|
// wx.isBluetoothDevicePaired({
|
|
@@ -201,23 +221,51 @@ class bleManager {
|
|
///获取 所有搜索到的蓝牙设备
|
|
///获取 所有搜索到的蓝牙设备
|
|
getConnectedDevices() {
|
|
getConnectedDevices() {
|
|
var that = this;
|
|
var that = this;
|
|
|
|
+ const hexUtil = require('../utils/hexUtil');
|
|
|
|
+
|
|
|
|
+ // wx.getConnectedBluetoothDevices({
|
|
|
|
+ // // services: ['ab00'],
|
|
|
|
+ // search: ['ab00', 'ab01', 'ab02', '0000AB00-0000-1000-8000-00805F9B34FB', '0000AB01-0000-1000-8000-00805F9B34FB', '0000AB02-0000-1000-8000-00805F9B34FB'],
|
|
|
|
+ // success: (res) => {
|
|
|
|
+ // // ble连接上,才会有这个返回
|
|
|
|
+ // console.log('根据主服务 UUID 获取已连接的蓝牙设备:', res.devices);
|
|
|
|
+ // if (res.devices.length > 0) {
|
|
|
|
+ // for (var i = 0; i < res.devices.length; i++) {
|
|
|
|
+ // var device = res.devices[i];
|
|
|
|
+ // if (device.name != "") {
|
|
|
|
+ // that.callBackConnect(device);
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
+ // })
|
|
|
|
+
|
|
wx.getBluetoothDevices({
|
|
wx.getBluetoothDevices({
|
|
success: (res) => {
|
|
success: (res) => {
|
|
|
|
+ console.log("搜索到的:" + res.length);
|
|
|
|
+
|
|
if (that.callBackConnect != null) {
|
|
if (that.callBackConnect != null) {
|
|
for (var i = 0; i < res.devices.length; i++) {
|
|
for (var i = 0; i < res.devices.length; i++) {
|
|
var temp = res.devices[i];
|
|
var temp = res.devices[i];
|
|
if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
|
|
if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
|
|
- temp.mac = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
|
|
|
|
|
|
+ temp.mac = temp.advertisData ? hexUtil.buf2hex(temp.advertisData) : '';
|
|
if (temp.mac) {
|
|
if (temp.mac) {
|
|
console.log("搜索到的" + JSON.stringify(temp));
|
|
console.log("搜索到的" + JSON.stringify(temp));
|
|
}
|
|
}
|
|
// if (that.callBackConnect != null) {
|
|
// if (that.callBackConnect != null) {
|
|
- if (that.callBackConnect != null) {
|
|
|
|
- // if (that.callBackConnect != null && temp.mac.includes("CF:CA")) {
|
|
|
|
- that.callBackConnect(temp);
|
|
|
|
|
|
+ if (that.testData(temp)) {
|
|
|
|
+ return
|
|
}
|
|
}
|
|
- break;
|
|
|
|
|
|
+ console.log("444:" + JSON.stringify(temp));
|
|
|
|
+
|
|
|
|
+ // if ((temp.deviceId === "07:F6:F4:66:FB:FA")) {
|
|
|
|
+ // that.callBackConnect(temp);
|
|
|
|
+ // }
|
|
|
|
+ that.callBackConnect(temp);
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
+ break;
|
|
|
|
+
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
for (var i = 0; i < res.devices.length; i++) {
|
|
for (var i = 0; i < res.devices.length; i++) {
|
|
@@ -249,22 +297,13 @@ class bleManager {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- ///获取数据
|
|
|
|
- buf2hex(buffer) {
|
|
|
|
- return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- ///获取毫秒
|
|
|
|
- getCurrentMills() {
|
|
|
|
- var currentDate = new Date();
|
|
|
|
- var currentTimeMillis = currentDate.getTime();
|
|
|
|
- // return Math.floor(currentTimeMillis / 1000);
|
|
|
|
- return currentTimeMillis;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 等待多少秒
|
|
|
|
- delay(ms) {
|
|
|
|
- return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
|
|
+ testData(temp) {
|
|
|
|
+ if ((temp.deviceId === "1A:B4:E0:40:22:8B" ||
|
|
|
|
+ temp.deviceId === "F3:55:1F:8D:26:A0" ||
|
|
|
|
+ temp.deviceId === "3F2F112F-ACB5-3EB3-30DA-93DE6A86228A")) {
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+ return false
|
|
}
|
|
}
|
|
|
|
|
|
// 开始搜索蓝牙设备
|
|
// 开始搜索蓝牙设备
|
|
@@ -272,38 +311,64 @@ class bleManager {
|
|
var that = this;
|
|
var that = this;
|
|
|
|
|
|
///限制搜索没有打开蓝牙一直询问打开蓝牙
|
|
///限制搜索没有打开蓝牙一直询问打开蓝牙
|
|
- var isAvailable = that.isAvailable;
|
|
|
|
- if (!isAvailable && callBackConnect == null) {
|
|
|
|
- return;
|
|
|
|
|
|
+ const time_util = require('../utils/timeUtil');
|
|
|
|
+ if (!that.isAvailable) {
|
|
|
|
+ if (callBackConnect == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ///做搜索蓝牙适配器权限
|
|
|
|
+ const permissionUtil = require('../utils/permissionUtil');
|
|
|
|
+ // var location = await permission_util.getSetting()
|
|
|
|
+ // if (!location) {
|
|
|
|
+ // if (boolean != null) {
|
|
|
|
+ // boolean(false);
|
|
|
|
+ // console.log("没有定位权限")
|
|
|
|
+ // }
|
|
|
|
+ // return
|
|
|
|
+ // }
|
|
|
|
+ var available = await permissionUtil.openBluetoothAdapter();
|
|
|
|
+ that.isAvailable = available;
|
|
|
|
+ if (!available) {
|
|
|
|
+ that.doStartScaning = false;
|
|
|
|
+ that.requestBlueTime = time_util.getCurrentMills();
|
|
|
|
+ if (boolean != null) {
|
|
|
|
+ boolean(false);
|
|
|
|
+ }
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- const route_util = require('../utils/route_util');
|
|
|
|
- const route_constant = require('../utils/route_constant');
|
|
|
|
- const indexRoot = route_constant.indexRoot;
|
|
|
|
- const connectBleRoot = route_constant.connectBleRoot;
|
|
|
|
- var lastPageRoute = route_util.getLastPageRoute();
|
|
|
|
- if (lastPageRoute != indexRoot && lastPageRoute != connectBleRoot) {
|
|
|
|
|
|
+ const routeUtil = require('../utils/routeUtil');
|
|
|
|
+ const routeRoot = require('../utils/routeRoot');
|
|
|
|
+
|
|
|
|
+ const index = routeRoot.index;
|
|
|
|
+ const connectBleRoot = routeRoot.connectBle;
|
|
|
|
+ var lastPageRoute = routeUtil.getLastPageRoute();
|
|
|
|
+ if (lastPageRoute != index && lastPageRoute != connectBleRoot) {
|
|
|
|
+ console.log("搜索蓝牙设备失败,请返回首页0");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
///蓝牙连接 做限制
|
|
///蓝牙连接 做限制
|
|
- if (lastPageRoute == indexRoot) {
|
|
|
|
|
|
+ if (lastPageRoute == index) {
|
|
if (that.doStartScaning == true) {
|
|
if (that.doStartScaning == true) {
|
|
|
|
+ console.log("搜索蓝牙设备失败,请返回首页1");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
that.doStartScaning = true;
|
|
that.doStartScaning = true;
|
|
- var currentMill = that.getCurrentMills();
|
|
|
|
|
|
+ var currentMill = time_util.getCurrentMills();
|
|
var waitMills = 0;
|
|
var waitMills = 0;
|
|
var reduce = currentMill - that.requestBlueTime;
|
|
var reduce = currentMill - that.requestBlueTime;
|
|
- const delayMiliis = 6 * 1000;
|
|
|
|
- if (reduce > 0 && reduce < delayMiliis) {
|
|
|
|
- waitMills = delayMiliis - reduce;
|
|
|
|
|
|
+ const dMiliis = 6 * 1000;
|
|
|
|
+ if (reduce > 0 && reduce < dMiliis) {
|
|
|
|
+ waitMills = dMiliis - reduce;
|
|
}
|
|
}
|
|
|
|
|
|
if (waitMills > 0) {
|
|
if (waitMills > 0) {
|
|
- await that.delay(waitMills);
|
|
|
|
|
|
+ await time_util.delayMills(waitMills);
|
|
}
|
|
}
|
|
|
|
|
|
if (callBackConnect == null && lastPageRoute == connectBleRoot) {
|
|
if (callBackConnect == null && lastPageRoute == connectBleRoot) {
|
|
@@ -311,46 +376,31 @@ class bleManager {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- wx.openBluetoothAdapter({
|
|
|
|
- success: function (res) {
|
|
|
|
- wx.stopBluetoothDevicesDiscovery({
|
|
|
|
- success: (res) => {
|
|
|
|
- that.search(connectWillDevice, boolean, callBackConnect);
|
|
|
|
- },
|
|
|
|
- fail: (err) => {
|
|
|
|
- that.doStartScaning = false;
|
|
|
|
- that.requestBlueTime = that.getCurrentMills();
|
|
|
|
- if (boolean != null) {
|
|
|
|
- boolean(false);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
|
|
+ wx.stopBluetoothDevicesDiscovery({
|
|
|
|
+ success: (res) => {
|
|
|
|
+ that.search(connectWillDevice, boolean, callBackConnect);
|
|
},
|
|
},
|
|
- fail: function (res) {
|
|
|
|
- // wx.showModal({
|
|
|
|
- // title: '提示',
|
|
|
|
- // content: '请检查手机蓝牙是否打开',
|
|
|
|
- // showCancel: false,
|
|
|
|
- // success: function (res) {}
|
|
|
|
- // });
|
|
|
|
|
|
+ fail: (err) => {
|
|
that.doStartScaning = false;
|
|
that.doStartScaning = false;
|
|
- that.requestBlueTime = that.getCurrentMills();
|
|
|
|
|
|
+ that.requestBlueTime = time_util.getCurrentMills();
|
|
if (boolean != null) {
|
|
if (boolean != null) {
|
|
boolean(false);
|
|
boolean(false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- })
|
|
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
search(connectWillDevice, boolean, callBackConnect) {
|
|
search(connectWillDevice, boolean, callBackConnect) {
|
|
var that = this;
|
|
var that = this;
|
|
|
|
+ const timeUtil = require('../utils/timeUtil');
|
|
wx.startBluetoothDevicesDiscovery({
|
|
wx.startBluetoothDevicesDiscovery({
|
|
allowDuplicatesKey: true,
|
|
allowDuplicatesKey: true,
|
|
success: function (res) {
|
|
success: function (res) {
|
|
|
|
+ console.log("开始搜索设备", res);
|
|
that.getConnectedDevices();
|
|
that.getConnectedDevices();
|
|
|
|
|
|
that.doStartScaning = false;
|
|
that.doStartScaning = false;
|
|
- that.requestBlueTime = that.getCurrentMills();
|
|
|
|
|
|
+ that.requestBlueTime = timeUtil.getCurrentMills();
|
|
if (boolean != null) {
|
|
if (boolean != null) {
|
|
boolean(true);
|
|
boolean(true);
|
|
}
|
|
}
|
|
@@ -359,8 +409,14 @@ class bleManager {
|
|
that.compareList = [];
|
|
that.compareList = [];
|
|
},
|
|
},
|
|
fail(err) {
|
|
fail(err) {
|
|
|
|
+ console.log("开始搜索设备失败", err);
|
|
that.doStartScaning = false;
|
|
that.doStartScaning = false;
|
|
- that.requestBlueTime = that.getCurrentMills();
|
|
|
|
|
|
+ that.requestBlueTime = timeUtil.getCurrentMills();
|
|
|
|
+ if (err.errMsg.indexOf("location") >= 0) {
|
|
|
|
+ wx.showToast({
|
|
|
|
+ title: '搜索失败,请检查下手机设置的微信定位权限是否开启',
|
|
|
|
+ })
|
|
|
|
+ }
|
|
if (boolean != null) {
|
|
if (boolean != null) {
|
|
boolean(false);
|
|
boolean(false);
|
|
}
|
|
}
|
|
@@ -394,7 +450,8 @@ class bleManager {
|
|
disconnect(mDevice) {
|
|
disconnect(mDevice) {
|
|
var that = this;
|
|
var that = this;
|
|
const strings = require('../utils/strings');
|
|
const strings = require('../utils/strings');
|
|
- let device = that.publicDevice;
|
|
|
|
|
|
+ // 没有参数就默认断开现在连接的
|
|
|
|
+ let device = mDevice;
|
|
if (mDevice != null) {
|
|
if (mDevice != null) {
|
|
device = mDevice;
|
|
device = mDevice;
|
|
}
|
|
}
|
|
@@ -406,27 +463,21 @@ class bleManager {
|
|
if (strings.isEmpty(deviceId)) {
|
|
if (strings.isEmpty(deviceId)) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- console.log('断开连接', device);
|
|
|
|
|
|
+ console.log('开始断开连接', device);
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
- wx.getConnectedBluetoothDevices({
|
|
|
|
|
|
+ wx.closeBLEConnection({
|
|
|
|
+ deviceId: deviceId,
|
|
success: (res) => {
|
|
success: (res) => {
|
|
- if (res.devices.length > 0) {
|
|
|
|
- wx.closeBLEConnection({
|
|
|
|
- deviceId: deviceId,
|
|
|
|
- success: (res) => {
|
|
|
|
- that.publicDevice = null;
|
|
|
|
- resolve(res);
|
|
|
|
- },
|
|
|
|
- fail: (err) => {
|
|
|
|
- resolve(err);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- resolve("暂无连接设备");
|
|
|
|
|
|
+ let publicDevice = that.publicDevice ?? {}
|
|
|
|
+ if (deviceId == publicDevice.deviceId) {
|
|
|
|
+ console.log('成功断开连接', res, deviceId);
|
|
|
|
+ that.publicDevice = null;
|
|
}
|
|
}
|
|
|
|
+ resolve(res);
|
|
},
|
|
},
|
|
fail: (err) => {
|
|
fail: (err) => {
|
|
|
|
+ console.log('断开连接失败', err);
|
|
resolve(err);
|
|
resolve(err);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
@@ -436,6 +487,11 @@ class bleManager {
|
|
// 连接到指定设备
|
|
// 连接到指定设备
|
|
async connectToDevice(device) {
|
|
async connectToDevice(device) {
|
|
var that = this;
|
|
var that = this;
|
|
|
|
+ if (that.isConnecting) {
|
|
|
|
+ // 防止多次进入连接
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ that.isConnecting = true
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
console.log("开始连接蓝牙:", device.deviceId)
|
|
console.log("开始连接蓝牙:", device.deviceId)
|
|
wx.createBLEConnection({
|
|
wx.createBLEConnection({
|
|
@@ -443,10 +499,37 @@ class bleManager {
|
|
success: (res) => {
|
|
success: (res) => {
|
|
that.publicDevice = device
|
|
that.publicDevice = device
|
|
console.log('连接成功:', res);
|
|
console.log('连接成功:', res);
|
|
|
|
+ that.isConnecting = false;
|
|
|
|
+ wx.setBLEMTU({
|
|
|
|
+ deviceId: device.deviceId,
|
|
|
|
+ mtu: 128,
|
|
|
|
+ success: (mtuRes) => {
|
|
|
|
+ console.log("设置mtu成功")
|
|
|
|
+ },
|
|
|
|
+ fail: (mtuErr) => {
|
|
|
|
+ console.log("设置mtu失败:", mtuErr)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
resolve(true);
|
|
resolve(true);
|
|
},
|
|
},
|
|
fail: (err) => {
|
|
fail: (err) => {
|
|
- that.publicDevice = null
|
|
|
|
|
|
+ // {errno: 1509007, errCode: -1, errMsg: "createBLEConnection:fail:already connect"}
|
|
|
|
+ if (err.errno == 1509007) {
|
|
|
|
+ that.disconnect({
|
|
|
|
+ "deviceId": device.deviceId
|
|
|
|
+ })
|
|
|
|
+ } else {
|
|
|
|
+ that.isConnecting = false;
|
|
|
|
+ console.log("监听到设备状态变化3:", err);
|
|
|
|
+ that.errorDisconnect({
|
|
|
|
+ "deviceId": device.deviceId,
|
|
|
|
+ "state": "offline"
|
|
|
|
+ })
|
|
|
|
+ let publicDevice = that.publicDevice ?? {}
|
|
|
|
+ if (device.deviceId == publicDevice.deviceId) {
|
|
|
|
+ that.publicDevice = null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
console.error('连接失败:', err);
|
|
console.error('连接失败:', err);
|
|
resolve(false);
|
|
resolve(false);
|
|
}
|
|
}
|
|
@@ -457,6 +540,10 @@ class bleManager {
|
|
// 发送数据到指定设备
|
|
// 发送数据到指定设备
|
|
async sendData(data, callback) {
|
|
async sendData(data, callback) {
|
|
var that = this
|
|
var that = this
|
|
|
|
+ if (that.publicDevice == null) {
|
|
|
|
+ console.log("没有连接设备");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
var buffer = null;
|
|
var buffer = null;
|
|
// todo 判断是否是buffer
|
|
// todo 判断是否是buffer
|
|
@@ -470,14 +557,15 @@ class bleManager {
|
|
dataView.setUint8(index, value); // 将每个16进制数值写入到 buffer 中
|
|
dataView.setUint8(index, value); // 将每个16进制数值写入到 buffer 中
|
|
});
|
|
});
|
|
// }
|
|
// }
|
|
-
|
|
|
|
- // console.log('开始发送数据:', data);
|
|
|
|
|
|
+ // let logData = new Uint8Array(buffer);
|
|
|
|
+ // console.log('开始发送数据:', buffer);
|
|
wx.writeBLECharacteristicValue({
|
|
wx.writeBLECharacteristicValue({
|
|
deviceId: that.publicDevice.deviceId,
|
|
deviceId: that.publicDevice.deviceId,
|
|
serviceId: that.publicDevice.serviceId,
|
|
serviceId: that.publicDevice.serviceId,
|
|
characteristicId: that.publicDevice.characteristicId,
|
|
characteristicId: that.publicDevice.characteristicId,
|
|
value: buffer,
|
|
value: buffer,
|
|
success: (res) => {
|
|
success: (res) => {
|
|
|
|
+ console.log('发送数据成功:', dataView);
|
|
if (callback) {
|
|
if (callback) {
|
|
callback(true)
|
|
callback(true)
|
|
}
|
|
}
|
|
@@ -487,7 +575,11 @@ class bleManager {
|
|
if (callback) {
|
|
if (callback) {
|
|
callback(false)
|
|
callback(false)
|
|
}
|
|
}
|
|
- console.log('数据发送失败:', err);
|
|
|
|
|
|
+ console.log('监听到设备状态变化4:', err);
|
|
|
|
+ that.errorDisconnect({
|
|
|
|
+ "deviceId": that.publicDevice.deviceId,
|
|
|
|
+ "state": "offline"
|
|
|
|
+ })
|
|
resolve(false);
|
|
resolve(false);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
@@ -570,14 +662,18 @@ class bleManager {
|
|
service_id = res.services[i].uuid;
|
|
service_id = res.services[i].uuid;
|
|
|
|
|
|
}
|
|
}
|
|
- that.publicDevice.serviceId = service_id;
|
|
|
|
- console.log('发现服务2:', service_id);
|
|
|
|
|
|
+ if (that.publicDevice) {
|
|
|
|
+ that.publicDevice.serviceId = service_id;
|
|
|
|
+ console.log('发现服务2:', that.publicDevice.service_id);
|
|
|
|
+ }
|
|
|
|
|
|
resolve(service_id);
|
|
resolve(service_id);
|
|
// resolve(res.services);
|
|
// resolve(res.services);
|
|
},
|
|
},
|
|
fail: (err) => {
|
|
fail: (err) => {
|
|
- that.publicDevice.serviceId = null;
|
|
|
|
|
|
+ if (that.publicDevice) {
|
|
|
|
+ that.publicDevice.serviceId = null;
|
|
|
|
+ }
|
|
|
|
|
|
console.error('发现服务失败:', err);
|
|
console.error('发现服务失败:', err);
|
|
reject([]);
|
|
reject([]);
|
|
@@ -586,7 +682,7 @@ class bleManager {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- connect() { }
|
|
|
|
|
|
+ connect() {}
|
|
|
|
|
|
// 发现特征值 read / write
|
|
// 发现特征值 read / write
|
|
discoverCharacteristics(deviceId, serviceId) {
|
|
discoverCharacteristics(deviceId, serviceId) {
|
|
@@ -607,7 +703,9 @@ class bleManager {
|
|
resolve(res.characteristics);
|
|
resolve(res.characteristics);
|
|
},
|
|
},
|
|
fail: (err) => {
|
|
fail: (err) => {
|
|
- that.publicDevice.characteristics = null;
|
|
|
|
|
|
+ if (that.publicDevice) {
|
|
|
|
+ that.publicDevice.characteristics = null;
|
|
|
|
+ }
|
|
console.error('发现特征值失败:', err);
|
|
console.error('发现特征值失败:', err);
|
|
reject("");
|
|
reject("");
|
|
}
|
|
}
|
|
@@ -682,8 +780,18 @@ class bleManager {
|
|
// that.publicDevice .wirte = wirte
|
|
// that.publicDevice .wirte = wirte
|
|
that.publicDevice.characteristicId = characteristicId;
|
|
that.publicDevice.characteristicId = characteristicId;
|
|
}
|
|
}
|
|
|
|
+ setStateChangeCallback(callback) {
|
|
|
|
+ this.stateChangeCallback = callback;
|
|
|
|
+ }
|
|
|
|
+ errorDisconnect(result) {
|
|
|
|
+ // this.disconnect();
|
|
|
|
+ console.log("监听到设备状态变化1:", result.deviceId, result.state, this.stateChangeCallback);
|
|
|
|
+ if (this.stateChangeCallback && result.deviceId) {
|
|
|
|
+ this.stateChangeCallback(result);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
// const ble = new bleManager();
|
|
// const ble = new bleManager();
|
|
|
|
|
|
// 导出 bleManager 类
|
|
// 导出 bleManager 类
|
|
-module.exports = bleManager;
|
|
|
|
|
|
+module.exports = bleManager;
|