|
@@ -312,9 +312,11 @@ class Manager {
|
|
wx.createBLEConnection({
|
|
wx.createBLEConnection({
|
|
deviceId: deviceId,
|
|
deviceId: deviceId,
|
|
success: function (res) {
|
|
success: function (res) {
|
|
|
|
+ console.log("gadsfasdfadfaf===mmm==" + JSON.stringify(res));
|
|
resolve(true);
|
|
resolve(true);
|
|
},
|
|
},
|
|
fail: function (res) {
|
|
fail: function (res) {
|
|
|
|
+ console.log("gadsfasdfadfaf===nnn==" + JSON.stringify(res));
|
|
var errCode = res.errCode;
|
|
var errCode = res.errCode;
|
|
var errMsg = res.errMsg;
|
|
var errMsg = res.errMsg;
|
|
if (errCode == -1 && errMsg == "createBLEConnection:fail:already connect") {
|
|
if (errCode == -1 && errMsg == "createBLEConnection:fail:already connect") {
|
|
@@ -327,6 +329,35 @@ class Manager {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 发现服务
|
|
|
|
+ discoverServices(device) {
|
|
|
|
+ var deviceId = device.deviceId;
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ wx.getBLEDeviceServices({
|
|
|
|
+ deviceId: deviceId,
|
|
|
|
+ success: (res) => {
|
|
|
|
+ let service_id = "";
|
|
|
|
+ for (let i = 0; i < res.services.length; i++) {
|
|
|
|
+ if (res.services[i].uuid.toUpperCase().indexOf("AB00") != -1 ||
|
|
|
|
+ res.services[i].uuid.toUpperCase().indexOf("FFC0") != -1
|
|
|
|
+ ) {
|
|
|
|
+ service_id = res.services[i].uuid;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ service_id = res.services[i].uuid;
|
|
|
|
+ }
|
|
|
|
+ // if (that.publicDevice) {
|
|
|
|
+ // that.publicDevice.serviceId = service_id;
|
|
|
|
+ // }
|
|
|
|
+ resolve(service_id);
|
|
|
|
+ },
|
|
|
|
+ fail: (err) => {
|
|
|
|
+ reject("");
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
///初始化 接收数据 获取特征值
|
|
///初始化 接收数据 获取特征值
|
|
getNotifyServices(device) {
|
|
getNotifyServices(device) {
|
|
var deviceId = device.deviceId;
|
|
var deviceId = device.deviceId;
|
|
@@ -334,67 +365,97 @@ class Manager {
|
|
wx.getBLEDeviceServices({
|
|
wx.getBLEDeviceServices({
|
|
deviceId: deviceId,
|
|
deviceId: deviceId,
|
|
success: function (res) {
|
|
success: function (res) {
|
|
- console.log("gadsfasdfadfaf===xxxx==" + JSON.stringify(res));
|
|
|
|
|
|
+ console.log("gadsfasdfadfaf===2222==" + JSON.stringify(res));
|
|
resolve(res.services);
|
|
resolve(res.services);
|
|
},
|
|
},
|
|
fail: function (res) {
|
|
fail: function (res) {
|
|
- console.log("gadsfasdfadfaf===yyy==" + JSON.stringify(res));
|
|
|
|
|
|
+ console.log("gadsfasdfadfaf===333==" + JSON.stringify(res));
|
|
resolve(null);
|
|
resolve(null);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 发现服务
|
|
|
|
+ discoverServices(device) {
|
|
|
|
+ var deviceId = device.deviceId;
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ wx.getBLEDeviceServices({
|
|
|
|
+ deviceId: deviceId,
|
|
|
|
+ success: (res) => {
|
|
|
|
+ let service_id = "";
|
|
|
|
+ for (let i = 0; i < res.services.length; i++) {
|
|
|
|
+ if (res.services[i].uuid.toUpperCase().indexOf("AB00") != -1 ||
|
|
|
|
+ res.services[i].uuid.toUpperCase().indexOf("FFC0") != -1
|
|
|
|
+ ) {
|
|
|
|
+ service_id = res.services[i].uuid;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ service_id = res.services[i].uuid;
|
|
|
|
+ }
|
|
|
|
+ resolve(service_id);
|
|
|
|
+ },
|
|
|
|
+ fail: (err) => {
|
|
|
|
+ console.error('发现服务失败:', err);
|
|
|
|
+ reject("");
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // 发现特征值 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("");
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
///获取设备真正的属性特征值
|
|
///获取设备真正的属性特征值
|
|
- getCharacteristics(device, services, index, notify, write, read, succeed, fail) {
|
|
|
|
|
|
+ getCharacteristics(device, serviceId, failed, getMessageCall, successed) {
|
|
var that = this;
|
|
var that = this;
|
|
var deviceId = device.deviceId;
|
|
var deviceId = device.deviceId;
|
|
wx.getBLEDeviceCharacteristics({
|
|
wx.getBLEDeviceCharacteristics({
|
|
deviceId: deviceId,
|
|
deviceId: deviceId,
|
|
- serviceId: services[index].uuid,
|
|
|
|
|
|
+ serviceId: serviceId,
|
|
success: function (res) {
|
|
success: function (res) {
|
|
- 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 (!notify) {
|
|
|
|
- if (properties.notify) {
|
|
|
|
- notifyCharaterId = charaterId;
|
|
|
|
- notifyServiceId = services[index].uuid;
|
|
|
|
- notify = true;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if (!write) {
|
|
|
|
- if (properties.write) {
|
|
|
|
- that.writeCharaterId = charaterId;
|
|
|
|
- that.writeServiceId = services[index].uuid;
|
|
|
|
- write = true;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ var characteristics = res.characteristics;
|
|
|
|
+ if (characteristics.length <= 0) {
|
|
|
|
+ failed();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
- if (!read) {
|
|
|
|
- if (properties.read) {
|
|
|
|
- that.readCharaterId = charaterId;
|
|
|
|
- that.readServiceId = services[index].uuid;
|
|
|
|
- read = true;
|
|
|
|
- }
|
|
|
|
|
|
+ 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 (!notify) {
|
|
|
|
- index++
|
|
|
|
- if (index == services.length) {
|
|
|
|
- fail();
|
|
|
|
- } else {
|
|
|
|
- that.getCharacteristics(device, services, index, notify, write, read, succeed, fail);
|
|
|
|
|
|
+ ///可以开始写入数据
|
|
|
|
+ if (properties.write || properties.writeWithoutResponse) {
|
|
|
|
+ that.writeServiceId = serviceId;
|
|
|
|
+ that.writeCharaterId = charaterId;
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ successed();
|
|
|
|
+ }, 100);
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- succeed(notifyServiceId, notifyCharaterId);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- })
|
|
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
// 监听发送的数据
|
|
// 监听发送的数据
|
|
@@ -419,7 +480,16 @@ class Manager {
|
|
///收取到的数据转换为文字
|
|
///收取到的数据转换为文字
|
|
onBLECharacteristicValueChange(callback) {
|
|
onBLECharacteristicValueChange(callback) {
|
|
wx.onBLECharacteristicValueChange(function (r) {
|
|
wx.onBLECharacteristicValueChange(function (r) {
|
|
- callback(r.value);
|
|
|
|
|
|
+ console.log("gadsfasdfadfaf===666==" + JSON.stringify(r));
|
|
|
|
+ var receiveText = hex_util.buf2string(r);
|
|
|
|
+ console.log("gadsfasdfadfaf===777==" + receiveText);
|
|
|
|
+ let buffer = characteristic.value
|
|
|
|
+ let dataView = new DataView(buffer)
|
|
|
|
+ let dataResult = []
|
|
|
|
+ for (let i = 0; i < dataView.byteLength; i++) {
|
|
|
|
+ dataResult.push(dataView.getUint8(i))
|
|
|
|
+ }
|
|
|
|
+ callback(dataResult);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
@@ -440,16 +510,28 @@ class Manager {
|
|
}
|
|
}
|
|
|
|
|
|
///发送数据
|
|
///发送数据
|
|
- sendData(device, text) {
|
|
|
|
|
|
+ sendData(device, data) {
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
var that = this;
|
|
var that = this;
|
|
var deviceId = device.deviceId;
|
|
var deviceId = device.deviceId;
|
|
- var buffer = new ArrayBuffer(text.length)
|
|
|
|
- var dataView = new Uint8Array(buffer);
|
|
|
|
|
|
+ var buffer = new ArrayBuffer(data.length);
|
|
|
|
+ // 下面是赋值,不能删
|
|
|
|
+ const dataView = new DataView(buffer);
|
|
|
|
|
|
- for (var i = 1; i < text.length; i++) {
|
|
|
|
- dataView[i] = text.charCodeAt(i)
|
|
|
|
|
|
+ for (var i = 0; i < data.length; 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));
|
|
|
|
+
|
|
|
|
+ // serviceId: that.publicDevice.serviceId, //服务通道,这里主要是notify
|
|
|
|
+ // characteristicId: characteristicId, //notify uuid
|
|
wx.writeBLECharacteristicValue({
|
|
wx.writeBLECharacteristicValue({
|
|
deviceId: deviceId,
|
|
deviceId: deviceId,
|
|
serviceId: that.writeServiceId,
|
|
serviceId: that.writeServiceId,
|
|
@@ -468,6 +550,65 @@ class Manager {
|
|
|
|
|
|
module.exports = Manager;
|
|
module.exports = Manager;
|
|
|
|
|
|
|
|
+///获取设备真正的属性特征值
|
|
|
|
+// getCharacteristics(device, services, index, notify, write, read, succeed, fail) {
|
|
|
|
+// var that = this;
|
|
|
|
+// var deviceId = device.deviceId;
|
|
|
|
+// var serviceId = services[index].uuid;
|
|
|
|
+// wx.getBLEDeviceCharacteristics({
|
|
|
|
+// deviceId: deviceId,
|
|
|
|
+// serviceId: serviceId,
|
|
|
|
+// success: function (res) {
|
|
|
|
+// var notifyCharaterId = "";
|
|
|
|
+// var notifyServiceId = "";
|
|
|
|
+
|
|
|
|
+// console.log("gadsfasdfadfaf===xxxx==" + JSON.stringify(res.characteristics));
|
|
|
|
+// for (var i = 0; i < res.characteristics.length; i++) {
|
|
|
|
+// var properties = res.characteristics[i].properties;
|
|
|
|
+// var charaterId = res.characteristics[i].uuid;
|
|
|
|
+
|
|
|
|
+// if (!notify) {
|
|
|
|
+// if (properties.notify) {
|
|
|
|
+// notifyCharaterId = charaterId;
|
|
|
|
+// notifyServiceId = services[index].uuid;
|
|
|
|
+// console.log("gadsfasdfadfaf===xxxx==" + properties.write);
|
|
|
|
+// console.log("gadsfasdfadfaf===yyy==" + properties.writeWithoutResponse);
|
|
|
|
+// console.log("gadsfasdfadfaf===zzz==" + JSON.stringify(res.characteristics[i]));
|
|
|
|
+// notify = true;
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// if (!write) {
|
|
|
|
+// if (properties.write) {
|
|
|
|
+// that.writeCharaterId = charaterId;
|
|
|
|
+// that.writeServiceId = services[index].uuid;
|
|
|
|
+// write = true;
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+// if (!read) {
|
|
|
|
+// if (properties.read) {
|
|
|
|
+// that.readCharaterId = charaterId;
|
|
|
|
+// that.readServiceId = services[index].uuid;
|
|
|
|
+// read = true;
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+// if (!notify) {
|
|
|
|
+// index++
|
|
|
|
+// if (index == services.length) {
|
|
|
|
+// fail();
|
|
|
|
+// } else {
|
|
|
|
+// console.log("gadsfasdfadfaf===yyy==");
|
|
|
|
+// that.getCharacteristics(device, services, index, notify, write, read, succeed, fail);
|
|
|
|
+// }
|
|
|
|
+// } else {
|
|
|
|
+// succeed(notifyServiceId, notifyCharaterId);
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// })
|
|
|
|
+// }
|
|
|
|
+
|
|
///获取 所有搜索过的蓝牙设备
|
|
///获取 所有搜索过的蓝牙设备
|
|
// getConnectedDevices() {
|
|
// getConnectedDevices() {
|
|
// var that = this;
|
|
// var that = this;
|