|
@@ -55,11 +55,12 @@ class bleManager {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- ///监听搜索设备列表
|
|
|
+ ///监听搜索设备列表 监听设备列表
|
|
|
getBluetoothDevices() {
|
|
|
var that = this;
|
|
|
const hex_util = require('./../utils/hex_util');
|
|
|
wx.onBluetoothDeviceFound(function (res) {
|
|
|
+ // console.log("蓝牙设备列表", res.length);
|
|
|
///第一种情况
|
|
|
if (res.deviceId) {
|
|
|
if (that.callBackConnect != null) {
|
|
@@ -104,11 +105,21 @@ class bleManager {
|
|
|
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);
|
|
|
- if (that.callBackConnect != null) {
|
|
|
- // if (that.callBackConnect != null && (temp.deviceId != "6E:66:C2:CA:74:F8" || temp.deviceId != "1A:B4:E0:40:22:8B")) {
|
|
|
- that.callBackConnect(temp);
|
|
|
+ // C8:96:D9:51:37:98
|
|
|
+ // 1A:B4:E0:40:22:8B
|
|
|
+ // if (that.callBackConnect != null) {
|
|
|
+ // || temp.deviceId != "1A:B4:E0:40:22:8B"
|
|
|
+ // A3:7B:97:45:CC:22
|
|
|
+ // DA:D5:FB:DC:91:30
|
|
|
+ if ((temp.deviceId === "1A:B4:E0:40:22:8B")) {
|
|
|
+ return
|
|
|
}
|
|
|
+ console.log("2222:", temp.mac, ":", temp.mac2, ":", temp.deviceId, temp, temp.connectable);
|
|
|
+
|
|
|
+ // if ((temp.deviceId === "07:F6:F4:66:FB:FA")) {
|
|
|
+ // that.callBackConnect(temp);
|
|
|
+ // }
|
|
|
+ that.callBackConnect(temp);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
@@ -193,6 +204,7 @@ class bleManager {
|
|
|
}
|
|
|
// that.disconnect(result)
|
|
|
//断开连接是失败的, 只能监听到下线。
|
|
|
+ console.log("监听到设备状态变化:", result);
|
|
|
that.errorDisconnect(result);
|
|
|
|
|
|
|
|
@@ -212,8 +224,28 @@ class bleManager {
|
|
|
getConnectedDevices() {
|
|
|
var that = this;
|
|
|
const hex_util = require('./../utils/hex_util');
|
|
|
+
|
|
|
+ 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({
|
|
|
success: (res) => {
|
|
|
+ console.log("搜索到的:" + res.length);
|
|
|
+
|
|
|
if (that.callBackConnect != null) {
|
|
|
for (var i = 0; i < res.devices.length; i++) {
|
|
|
var temp = res.devices[i];
|
|
@@ -224,8 +256,16 @@ class bleManager {
|
|
|
}
|
|
|
// if (that.callBackConnect != null) {
|
|
|
if (that.callBackConnect != null) {
|
|
|
- // if (that.callBackConnect != null && temp.mac.includes("CF:CA")) {
|
|
|
+ if ((temp.deviceId === "1A:B4:E0:40:22:8B")) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ console.log("444:" + JSON.stringify(temp));
|
|
|
+
|
|
|
+ // if ((temp.deviceId === "07:F6:F4:66:FB:FA")) {
|
|
|
+ // that.callBackConnect(temp);
|
|
|
+ // }
|
|
|
that.callBackConnect(temp);
|
|
|
+
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
@@ -424,8 +464,10 @@ class bleManager {
|
|
|
async connectToDevice(device) {
|
|
|
var that = this;
|
|
|
if (that.isConnecting) {
|
|
|
+ // 防止多次进入连接
|
|
|
return;
|
|
|
}
|
|
|
+ that.isConnecting = true
|
|
|
return new Promise((resolve, reject) => {
|
|
|
console.log("开始连接蓝牙:", device.deviceId)
|
|
|
wx.createBLEConnection({
|
|
@@ -437,8 +479,14 @@ class bleManager {
|
|
|
resolve(true);
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
- that.isConnecting = false;
|
|
|
- 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;
|
|
|
+ that.errorDisconnect()
|
|
|
+ that.publicDevice = null
|
|
|
+ }
|
|
|
console.error('连接失败:', err);
|
|
|
resolve(false);
|
|
|
}
|
|
@@ -474,6 +522,7 @@ class bleManager {
|
|
|
characteristicId: that.publicDevice.characteristicId,
|
|
|
value: buffer,
|
|
|
success: (res) => {
|
|
|
+ console.log('发送数据成功:', res, callback);
|
|
|
if (callback) {
|
|
|
callback(true)
|
|
|
}
|
|
@@ -567,14 +616,18 @@ class bleManager {
|
|
|
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(res.services);
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
- that.publicDevice.serviceId = null;
|
|
|
+ if (that.publicDevice) {
|
|
|
+ that.publicDevice.serviceId = null;
|
|
|
+ }
|
|
|
|
|
|
console.error('发现服务失败:', err);
|
|
|
reject([]);
|
|
@@ -685,7 +738,8 @@ class bleManager {
|
|
|
errorDisconnect() {
|
|
|
// this.disconnect();
|
|
|
if (this.stateChangeCallback) {
|
|
|
- this.stateChangeCallback({ "deviceId": this.publicDevice.deviceId, "state": "offline" });
|
|
|
+ let device = this.publicDevice ?? {};
|
|
|
+ this.stateChangeCallback({ "deviceId": device.deviceId ?? "", "state": "offline" });
|
|
|
}
|
|
|
}
|
|
|
}
|