|
@@ -85,6 +85,9 @@ class bleManager {
|
|
|
if (that.callBackConnect != null) {
|
|
|
for (var i = 0; i < res.devices.length; i++) {
|
|
|
var temp = res.devices[i];
|
|
|
+ if (temp.name == "MW-SR1(4G_WIFI)") {
|
|
|
+ console.log("gadsfqewrqewrqwerqrqr==111==" + JSON.stringify(res.devices[i]));
|
|
|
+ }
|
|
|
if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
|
|
|
temp.mac = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
|
|
|
if (that.callBackConnect != null) {
|
|
@@ -143,9 +146,20 @@ class bleManager {
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
+
|
|
|
+ ///监听已连接或没有连接
|
|
|
+ // {"deviceId":"E4:9F:80:09:40:EC","connected":false}
|
|
|
+ wx.onBLEConnectionStateChange((result) => {
|
|
|
+ if (result.connected) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
+ console.log("gadsfqewrqewrqwerqrqr==222==" + JSON.stringify(result));
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- ///获取已连接的设备
|
|
|
+ ///获取 所有搜索到的蓝牙设备
|
|
|
getConnectedDevices() {
|
|
|
var that = this;
|
|
|
wx.getBluetoothDevices({
|
|
@@ -167,6 +181,9 @@ class bleManager {
|
|
|
var has = false;
|
|
|
for (var j = 0; j < that.compareList.length; j++) {
|
|
|
if (res.devices[i].name != "") {
|
|
|
+ if (res.devices[i].name == "MW-SR1(4G_WIFI)") {
|
|
|
+ console.log("gadsfqewrqewrqwerqrqr==000==" + JSON.stringify(res.devices[i]));
|
|
|
+ }
|
|
|
if (res.devices[i].deviceId == that.compareList[j].deviceId) {
|
|
|
has = true;
|
|
|
break;
|
|
@@ -188,6 +205,28 @@ class bleManager {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ ///获取 已连接的蓝牙设备
|
|
|
+ getConnectedBluetoothDevices() {
|
|
|
+ // [{"deviceId":"E4:9F:80:09:40:EC","name":"MW-SR1(4G_WIFI)"}]
|
|
|
+ // wx.getConnectedBluetoothDevices({
|
|
|
+ // success: (res) => {
|
|
|
+ // console.log("时代发生的法==33==" + JSON.stringify(res));
|
|
|
+ // if (res.devices.length > 0) {
|
|
|
+ // console.log("时代发生的法==22==" + JSON.stringify(res.devices));
|
|
|
+ // for (var i = 0; i < res.devices.length; i++) {
|
|
|
+ // console.log("时代发生的法==11==" + JSON.stringify(res.devices[i]));
|
|
|
+ // if (res.devices[i].name == "MW-SR1(4G_WIFI)") {
|
|
|
+ // console.log("gasdfqwerqwerqdfasdfr==11==" + JSON.stringify(res.devices[i]));
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // } else {
|
|
|
+
|
|
|
+ // }
|
|
|
+ // },
|
|
|
+ // fail: (err) => {}
|
|
|
+ // });
|
|
|
+ }
|
|
|
+
|
|
|
///获取数据
|
|
|
buf2hex(buffer) {
|
|
|
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
|
|
@@ -280,6 +319,7 @@ class bleManager {
|
|
|
allowDuplicatesKey: true,
|
|
|
success: function (res) {
|
|
|
that.getConnectedDevices();
|
|
|
+ that.getConnectedBluetoothDevices();
|
|
|
|
|
|
that.doStartScaning = false;
|
|
|
that.requestBlueTime = that.getCurrentMills();
|
|
@@ -311,7 +351,7 @@ class bleManager {
|
|
|
resolve(res);
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
- reject(new Error('停止搜索失败'));
|
|
|
+ reject('停止搜索失败');
|
|
|
}
|
|
|
});
|
|
|
});
|
|
@@ -323,10 +363,13 @@ class bleManager {
|
|
|
}
|
|
|
|
|
|
// 断开与指定设备的连接
|
|
|
- disconnect() {
|
|
|
+ disconnect(mDevice) {
|
|
|
var that = this;
|
|
|
const strings = require('../utils/strings');
|
|
|
let device = that.publicDevice;
|
|
|
+ if (mDevice != null) {
|
|
|
+ device = mDevice;
|
|
|
+ }
|
|
|
if (strings.isEmpty(device)) {
|
|
|
return;
|
|
|
}
|
|
@@ -361,6 +404,39 @@ class bleManager {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ // 连接到指定设备
|
|
|
+ async connectToDevice(device) {
|
|
|
+ var that = this;
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ console.log("开始连接蓝牙:", device.deviceId)
|
|
|
+
|
|
|
+ ///是否已配对
|
|
|
+ // wx.isBluetoothDevicePaired({
|
|
|
+ // deviceId: device.deviceId,
|
|
|
+ // ///{"isPaired":false,"errno":0,"errMsg":"isBluetoothDevicePaired:ok"}
|
|
|
+ // success: (res) => {
|
|
|
+ // console.log("gasdfqwerqwerqr==00===" + JSON.stringify(res));
|
|
|
+ // },
|
|
|
+ // fail: (err) => {
|
|
|
+ // console.log("gasdfqwerqwerqr==111===" + JSON.stringify(err));
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ wx.createBLEConnection({
|
|
|
+ deviceId: device.deviceId,
|
|
|
+ success: (res) => {
|
|
|
+ that.publicDevice = device
|
|
|
+ console.log('连接成功:', res);
|
|
|
+ resolve(true);
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ that.publicDevice = null
|
|
|
+ console.error('连接失败:', err);
|
|
|
+ resolve(false);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
// 发送数据到指定设备
|
|
|
async sendData(data) {
|
|
|
var that = this
|
|
@@ -449,26 +525,6 @@ class bleManager {
|
|
|
// }
|
|
|
return isNot;
|
|
|
}
|
|
|
- // 连接到指定设备
|
|
|
- async connectToDevice(device) {
|
|
|
- var that = this;
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- console.log("开始连接蓝牙:", device.deviceId)
|
|
|
- wx.createBLEConnection({
|
|
|
- deviceId: device.deviceId,
|
|
|
- success: (res) => {
|
|
|
- that.publicDevice = device
|
|
|
- console.log('连接成功:', res);
|
|
|
- resolve(true);
|
|
|
- },
|
|
|
- fail: (err) => {
|
|
|
- that.publicDevice = null
|
|
|
- console.error('连接失败:', err);
|
|
|
- resolve(false);
|
|
|
- }
|
|
|
- });
|
|
|
- });
|
|
|
- }
|
|
|
|
|
|
// 发现服务
|
|
|
discoverServices(deviceId) {
|