|
@@ -19,16 +19,8 @@ class Manager {
|
|
|
that.dissmissDevice = [];
|
|
|
|
|
|
///当前蓝牙ble连接设备的相关属性
|
|
|
- that.services = {};
|
|
|
- that.serviceIndex = 0;
|
|
|
- that.notify = false;
|
|
|
- that.write = false;
|
|
|
- that.read = false;
|
|
|
-
|
|
|
that.writeCharaterId = "";
|
|
|
that.writeServiceId = "";
|
|
|
- that.notifyCharaterId = "";
|
|
|
- that.notifyServiceId = "";
|
|
|
that.readCharaterId = "";
|
|
|
that.readServiceId = "";
|
|
|
}
|
|
@@ -329,99 +321,97 @@ class Manager {
|
|
|
}
|
|
|
|
|
|
///初始化 接收数据 获取特征值
|
|
|
- getNotifyCharaterId(device) {
|
|
|
- var that = this;
|
|
|
+ getNotifyServices(device) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
var deviceId = device.deviceId;
|
|
|
wx.getBLEDeviceServices({
|
|
|
deviceId: deviceId,
|
|
|
success: function (res) {
|
|
|
- that.services = res.services;
|
|
|
- that.getCharacteristics(device);
|
|
|
+ resolve(res.services);
|
|
|
+ },
|
|
|
+ fail: function (res) {
|
|
|
+ resolve(null);
|
|
|
}
|
|
|
- })
|
|
|
+ });
|
|
|
});
|
|
|
}
|
|
|
|
|
|
///获取设备真正的属性特征值
|
|
|
- getCharacteristics(device) {
|
|
|
+ getCharacteristics(device, services, index, notify, write, read, succeed, fail) {
|
|
|
var that = this;
|
|
|
var deviceId = device.deviceId;
|
|
|
- var index = that.serviceIndex;
|
|
|
- var notify = that.notify;
|
|
|
- var write = that.write;
|
|
|
- var read = that.read;
|
|
|
- var services = that.services;
|
|
|
wx.getBLEDeviceCharacteristics({
|
|
|
deviceId: deviceId,
|
|
|
serviceId: services[index].uuid,
|
|
|
success: function (res) {
|
|
|
- console.log(res.characteristics)
|
|
|
+ var notifyCharaterId = "";
|
|
|
+ var notifyServiceId = "";
|
|
|
for (var i = 0; i < res.characteristics.length; i++) {
|
|
|
var properties = res.characteristics[i].properties;
|
|
|
var charaterId = res.characteristics[i].uuid;
|
|
|
|
|
|
- if (!that.notify) {
|
|
|
+ if (!notify) {
|
|
|
if (properties.notify) {
|
|
|
- that.notifyCharaterId = charaterId;
|
|
|
- that.notifyServiceId = services[index].uuid;
|
|
|
- that.notify = true;
|
|
|
+ notifyCharaterId = charaterId;
|
|
|
+ notifyServiceId = services[index].uuid;
|
|
|
+ notify = true;
|
|
|
}
|
|
|
}
|
|
|
- if (!that.write) {
|
|
|
+ if (!write) {
|
|
|
if (properties.write) {
|
|
|
that.writeCharaterId = charaterId;
|
|
|
that.writeServiceId = services[index].uuid;
|
|
|
- that.write = true;
|
|
|
+ write = true;
|
|
|
}
|
|
|
}
|
|
|
- if (!that.read) {
|
|
|
+
|
|
|
+ if (!read) {
|
|
|
if (properties.read) {
|
|
|
that.readCharaterId = charaterId;
|
|
|
that.readServiceId = services[index].uuid;
|
|
|
- that.read = true;
|
|
|
+ read = true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if (!that.notify) {
|
|
|
- index++
|
|
|
- that.serviceIndex = index;
|
|
|
- that.notify = notify;
|
|
|
- that.write = write;
|
|
|
- that.read = read;
|
|
|
|
|
|
+ if (!notify) {
|
|
|
+ index++
|
|
|
if (index == services.length) {
|
|
|
- that.serviceIndex = 0;
|
|
|
- that.notify = false;
|
|
|
- that.write = false;
|
|
|
- that.read = false;
|
|
|
+ fail();
|
|
|
} else {
|
|
|
- that.getCharacteristics(device);
|
|
|
+ that.getCharacteristics(device, services, index, notify, write, read, succeed, fail);
|
|
|
}
|
|
|
} else {
|
|
|
- that.monitorCharacteristicValueChange(device);
|
|
|
+ succeed(notifyServiceId, notifyCharaterId);
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
// 监听发送的数据
|
|
|
- monitorCharacteristicValueChange(device) {
|
|
|
- var that = this;
|
|
|
- var deviceId = device.deviceId;
|
|
|
- const hex_util = require('./../../utils/hex_util');
|
|
|
- wx.notifyBLECharacteristicValueChange({
|
|
|
- state: true,
|
|
|
- deviceId: deviceId,
|
|
|
- serviceId: that.notifyServiceId,
|
|
|
- characteristicId: that.notifyCharaterId,
|
|
|
- success: function (res) {
|
|
|
- wx.onBLECharacteristicValueChange(function (r) {
|
|
|
- var receiveText = hex_util.buf2string(r.value);
|
|
|
- console.log('接收到数据文字:' + receiveText)
|
|
|
- });
|
|
|
- }
|
|
|
- })
|
|
|
+ monitorCharacteristicValueChange(device, notifyServiceId, notifyCharaterId) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ var deviceId = device.deviceId;
|
|
|
+ wx.notifyBLECharacteristicValueChange({
|
|
|
+ state: true,
|
|
|
+ deviceId: deviceId,
|
|
|
+ serviceId: notifyServiceId,
|
|
|
+ characteristicId: notifyCharaterId,
|
|
|
+ success: function (res) {
|
|
|
+ resolve(true);
|
|
|
+ },
|
|
|
+ fail: function (res) {
|
|
|
+ resolve(false);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ ///收取到的数据转换为文字
|
|
|
+ onBLECharacteristicValueChange(callback) {
|
|
|
+ wx.onBLECharacteristicValueChange(function (r) {
|
|
|
+ callback(r.value);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
// 断开设备的连接
|
|
@@ -441,21 +431,27 @@ class Manager {
|
|
|
}
|
|
|
|
|
|
///发送数据
|
|
|
- sendData(text, device) {
|
|
|
- var that = this;
|
|
|
- var deviceId = device.deviceId;
|
|
|
- var buffer = new ArrayBuffer(text.length)
|
|
|
- var dataView = new Uint8Array(buffer)
|
|
|
- for (var i = 0; i < text.length; i++) {
|
|
|
- dataView[i] = text.charCodeAt(i)
|
|
|
- }
|
|
|
- wx.writeBLECharacteristicValue({
|
|
|
- deviceId: deviceId,
|
|
|
- serviceId: that.writeServiceId,
|
|
|
- characteristicId: that.writeCharaterId,
|
|
|
- value: buffer,
|
|
|
- success: function (res) {},
|
|
|
- fail(err) {}
|
|
|
+ sendData(device, text) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ var that = this;
|
|
|
+ var deviceId = device.deviceId;
|
|
|
+ var buffer = new ArrayBuffer(text.length)
|
|
|
+ var dataView = new Uint8Array(buffer)
|
|
|
+ for (var i = 0; i < text.length; i++) {
|
|
|
+ dataView[i] = text.charCodeAt(i)
|
|
|
+ }
|
|
|
+ wx.writeBLECharacteristicValue({
|
|
|
+ deviceId: deviceId,
|
|
|
+ serviceId: that.writeServiceId,
|
|
|
+ characteristicId: that.writeCharaterId,
|
|
|
+ value: buffer,
|
|
|
+ success: function (res) {
|
|
|
+ resolve(true);
|
|
|
+ },
|
|
|
+ fail(err) {
|
|
|
+ resolve(false);
|
|
|
+ }
|
|
|
+ });
|
|
|
});
|
|
|
}
|
|
|
}
|