|
@@ -403,31 +403,18 @@ class Manager {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- // 发现特征值 read / write
|
|
|
- discoverCharacteristics(device, serviceId) {
|
|
|
- var deviceId = device.deviceId;
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- wx.getBLEDeviceCharacteristics({
|
|
|
- deviceId: deviceId,
|
|
|
- serviceId: serviceId,
|
|
|
- success: (res) => {
|
|
|
- resolve(res.characteristics);
|
|
|
- },
|
|
|
- fail: (err) => {
|
|
|
- reject("");
|
|
|
- }
|
|
|
- });
|
|
|
- });
|
|
|
+ setWriteServiceId(serviceId) {
|
|
|
+ var that = this;
|
|
|
+ that.writeServiceId = serviceId;
|
|
|
}
|
|
|
|
|
|
///获取设备真正的属性特征值
|
|
|
- getCharacteristics(device, serviceId, failed, getMessageCall, successed) {
|
|
|
+ getCharacteristics(device, failed, getMessageCall, successed) {
|
|
|
var that = this;
|
|
|
var deviceId = device.deviceId;
|
|
|
wx.getBLEDeviceCharacteristics({
|
|
|
deviceId: deviceId,
|
|
|
- serviceId: serviceId,
|
|
|
+ serviceId: that.writeServiceId,
|
|
|
success: function (res) {
|
|
|
var characteristics = res.characteristics;
|
|
|
if (characteristics.length <= 0) {
|
|
@@ -437,40 +424,46 @@ class Manager {
|
|
|
|
|
|
for (let i = 0; i < characteristics.length; i++) {
|
|
|
var charc = characteristics[i];
|
|
|
- var properties = charc.properties;
|
|
|
- var charaterId = charc.uuid;
|
|
|
///可以开始读取数据
|
|
|
- if (properties.notify) {
|
|
|
- var notifyCharaterId = charaterId;
|
|
|
- getMessageCall(notifyCharaterId);
|
|
|
+ if (charc.properties.notify) {
|
|
|
+ console.log("gadsfasdfadfaf===zzz==");
|
|
|
+ getMessageCall(charc.uuid);
|
|
|
}
|
|
|
|
|
|
///可以开始写入数据
|
|
|
- if (properties.write || properties.writeWithoutResponse) {
|
|
|
- that.writeServiceId = serviceId;
|
|
|
- that.writeCharaterId = charaterId;
|
|
|
+ if (charc.properties.write || charc.properties.writeWithoutResponse) {
|
|
|
+ console.log("gadsfasdfadfaf===yyy==" + charc.properties.write + "===" + charc.properties.writeWithoutResponse);
|
|
|
+ that.writeCharaterId = charc.uuid;
|
|
|
+ console.log("gadsfasdfadfaf===uuu==" + that.writeServiceId + "===" + that.writeCharaterId);
|
|
|
setTimeout(() => {
|
|
|
successed();
|
|
|
}, 100);
|
|
|
}
|
|
|
}
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ failed();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- // 监听发送的数据
|
|
|
- monitorCharacteristicValueChange(device, notifyServiceId, notifyCharaterId) {
|
|
|
+ // 监听特征值变化
|
|
|
+ monitorCharacteristicValueChange(device, serviceId, notifyCharaterId) {
|
|
|
+ console.log("gadsfasdfadfaf===wwww==" + device.deviceId + "===" + serviceId +
|
|
|
+ "===" + notifyCharaterId);
|
|
|
return new Promise((resolve, reject) => {
|
|
|
var deviceId = device.deviceId;
|
|
|
wx.notifyBLECharacteristicValueChange({
|
|
|
state: true,
|
|
|
deviceId: deviceId,
|
|
|
- serviceId: notifyServiceId,
|
|
|
+ serviceId: serviceId,
|
|
|
characteristicId: notifyCharaterId,
|
|
|
success: function (res) {
|
|
|
+ console.log("gadsfasdfadfaf===eee==" + JSON.stringify(res));
|
|
|
resolve(true);
|
|
|
},
|
|
|
fail: function (res) {
|
|
|
+ console.log("gadsfasdfadfaf===rrr==" + JSON.stringify(res));
|
|
|
resolve(false);
|
|
|
}
|
|
|
});
|
|
@@ -479,9 +472,9 @@ class Manager {
|
|
|
|
|
|
///收取到的数据转换为文字
|
|
|
onBLECharacteristicValueChange(callback) {
|
|
|
- wx.onBLECharacteristicValueChange(function (r) {
|
|
|
+ wx.onBLECharacteristicValueChange(function (characteristic) {
|
|
|
console.log("gadsfasdfadfaf===666==" + JSON.stringify(r));
|
|
|
- var receiveText = hex_util.buf2string(r);
|
|
|
+ var receiveText = hex_util.buf2string(characteristic);
|
|
|
console.log("gadsfasdfadfaf===777==" + receiveText);
|
|
|
let buffer = characteristic.value
|
|
|
let dataView = new DataView(buffer)
|
|
@@ -513,25 +506,28 @@ class Manager {
|
|
|
sendData(device, data) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
var that = this;
|
|
|
- var deviceId = device.deviceId;
|
|
|
- var buffer = new ArrayBuffer(data.length);
|
|
|
+ var length = data.length;
|
|
|
+ console.log("gadsfasdfadfaf===hhh==" + length + "===" + data);
|
|
|
+ var buffer = new ArrayBuffer(length);
|
|
|
// 下面是赋值,不能删
|
|
|
const dataView = new DataView(buffer);
|
|
|
-
|
|
|
- for (var i = 0; i < data.length; i++) {
|
|
|
+ for (var i = 0; i < length; i++) {
|
|
|
+ console.log("gadsfasdfadfaf===iii==" + i + "===" + data[i]);
|
|
|
dataView.setUint8(i, data[i]);
|
|
|
}
|
|
|
// data.forEach((value, index) => {
|
|
|
// dataView.setUint8(index, value); // 将每个16进制数值写入到 buffer 中
|
|
|
// });
|
|
|
|
|
|
- console.log("gadsfasdfadfaf===bbb==" + deviceId);
|
|
|
+
|
|
|
console.log("gadsfasdfadfaf===ccc==" + that.writeServiceId);
|
|
|
console.log("gadsfasdfadfaf===ddd==" + that.writeCharaterId);
|
|
|
- console.log("gadsfasdfadfaf===eee==" + JSON.stringify(buffer));
|
|
|
+ console.log("gadsfasdfadfaf===eee==" + buffer);
|
|
|
|
|
|
// serviceId: that.publicDevice.serviceId, //服务通道,这里主要是notify
|
|
|
// characteristicId: characteristicId, //notify uuid
|
|
|
+ var deviceId = device.deviceId;
|
|
|
+ console.log("gadsfasdfadfaf===bbb==" + deviceId);
|
|
|
wx.writeBLECharacteristicValue({
|
|
|
deviceId: deviceId,
|
|
|
serviceId: that.writeServiceId,
|