|
|
@@ -209,6 +209,7 @@
|
|
|
// 监听搜索到新设备
|
|
|
searchWatch() {
|
|
|
let that = this;
|
|
|
+ that.watchs = [];
|
|
|
plus.bluetooth.startBluetoothDevicesDiscovery({
|
|
|
// services: ['heart_rate'],//ios不支持
|
|
|
success: function (e) {
|
|
|
@@ -217,9 +218,9 @@
|
|
|
let devices = e.devices;
|
|
|
for (let i in devices) {
|
|
|
let device = devices[i];
|
|
|
- console.log(JSON.stringify(device));
|
|
|
// localName CL831
|
|
|
if (device.localName.includes('CL831')) {
|
|
|
+ console.log(JSON.stringify(device));
|
|
|
that.watchs.push(device)
|
|
|
}
|
|
|
}
|
|
|
@@ -234,7 +235,88 @@
|
|
|
choiceWatch(w) {
|
|
|
let that = this;
|
|
|
that.Toast('连接心率带' + w.localName + '中...');
|
|
|
-
|
|
|
+ // 建立连接
|
|
|
+ plus.bluetooth.createBLEConnection({
|
|
|
+ deviceId: w.deviceId,
|
|
|
+ success: function (e) {
|
|
|
+ that.Toast('连接成功!');
|
|
|
+ // 渲染完毕再执行这个,所以才能没有延时的更改
|
|
|
+ // 需要一个回调等待5S
|
|
|
+ clearTimeout(that.timer); //清除延迟执行
|
|
|
+ that.timer = setTimeout(() => { //设置延迟执行
|
|
|
+ // 获取服务
|
|
|
+ that.getServices(w);
|
|
|
+ }, 5000);
|
|
|
+ },
|
|
|
+ fail: function (e) {
|
|
|
+ that.Toast('连接失败! ' + JSON.stringify(e));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ getServices(w) {
|
|
|
+ let that = this;
|
|
|
+ that.Toast('获取蓝牙设备服务:');
|
|
|
+ console.log(w.deviceId);
|
|
|
+ plus.bluetooth.getBLEDeviceServices({
|
|
|
+ deviceId: w.deviceId,
|
|
|
+ success: function (e) {
|
|
|
+ that.Toast('获取服务成功!');
|
|
|
+ // 获取服务的特征值
|
|
|
+ // console.log('服务的特征值' + w.advertisServiceUUIDs[0]);
|
|
|
+ var uuid = "0000180D-0000-1000-8000-00805F9B34FB";//HEART RATE
|
|
|
+ that.getCharacteristics(w.deviceId, uuid);
|
|
|
+ },
|
|
|
+ fail: function (e) {
|
|
|
+ console.log('获取服务失败! ' + JSON.stringify(e));
|
|
|
+ that.Toast('获取服务失败! ' + JSON.stringify(e));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ // 获取服务的特征值
|
|
|
+ getCharacteristics(deviceId, serviceId) {
|
|
|
+ let that = this;
|
|
|
+ let chaaracterUuid = '00002A37-0000-1000-8000-00805F9B34FB';
|
|
|
+ let characteristicId = null;
|
|
|
+ plus.bluetooth.getBLEDeviceCharacteristics({
|
|
|
+ deviceId: deviceId,
|
|
|
+ serviceId: serviceId,
|
|
|
+ success: function (e) {
|
|
|
+ let characteristics = e.characteristics;
|
|
|
+ that.Toast('获取特征值成功! ' + characteristics.length);
|
|
|
+ plus.bluetooth.notifyBLECharacteristicValueChange({ //监听数据变化
|
|
|
+ deviceId: deviceId,
|
|
|
+ serviceId: serviceId,
|
|
|
+ characteristicId: chaaracterUuid,
|
|
|
+ success: function (e) {
|
|
|
+ that.Toast('notifyBLECharacteristicValueChange success.');
|
|
|
+ that.readValue(deviceId, serviceId, chaaracterUuid)
|
|
|
+ },
|
|
|
+ fail: function (e) {
|
|
|
+ that.Toast('notifyBLECharacteristicValueChange failed! ' + JSON.stringify(e));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ fail: function (e) {
|
|
|
+ console.log('获取特征值失败! ' + JSON.stringify(e));
|
|
|
+ that.Toast('获取特征值失败! ' + JSON.stringify(e));
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ readValue(deviceId, serviceId, characteristicId) {
|
|
|
+ let that = this;
|
|
|
+ plus.bluetooth.readBLECharacteristicValue({
|
|
|
+ deviceId: deviceId,
|
|
|
+ serviceId: serviceId,
|
|
|
+ characteristicId: characteristicId,
|
|
|
+ success: function (e) {
|
|
|
+ that.Toast('读取数据成功!');
|
|
|
+ that.Toast(e)
|
|
|
+ console.log("读取数据成功" + e);
|
|
|
+ },
|
|
|
+ fail: function (e) {
|
|
|
+ that.Toast('读取数据失败! ' + JSON.stringify(e));
|
|
|
+ }
|
|
|
+ });
|
|
|
},
|
|
|
// 获取基础身体数据
|
|
|
getBodyInfo() {
|