时间:2019-05-03 来源:小程序工厂
王***:
getService 和 getCharacteristics 的逻辑麻烦贴一下
O***: /**
* 获取蓝牙设备serviceId
*/
getBleService() {
let that = this
return new Promise((resolve,reject)=>{
let deviceId = that.deviceInfo.deviceId
that.getBLEDeviceServices({
deviceId: deviceId
})
.then(res => {
let serviceId = ''
res.services.forEach((value, index) => {
if (value.isPrimary && value.uuid.toUpperCase() == that.services[0]) {
serviceId = value.uuid
that.serviceId = serviceId
}
})
resolve(that.code.GET_BLUETOOTH_SERVICES_SUCCESS)
})
.catch(e => {
if (e && e.code && e.code === 1001) {
reject(that.code.LOW_WX_VERSION)
} else {
reject(that.code.GET_BLUETOOTH_SERVICES_FAIL)
}
})
})
}
/**
* 获取蓝牙特征值
*/
getBleCharacteristics() {
let that = this
return new Promise((resolve,reject)=>{
let deviceId = that.deviceInfo.deviceId
let serviceId = that.serviceId
that.getBLEDeviceCharacteristics({
deviceId,
serviceId
})
.then(res => {
if (res.errCode == 0) {
let characteristics = res.characteristics
let propertyWrite = null
let propertyNotify = null
for (let j = 0; j < characteristics.length; j++) {
let properties = characteristics[j].properties
if (properties.write) {
propertyWrite = characteristics[j]
}
if (properties.notify) {
propertyNotify = characteristics[j]
}
if (propertyWrite && propertyNotify) {
that.propertyWrite = propertyWrite
that.propertyNotify = propertyNotify
break
}
}
resolve(that.code.GET_BLUETOOTH_CHARACTERISTIC_SUCCESS)
} else {
reject(that.code.GET_BLUETOOTH_CHARACTERISTIC_FAIL)
}
})
.catch(e => {
if (e && e.code && e.code === 1001) {
reject(that.code.LOW_WX_VERSION)
} else {
reject(that.code.GET_BLUETOOTH_CHARACTERISTIC_FAIL)
}
})
})
}
that.getBLEDeviceServices和that.getBLEDeviceCharacteristics是对微信api的promise的封装。
问题已经解决了,notify的uuid和write的uuid设置成不同的就可以了,硬件已经调整了。