|
|
@@ -139,7 +139,7 @@ const store = new Vuex.Store({
|
|
|
//选择连接的蓝牙设备
|
|
|
BLEConnectDevice: null,
|
|
|
//获取到的服务
|
|
|
- BLEGetServices:null,
|
|
|
+ BLEGetServices: null,
|
|
|
//当前发送给蓝牙的指令,目前是只有蓝牙手柄
|
|
|
currentInstruction: '',
|
|
|
|
|
|
@@ -158,6 +158,10 @@ const store = new Vuex.Store({
|
|
|
cIndex: -1,
|
|
|
//当前是否连接
|
|
|
bConnection: false,
|
|
|
+ /**
|
|
|
+ * 通过验证的连接,比如通过mac验证,或者说是首页直连,都设置这个参数,确保已经连上
|
|
|
+ */
|
|
|
+ bVerifiedConnection: false,
|
|
|
//最近连接过的设备
|
|
|
finallyUseDevice: null,
|
|
|
|
|
|
@@ -237,8 +241,24 @@ const store = new Vuex.Store({
|
|
|
|
|
|
//快速打击对象
|
|
|
filter: null,
|
|
|
- deviceMs: 1
|
|
|
+ deviceMs: 1,
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 蓝牙参数对象
|
|
|
+ */
|
|
|
+
|
|
|
+ // 设备Id
|
|
|
+ deviceId: "",
|
|
|
+ // 服务Id
|
|
|
+ serviceId: "",
|
|
|
+ notifyCharacteristicId: "",
|
|
|
+ writeCharacteristicId: "",
|
|
|
+ currentInstruction: "",
|
|
|
+ showToast: false,
|
|
|
+ bListenerUpdate: false,
|
|
|
+ bListenerJson: false,
|
|
|
+ bListenerMac: false,
|
|
|
},
|
|
|
mutations: {
|
|
|
|
|
|
@@ -1074,7 +1094,7 @@ const store = new Vuex.Store({
|
|
|
state.ConnectBindingDevice = null;
|
|
|
//蓝牙设备
|
|
|
state.BLEConnectDevice = null;
|
|
|
-
|
|
|
+
|
|
|
//蓝牙服务
|
|
|
state.BLEGetServices = null;
|
|
|
|
|
|
@@ -1215,20 +1235,15 @@ const store = new Vuex.Store({
|
|
|
|
|
|
|
|
|
initAdapter(state, callback) {
|
|
|
-
|
|
|
- // if (state.bOpenBluetooth) return;
|
|
|
-
|
|
|
//初始化蓝牙模块
|
|
|
- BLE.openBluetoothAdapter({
|
|
|
+ let params = {
|
|
|
success: (res) => {
|
|
|
state.bOpenSuccess = true;
|
|
|
state.bOpenBluetooth = true;
|
|
|
if (callback)
|
|
|
callback(res);
|
|
|
-
|
|
|
//监听断开事件
|
|
|
- BLE.onBLEConnectionStateChange();
|
|
|
-
|
|
|
+ this.commit("B_OnBLEConnectionStateChange");
|
|
|
},
|
|
|
fail: (fail) => {
|
|
|
state.bOpenSuccess = false;
|
|
|
@@ -1253,9 +1268,10 @@ const store = new Vuex.Store({
|
|
|
})
|
|
|
|
|
|
}
|
|
|
- });
|
|
|
+ };
|
|
|
+ this.commit("B_OpenBluetoothAdapter", params);
|
|
|
},
|
|
|
-
|
|
|
+ // todo 未使用到
|
|
|
onGetDevices(state, context) {
|
|
|
let {
|
|
|
success
|
|
|
@@ -1279,9 +1295,10 @@ const store = new Vuex.Store({
|
|
|
}
|
|
|
|
|
|
setTimeout(() => {
|
|
|
- BLE.closeBLEConnection(deviceId);
|
|
|
+ this.commit("B_CloseBLEConnection",{deviceId:deviceId})
|
|
|
state.cIndex = -1;
|
|
|
state.bConnection = false;
|
|
|
+ state.bVerifiedConnection = false;
|
|
|
}, 1000)
|
|
|
|
|
|
} else {
|
|
|
@@ -1306,6 +1323,28 @@ const store = new Vuex.Store({
|
|
|
state.getBLEDeviceServicesTimeout = null;
|
|
|
}
|
|
|
},
|
|
|
+
|
|
|
+ initBLEData(state) {
|
|
|
+ // 设备Id
|
|
|
+ state.deviceId = "";
|
|
|
+ // 服务Id
|
|
|
+ state.serviceId = "";
|
|
|
+
|
|
|
+ state.notifyCharacteristicId = "";
|
|
|
+
|
|
|
+ state.writeCharacteristicId = "";
|
|
|
+
|
|
|
+ state.currentInstruction = "";
|
|
|
+
|
|
|
+ state.showToast = false;
|
|
|
+
|
|
|
+ state.bListenerUpdate = false;
|
|
|
+
|
|
|
+ state.bListenerJson = false;
|
|
|
+
|
|
|
+ state.bListenerMac = false;
|
|
|
+
|
|
|
+ },
|
|
|
//连接蓝牙
|
|
|
onCreateBLEConnection(state, context) {
|
|
|
let {
|
|
|
@@ -1316,77 +1355,49 @@ const store = new Vuex.Store({
|
|
|
getinitAdapter = null
|
|
|
} = context;
|
|
|
|
|
|
- BLE.createBLEConnection(item, success => {
|
|
|
+ let params = {
|
|
|
+ item:item,
|
|
|
+ success:(success) => {
|
|
|
state.bConnection = true;
|
|
|
if (state.getBLEDeviceServicesTimeout) {
|
|
|
clearTimeout(state.getBLEDeviceServicesTimeout);
|
|
|
state.getBLEDeviceServicesTimeout = null;
|
|
|
}
|
|
|
-
|
|
|
- state.getBLEDeviceServicesTimeout = setTimeout(function() {
|
|
|
- BLE.getBLEDeviceServices(item, () => {
|
|
|
- uni.showToast({
|
|
|
- title: '连接成功',
|
|
|
- icon: 'loading',
|
|
|
- duration: 2000,
|
|
|
- mask: true
|
|
|
- })
|
|
|
- //是否需要设置item,默认不设置
|
|
|
- if (initItem) {
|
|
|
- state.cIndex = index;
|
|
|
- state.BLEConnectDevice = item;
|
|
|
+
|
|
|
+ state.getBLEDeviceServicesTimeout = setTimeout(() => {
|
|
|
+ this.commit("B_GetBLEDeviceServices", {
|
|
|
+ item:item,
|
|
|
+ callback:() => {
|
|
|
+ uni.showToast({
|
|
|
+ title: '连接成功',
|
|
|
+ icon: 'loading',
|
|
|
+ duration: 2000,
|
|
|
+ mask: true
|
|
|
+ })
|
|
|
+ //是否需要设置item,默认不设置
|
|
|
+ if (initItem) {
|
|
|
+ state.cIndex = index;
|
|
|
+ state.BLEConnectDevice = item;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 直接连接过成功
|
|
|
+ */
|
|
|
+ state.bVerifiedConnection = true;
|
|
|
+ if (getSuccess)
|
|
|
+ getSuccess();
|
|
|
+ },
|
|
|
+ getServiceList:(serviceList) => {
|
|
|
+ //搜索服务失败后
|
|
|
+ state.BLEGetServices = serviceList;
|
|
|
}
|
|
|
- if (getSuccess)
|
|
|
- getSuccess();
|
|
|
- },(serviceList)=>{
|
|
|
- //搜索服务失败后
|
|
|
- state.BLEGetServices = serviceList;
|
|
|
});
|
|
|
+
|
|
|
}, 2000);
|
|
|
- // this.commit('onGetDevices', {
|
|
|
- // success: () => {
|
|
|
-
|
|
|
- // if (state.getBLEDeviceServicesTimeout) {
|
|
|
- // clearTimeout(state.getBLEDeviceServicesTimeout);
|
|
|
- // state.getBLEDeviceServicesTimeout = null;
|
|
|
- // }
|
|
|
-
|
|
|
- // state.getBLEDeviceServicesTimeout = setTimeout(function() {
|
|
|
- // BLE.getBLEDeviceServices(item, () => {
|
|
|
- // uni.showToast({
|
|
|
- // title: '连接成功',
|
|
|
- // icon: 'loading',
|
|
|
- // duration: 2000,
|
|
|
- // mask: true
|
|
|
- // })
|
|
|
- // //是否需要设置item,默认不设置
|
|
|
- // if (initItem) {
|
|
|
- // state.cIndex = index;
|
|
|
- // state.BLEConnectDevice = item;
|
|
|
- // }
|
|
|
- // state.bConnection = true;
|
|
|
- // if (getSuccess)
|
|
|
- // getSuccess();
|
|
|
- // });
|
|
|
- // }, 500);
|
|
|
-
|
|
|
- // // uni.$emit('connectionSuccess');
|
|
|
-
|
|
|
- // if (state.BLERSSIInterval) {
|
|
|
- // clearInterval(state.BLERSSIInterval);
|
|
|
- // state.BLERSSIInterval = null;
|
|
|
- // }
|
|
|
- // state.BLERSSIInterval = setInterval(() => {
|
|
|
- // this.commit('onGetDevices', {
|
|
|
- // success: () => {}
|
|
|
- // });
|
|
|
- // }, 1000)
|
|
|
- // }
|
|
|
- // })
|
|
|
|
|
|
},
|
|
|
- fail => {
|
|
|
- console.log("***fail:",fail);
|
|
|
+ fail:(fail) => {
|
|
|
+ console.log("***fail:", fail);
|
|
|
if (fail.errCode === 10012) {
|
|
|
console.log("连接超时,请重试!");
|
|
|
uni.showToast({
|
|
|
@@ -1430,39 +1441,26 @@ const store = new Vuex.Store({
|
|
|
mask: true
|
|
|
})
|
|
|
}
|
|
|
- });
|
|
|
+ }
|
|
|
+ };
|
|
|
+ this.commit("B_CreateBLEConnection", params);
|
|
|
},
|
|
|
- //直接处理成功
|
|
|
+ //devices hardware 连接后验证,这里不需要处理 bVerifiedConnection,通过mac 验证后处理
|
|
|
onCreateBLESuccess(state, context) {
|
|
|
let {
|
|
|
item,
|
|
|
getSuccess = null
|
|
|
} = context;
|
|
|
-
|
|
|
- BLE.createBLEConnection(item, success => {
|
|
|
+ this.commit("B_CreateBLEConnection", {
|
|
|
+ item:item,
|
|
|
+ success:success => {
|
|
|
state.BLEConnectDevice = item;
|
|
|
state.bConnection = true;
|
|
|
if (getSuccess) {
|
|
|
getSuccess();
|
|
|
}
|
|
|
-
|
|
|
- // if (state.BLERSSIInterval) {
|
|
|
- // clearInterval(state.BLERSSIInterval);
|
|
|
- // state.BLERSSIInterval = null;
|
|
|
- // }
|
|
|
- // state.BLERSSIInterval = setInterval(() => {
|
|
|
- // this.commit('onGetDevices', {
|
|
|
- // success: () => {}
|
|
|
- // });
|
|
|
- // }, 1000)
|
|
|
-
|
|
|
- // uni.showToast({
|
|
|
- // icon: "none",
|
|
|
- // title: "连接设备成功!",
|
|
|
- // duration: 2000
|
|
|
- // })
|
|
|
},
|
|
|
- fail => {
|
|
|
+ fail:fail => {
|
|
|
if (fail.errCode === 10012) {
|
|
|
console.log("连接超时,请重试!");
|
|
|
uni.showToast({
|
|
|
@@ -1500,7 +1498,8 @@ const store = new Vuex.Store({
|
|
|
// mask: true
|
|
|
// })
|
|
|
}
|
|
|
- });
|
|
|
+ }
|
|
|
+ });
|
|
|
},
|
|
|
//获取蓝牙服务
|
|
|
onGetBLEDeviceServices(state, context) {
|
|
|
@@ -1508,9 +1507,13 @@ const store = new Vuex.Store({
|
|
|
item,
|
|
|
success = null,
|
|
|
} = context;
|
|
|
- BLE.getBLEDeviceServices(item, success,(serviceList)=>{
|
|
|
- state.BLEGetServices = serviceList;
|
|
|
- });
|
|
|
+ this.commit("B_GetBLEDeviceServices", {
|
|
|
+ item:item,
|
|
|
+ callback:success,
|
|
|
+ getServiceList:(serviceList) => {
|
|
|
+ state.BLEGetServices = serviceList;
|
|
|
+ }
|
|
|
+ })
|
|
|
},
|
|
|
onCloseBLEConnection(state, context) {
|
|
|
|
|
|
@@ -1523,27 +1526,30 @@ const store = new Vuex.Store({
|
|
|
clearInterval(state.BLERSSIInterval);
|
|
|
state.BLERSSIInterval = null;
|
|
|
}
|
|
|
+ this.commit("B_CloseBLEConnection", {
|
|
|
+ deviceId:state.BLEConnectDevice.deviceId,
|
|
|
+ success:() => {
|
|
|
+ state.cIndex = -1;
|
|
|
+ state.BLEConnectDevice = null;
|
|
|
+ //蓝牙服务
|
|
|
+ state.BLEGetServices = null;
|
|
|
|
|
|
- BLE.closeBLEConnection(state.BLEConnectDevice.deviceId, () => {
|
|
|
- state.cIndex = -1;
|
|
|
- state.BLEConnectDevice = null;
|
|
|
- //蓝牙服务
|
|
|
- state.BLEGetServices = null;
|
|
|
+ if (getSuccess) {
|
|
|
+ getSuccess();
|
|
|
+ }
|
|
|
+ state.bConnection = false;
|
|
|
+
|
|
|
+ state.bVerifiedConnection = false;
|
|
|
|
|
|
- if (getSuccess) {
|
|
|
- getSuccess();
|
|
|
+ uni.closeBluetoothAdapter({
|
|
|
+ success(res) {
|
|
|
+ console.log(res)
|
|
|
+ state.bOpenBluetooth = false;
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
- state.bConnection = false;
|
|
|
-
|
|
|
- uni.closeBluetoothAdapter({
|
|
|
- success(res) {
|
|
|
- console.log(res)
|
|
|
- state.bOpenBluetooth = false;
|
|
|
- }
|
|
|
- })
|
|
|
});
|
|
|
|
|
|
-
|
|
|
},
|
|
|
//不关闭 adapter
|
|
|
onOnlyCloseBLEConnection(state, context) {
|
|
|
@@ -1556,24 +1562,28 @@ const store = new Vuex.Store({
|
|
|
state.BLERSSIInterval = null;
|
|
|
}
|
|
|
|
|
|
- BLE.closeBLEConnection(state.BLEConnectDevice.deviceId, () => {
|
|
|
- state.cIndex = -1;
|
|
|
- state.BLEConnectDevice = null;
|
|
|
- //蓝牙服务
|
|
|
- state.BLEGetServices = null;
|
|
|
-
|
|
|
- if (getSuccess) {
|
|
|
- getSuccess();
|
|
|
+ this.commit("B_CloseBLEConnection", {
|
|
|
+ deviceId: state.BLEConnectDevice.deviceId,
|
|
|
+ success:() => {
|
|
|
+ state.cIndex = -1;
|
|
|
+ state.BLEConnectDevice = null;
|
|
|
+ //蓝牙服务
|
|
|
+ state.BLEGetServices = null;
|
|
|
+ if (getSuccess) {
|
|
|
+ getSuccess();
|
|
|
+ }
|
|
|
+ state.bConnection = false;
|
|
|
+
|
|
|
+ state.bVerifiedConnection = false;
|
|
|
}
|
|
|
- state.bConnection = false;
|
|
|
});
|
|
|
|
|
|
},
|
|
|
onWriteBLEConnectionValue(state, context) {
|
|
|
let {
|
|
|
getSuccess = null,
|
|
|
- getFail = null,
|
|
|
- value
|
|
|
+ getFail = null,
|
|
|
+ value
|
|
|
} = context;
|
|
|
|
|
|
if (!state.BLEConnectDevice && state.cIndex == -1) {
|
|
|
@@ -1598,9 +1608,8 @@ const store = new Vuex.Store({
|
|
|
} else if (value == "6") {
|
|
|
state.instructionState.bSteps = false;
|
|
|
}
|
|
|
- let retryCount = 3;
|
|
|
-
|
|
|
- BLE.writeBLECharacteristicValue(value,retryCount,getSuccess,getFail);
|
|
|
+ let retryCount = 4;
|
|
|
+ this.commit("B_WriteBLECharacteristicValue",{value, retryCount, getSuccess, getFail});
|
|
|
},
|
|
|
//获取距离
|
|
|
onGetRSSITransDistance(state, context) {
|
|
|
@@ -1784,6 +1793,416 @@ const store = new Vuex.Store({
|
|
|
|
|
|
},
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 蓝牙BLE迁移到store.js
|
|
|
+ */
|
|
|
+ // 初始化蓝牙模块适配器
|
|
|
+ B_OpenBluetoothAdapter(state, context) {
|
|
|
+ let {
|
|
|
+ success,
|
|
|
+ fail,
|
|
|
+ complete
|
|
|
+ } = context;
|
|
|
+ //#ifdef H5
|
|
|
+ console.warn('h5不加载蓝牙模块');
|
|
|
+ return;
|
|
|
+ //#endif
|
|
|
+ uni.openBluetoothAdapter({
|
|
|
+ success: res => {
|
|
|
+ if (success)
|
|
|
+ success(res);
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
+ if (fail)
|
|
|
+ fail(res);
|
|
|
+ },
|
|
|
+ complete: res => {
|
|
|
+ if (complete)
|
|
|
+ complete(res);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关闭蓝牙模块适配器
|
|
|
+ */
|
|
|
+ B_CloseBluetoothAdapter() {
|
|
|
+ //#ifdef H5
|
|
|
+ console.warn('h5不加载蓝牙模块');
|
|
|
+ return;
|
|
|
+ //#endif
|
|
|
+ uni.closeBluetoothAdapter({
|
|
|
+ success: res => {
|
|
|
+ console.log('断开蓝牙模块成功');
|
|
|
+ uni.showToast({
|
|
|
+ icon: "none",
|
|
|
+ title: "蓝牙已经断开!",
|
|
|
+ mask: false,
|
|
|
+ duration: 3000
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ B_OnBLEConnectionStateChange(state) {
|
|
|
+ //#ifdef H5
|
|
|
+ console.warn('h5不加载蓝牙模块');
|
|
|
+ return;
|
|
|
+ //#endif
|
|
|
+ uni.onBLEConnectionStateChange(res => {
|
|
|
+ // 该方法回调中可以用于处理连接意外断开等异常情况
|
|
|
+ console.log(`蓝牙连接状态 -------------------------->`, JSON.stringify(res));
|
|
|
+ if (!res.connected) {
|
|
|
+ uni.$emit('callbackCloseBLE');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 连接低功耗蓝牙设备。
|
|
|
+ * 若APP在之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的 deviceId 直接尝试连接该设备,无需进行搜索操作。
|
|
|
+ */
|
|
|
+ B_CreateBLEConnection(state, context) {
|
|
|
+ let {
|
|
|
+ item,
|
|
|
+ success,
|
|
|
+ fail
|
|
|
+ } = context;
|
|
|
+ this.commit('initBLEData');
|
|
|
+ state.deviceId = item.deviceId;
|
|
|
+ let timeout = 10000;
|
|
|
+ uni.createBLEConnection({
|
|
|
+ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
|
|
|
+ deviceId: state.deviceId,
|
|
|
+ timeout,
|
|
|
+ success: res => {
|
|
|
+ if (success) {
|
|
|
+ success(res);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
+ if (fail)
|
|
|
+ fail(res);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取设备的服务ID
|
|
|
+ */
|
|
|
+ B_GetBLEDeviceServices(state, context) {
|
|
|
+
|
|
|
+ let {
|
|
|
+ item,
|
|
|
+ callback,
|
|
|
+ getServiceList
|
|
|
+ } = context;
|
|
|
+ let serviceList = [];
|
|
|
+ let _self = this;
|
|
|
+ uni.getBLEDeviceServices({
|
|
|
+ deviceId: item.deviceId,
|
|
|
+ success: res => {
|
|
|
+ console.log("getBLEDeviceServices==", JSON.stringify(res), item);
|
|
|
+ serviceList = res.services;
|
|
|
+ if (serviceList.length == 0) {
|
|
|
+ uni.hideToast();
|
|
|
+ uni.showModal({
|
|
|
+ title: '失败',
|
|
|
+ content: '获取服务失败,请尝试重新连接对应的模式。'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ if (getServiceList) {
|
|
|
+ getServiceList(serviceList);
|
|
|
+ }
|
|
|
+ for (let i = 0; i < serviceList.length; i++) {
|
|
|
+ let service = serviceList[i];
|
|
|
+ if (service.uuid.toLocaleLowerCase() === item.PRIMARY_SERVICE.toLocaleLowerCase()) {
|
|
|
+ state.serviceId = service.uuid;
|
|
|
+ //开始获取指定服务的特征值
|
|
|
+ _self.commit('B_GetBLEDeviceCharacteristics', {
|
|
|
+ item:item,
|
|
|
+ callback:callback
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: failRes => {
|
|
|
+ console.log('device services:', failRes.services)
|
|
|
+ uni.showModal({
|
|
|
+ title: '连接失败',
|
|
|
+ content: '未获取到相应服务,请继续尝试连接设备。'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 获取指定服务的特征值
|
|
|
+ */
|
|
|
+ B_GetBLEDeviceCharacteristics(state, context) {
|
|
|
+ let {
|
|
|
+ item,
|
|
|
+ callback
|
|
|
+ } = context;
|
|
|
+ let deviceId = state.deviceId;
|
|
|
+ let serviceId = state.serviceId;
|
|
|
+ let characteristicsList = [];
|
|
|
+ uni.getBLEDeviceCharacteristics({
|
|
|
+ deviceId,
|
|
|
+ serviceId,
|
|
|
+ success: res => {
|
|
|
+ if (item.PRIMARY_NOTIFY != '') {
|
|
|
+ state.notifyCharacteristicId = item.PRIMARY_NOTIFY;
|
|
|
+ this.commit('B_NotifyBLECharacteristicValueChange', {
|
|
|
+ callback:callback
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (item.PRIMARY_WRITE != '') {
|
|
|
+ state.writeCharacteristicId = item.PRIMARY_WRITE;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
+ console.log('device getBLEDeviceCharacteristics failed:', JSON.stringify(res))
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ //启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。
|
|
|
+ //注意:必须设备的特征值支持notify或者indicate才可以成功调用,具体参照 characteristic 的 properties 属性
|
|
|
+ B_NotifyBLECharacteristicValueChange(state, context) {
|
|
|
+ let {
|
|
|
+ callback
|
|
|
+ } = context;
|
|
|
+ // 启用notify功能
|
|
|
+ // console.log("启用notify功能");
|
|
|
+ uni.notifyBLECharacteristicValueChange({
|
|
|
+ state: true,
|
|
|
+ deviceId: state.deviceId,
|
|
|
+ serviceId: state.serviceId,
|
|
|
+ characteristicId: state.notifyCharacteristicId,
|
|
|
+ success: (res) => {
|
|
|
+ this.commit('B_OnBLECharacteristicValueChange', {
|
|
|
+ callback:callback
|
|
|
+ });
|
|
|
+ // _self.onBLECharacteristicValueChange(callback); //监听特征值变化
|
|
|
+ },
|
|
|
+ fail: (res) => {
|
|
|
+ uni.showToast({
|
|
|
+ title: 'notify启动失败',
|
|
|
+ icon: "none",
|
|
|
+ mask: true
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ //监听低功耗蓝牙设备的特征值变化。必须先启用notify接口才能接收到设备推送的notification。
|
|
|
+ B_OnBLECharacteristicValueChange(state, context) {
|
|
|
+ let {
|
|
|
+ callback
|
|
|
+ } = context;
|
|
|
+ console.log("onBLECharacteristicValueChange success");
|
|
|
+ if (callback) {
|
|
|
+ callback({
|
|
|
+ notifyCharacteristicId: state.notifyCharacteristicId,
|
|
|
+ writeCharacteristicId: state.writeCharacteristicId
|
|
|
+ })
|
|
|
+ }
|
|
|
+ uni.onBLECharacteristicValueChange(function(res) {
|
|
|
+ let box = {};
|
|
|
+ var resValue = BLE.ab2hext(res.value); //16进制字符串
|
|
|
+ if (state.currentInstruction == "V" && state.showToast) {
|
|
|
+ var resValueStr = BLE.hexToString(resValue);
|
|
|
+ let _info = {
|
|
|
+ type: 'version',
|
|
|
+ instruction: 'V',
|
|
|
+ value: resValueStr.replace(/\{|}/g, '').trim()
|
|
|
+ }
|
|
|
+ uni.$emit('listenerBLE', _info);
|
|
|
+ state.currentInstruction = "";
|
|
|
+ state.showToast = false;
|
|
|
+ return;
|
|
|
+ } else if (state.currentInstruction == 'M' && state.bListenerMac) {
|
|
|
+ var resValueStr = BLE.hexToString(resValue);
|
|
|
+ let _info = {
|
|
|
+ type: 'mac',
|
|
|
+ instruction: 'M',
|
|
|
+ value: resValueStr.replace(/\{|}/g, '').trim()
|
|
|
+ }
|
|
|
+ uni.$emit('listenerBLE', _info);
|
|
|
+ state.currentInstruction = "";
|
|
|
+ state.bListenerMac = false;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //步数数据解析
|
|
|
+ if (state.bListenerJson) {
|
|
|
+ var resValueStr = BLE.hexToString(resValue);
|
|
|
+ console.log(resValueStr);
|
|
|
+ box["Json"] = resValueStr;
|
|
|
+ uni.$emit('updateBLEDeviceJson', box);
|
|
|
+ }
|
|
|
+ //原始数据解析
|
|
|
+ if (state.bListenerUpdate) {
|
|
|
+ //R/L 左手还是右手
|
|
|
+ let handle = resValue.substr(6, 2);
|
|
|
+ let axStr = resValue.substr(8, 4);
|
|
|
+ let ayStr = resValue.substr(12, 4);
|
|
|
+ let azStr = resValue.substr(16, 4);
|
|
|
+ //一个字节最大只能表示265 2个字节可以表示65536 然后你这边做一下转换 例如00 3E 3*16+14=62 ----->0.03 g
|
|
|
+ //(真实值= AD值 /32768 * 16)
|
|
|
+ //0.003 = 62 / 2768 * 16
|
|
|
+ let ax = BLE.hexToSignedInt(axStr) / 32768 * 16;
|
|
|
+ let ay = BLE.hexToSignedInt(ayStr) / 32768 * 16;
|
|
|
+ let az = BLE.hexToSignedInt(azStr) / 32768 * 16;
|
|
|
+
|
|
|
+ let gxStr = resValue.substr(20, 4);
|
|
|
+ let gyStr = resValue.substr(24, 4);
|
|
|
+ let gzStr = resValue.substr(28, 4);
|
|
|
+ //角速度(AD值/32768 * 2000) 角速度单位是 °/s 范围是 正负2000
|
|
|
+ let gx = BLE.hexToSignedInt(gxStr) / 32768 * 2000;
|
|
|
+ let gy = BLE.hexToSignedInt(gyStr) / 32768 * 2000;
|
|
|
+ let gz = BLE.hexToSignedInt(gzStr) / 32768 * 2000;
|
|
|
+ //毫秒
|
|
|
+ let ms = BLE.hex2int(resValue.substr(2, 4));
|
|
|
+ //分
|
|
|
+ let min = BLE.hex2int(resValue.substr(32, 2));
|
|
|
+ //秒
|
|
|
+ let s = BLE.hex2int(resValue.substr(34, 2));
|
|
|
+ box["handle"] = BLE.hexToString(handle);
|
|
|
+ box["acc"] = {
|
|
|
+ ax,
|
|
|
+ ay,
|
|
|
+ az
|
|
|
+ };
|
|
|
+ box["gyro"] = {
|
|
|
+ gx,
|
|
|
+ gy,
|
|
|
+ gz
|
|
|
+ };
|
|
|
+
|
|
|
+ box["min"] = min;
|
|
|
+ box["s"] = s;
|
|
|
+ box["ms"] = ms;
|
|
|
+
|
|
|
+ //更新数据给webview,在game-play-web||game-play-sub绑定
|
|
|
+ //后面更新数据都用此接口
|
|
|
+ uni.$emit('updateBLEDeviceData', box);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ //向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持 write 才可以成功调用。
|
|
|
+ B_WriteBLECharacteristicValue(state, context) {
|
|
|
+ let {
|
|
|
+ value,
|
|
|
+ retryCount,
|
|
|
+ success,
|
|
|
+ getFail
|
|
|
+ } = context;
|
|
|
+ if (!value && value === '') {
|
|
|
+ uni.showToast({
|
|
|
+ title: "指令为空"
|
|
|
+ })
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //记录一下指令
|
|
|
+ state.currentInstruction = value;
|
|
|
+ //V 获取版本,M 获取mac 地址
|
|
|
+ if (value == "V")
|
|
|
+ state.showToast = true;
|
|
|
+
|
|
|
+ if (value == "M") {
|
|
|
+ state.showToast = true;
|
|
|
+ state.bListenerMac = true;
|
|
|
+ }
|
|
|
+ if (value == "4") {
|
|
|
+ state.bListenerUpdate = false;
|
|
|
+ }
|
|
|
+ if (value == "3") {
|
|
|
+ if (state.bListenerUpdate) {
|
|
|
+ uni.showToast({
|
|
|
+ title: "原始数据已开启"
|
|
|
+ })
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ state.bListenerUpdate = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (value == "6") {
|
|
|
+ state.bListenerJson = false;
|
|
|
+ }
|
|
|
+ if (value == "5") {
|
|
|
+ if (state.bListenerJson) {
|
|
|
+ uni.showToast({
|
|
|
+ title: "步数数据已开启"
|
|
|
+ })
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ state.bListenerJson = true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ console.log("currentInstruction:", state.currentInstruction, value, state.bListenerUpdate, state.bListenerMac);
|
|
|
+ console.log("state.deviceId:", state.deviceId, state.serviceId, state.writeCharacteristicId);
|
|
|
+ uni.writeBLECharacteristicValue({
|
|
|
+ deviceId: state.deviceId,
|
|
|
+ serviceId: state.serviceId,
|
|
|
+ // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
|
|
|
+ characteristicId: state.writeCharacteristicId,
|
|
|
+ // 这里的value是ArrayBuffer类型
|
|
|
+ value: BLE.str2ab(value),
|
|
|
+ success: (res) => {
|
|
|
+ console.log('writeBLECharacteristicValue success', res.errMsg, value, retryCount);
|
|
|
+ if (success) {
|
|
|
+ success(res);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: (fail) => {
|
|
|
+ console.log(fail)
|
|
|
+ //重新写入
|
|
|
+ if (retryCount > 0) {
|
|
|
+ console.log("writeBLECharacteristicValue Rewrite ===================>" + value + " == " + retryCount);
|
|
|
+ retryCount--;
|
|
|
+ setTimeout(() => {
|
|
|
+ this.commit("B_WriteBLECharacteristicValue", {
|
|
|
+ value,
|
|
|
+ retryCount,
|
|
|
+ success,
|
|
|
+ getFail
|
|
|
+ });
|
|
|
+ }, 300);
|
|
|
+ }
|
|
|
+ if (getFail) {
|
|
|
+ getFail(fail);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 断开蓝牙连接
|
|
|
+ */
|
|
|
+ B_CloseBLEConnection(state, context) {
|
|
|
+ let {
|
|
|
+ deviceId,
|
|
|
+ success = null
|
|
|
+ } = context;
|
|
|
+ uni.closeBLEConnection({
|
|
|
+ deviceId,
|
|
|
+ success: res => {
|
|
|
+ if (success)
|
|
|
+ success();
|
|
|
+ },
|
|
|
+ fail: (fail) => {
|
|
|
+ console.log("关闭蓝牙失败", fail);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
})
|
|
|
|