|
@@ -56,12 +56,13 @@ class bleManager {
|
|
|
///监听搜索设备列表
|
|
|
getBluetoothDevices() {
|
|
|
var that = this;
|
|
|
+ const hex_util = require('./../utils/hex_util');
|
|
|
wx.onBluetoothDeviceFound(function (res) {
|
|
|
///第一种情况
|
|
|
if (res.deviceId) {
|
|
|
if (that.callBackConnect != null) {
|
|
|
if (that.connectWillDevice != null && res.name == that.connectWillDevice.clientType) {
|
|
|
- res.mac = res.advertisData ? that.buf2hex(res.advertisData) : '';
|
|
|
+ res.mac = res.advertisData ? hex_util.buf2hex(res.advertisData) : '';
|
|
|
// if (that.callBackConnect != null) {
|
|
|
console.log("1111", res.mac);
|
|
|
if (that.callBackConnect != null) {
|
|
@@ -98,7 +99,7 @@ class bleManager {
|
|
|
// console.log("2222", temp.name);
|
|
|
// }
|
|
|
if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
|
|
|
- temp.mac = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
|
|
|
+ temp.mac = temp.advertisData ? hex_util.buf2hex(temp.advertisData) : '';
|
|
|
temp.mac2 = that.ab2hex(temp.advertisData ?? "")
|
|
|
// if (that.callBackConnect != null) {
|
|
|
console.log("2222:", temp.mac, ":", temp.mac2, ":", temp.deviceId, temp);
|
|
@@ -135,12 +136,10 @@ class bleManager {
|
|
|
else if (res[0]) {
|
|
|
if (that.callBackConnect != null) {
|
|
|
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 ? hex_util.buf2hex(res[0].advertisData) : '';
|
|
|
// if (that.callBackConnect != null) {
|
|
|
console.log("3333", res.mac);
|
|
|
-
|
|
|
if (that.callBackConnect != null) {
|
|
|
-
|
|
|
that.callBackConnect(res[0]);
|
|
|
}
|
|
|
}
|
|
@@ -211,13 +210,14 @@ class bleManager {
|
|
|
///获取 所有搜索到的蓝牙设备
|
|
|
getConnectedDevices() {
|
|
|
var that = this;
|
|
|
+ const hex_util = require('./../utils/hex_util');
|
|
|
wx.getBluetoothDevices({
|
|
|
success: (res) => {
|
|
|
if (that.callBackConnect != null) {
|
|
|
for (var i = 0; i < res.devices.length; i++) {
|
|
|
var temp = res.devices[i];
|
|
|
if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
|
|
|
- temp.mac = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
|
|
|
+ temp.mac = temp.advertisData ? hex_util.buf2hex(temp.advertisData) : '';
|
|
|
if (temp.mac) {
|
|
|
console.log("搜索到的" + JSON.stringify(temp));
|
|
|
}
|
|
@@ -259,29 +259,12 @@ 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));
|
|
|
- }
|
|
|
-
|
|
|
// 开始搜索蓝牙设备
|
|
|
async startScan(connectWillDevice, boolean, callBackConnect) {
|
|
|
var that = this;
|
|
|
|
|
|
///限制搜索没有打开蓝牙一直询问打开蓝牙
|
|
|
+ const time_util = require('./../utils/time_util');
|
|
|
if (!that.isAvailable) {
|
|
|
if (callBackConnect == null) {
|
|
|
return;
|
|
@@ -293,7 +276,7 @@ class bleManager {
|
|
|
that.isAvailable = available;
|
|
|
if (!available) {
|
|
|
that.doStartScaning = false;
|
|
|
- that.requestBlueTime = that.getCurrentMills();
|
|
|
+ that.requestBlueTime = time_util.getCurrentMills();
|
|
|
if (boolean != null) {
|
|
|
boolean(false);
|
|
|
}
|
|
@@ -320,16 +303,16 @@ class bleManager {
|
|
|
}
|
|
|
|
|
|
that.doStartScaning = true;
|
|
|
- var currentMill = that.getCurrentMills();
|
|
|
+ var currentMill = time_util.getCurrentMills();
|
|
|
var waitMills = 0;
|
|
|
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) {
|
|
|
- await that.delay(waitMills);
|
|
|
+ await time_util.delayMills(waitMills);
|
|
|
}
|
|
|
|
|
|
if (callBackConnect == null && lastPageRoute == connectBleRoot) {
|
|
@@ -343,7 +326,7 @@ class bleManager {
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
that.doStartScaning = false;
|
|
|
- that.requestBlueTime = that.getCurrentMills();
|
|
|
+ that.requestBlueTime = time_util.getCurrentMills();
|
|
|
if (boolean != null) {
|
|
|
boolean(false);
|
|
|
}
|
|
@@ -353,6 +336,7 @@ class bleManager {
|
|
|
|
|
|
search(connectWillDevice, boolean, callBackConnect) {
|
|
|
var that = this;
|
|
|
+ const time_util = require('./../utils/time_util');
|
|
|
wx.startBluetoothDevicesDiscovery({
|
|
|
allowDuplicatesKey: true,
|
|
|
success: function (res) {
|
|
@@ -360,7 +344,7 @@ class bleManager {
|
|
|
that.getConnectedDevices();
|
|
|
|
|
|
that.doStartScaning = false;
|
|
|
- that.requestBlueTime = that.getCurrentMills();
|
|
|
+ that.requestBlueTime = time_util.getCurrentMills();
|
|
|
if (boolean != null) {
|
|
|
boolean(true);
|
|
|
}
|
|
@@ -370,9 +354,8 @@ class bleManager {
|
|
|
},
|
|
|
fail(err) {
|
|
|
console.log("开始搜索设备失败", res);
|
|
|
-
|
|
|
that.doStartScaning = false;
|
|
|
- that.requestBlueTime = that.getCurrentMills();
|
|
|
+ that.requestBlueTime = time_util.getCurrentMills();
|
|
|
if (boolean != null) {
|
|
|
boolean(false);
|
|
|
}
|