feat: add user deactivation and vendor BLE protocol
这个提交包含在:
@@ -81,6 +81,17 @@ function handleNotification(frame) {
|
||||
}
|
||||
}
|
||||
|
||||
function handleValueChange(res) {
|
||||
var frame = protocol.parseFrame(res.value)
|
||||
if (frame) {
|
||||
handleNotification(frame)
|
||||
return
|
||||
}
|
||||
if (protocol.PROTOCOL_MODE === 'vendor_33') {
|
||||
emit('status', protocol.parseVendorStatus(res.value))
|
||||
}
|
||||
}
|
||||
|
||||
// --- service/characteristic discovery ---
|
||||
|
||||
function discoverChars(deviceId, serviceId, group) {
|
||||
@@ -118,10 +129,10 @@ function subscribeToNotifications(deviceId, serviceId) {
|
||||
characteristicId: _chars.status.uuid,
|
||||
state: true,
|
||||
success: function () {
|
||||
wx.onBLECharacteristicValueChange(function (res) {
|
||||
var frame = protocol.parseFrame(res.value)
|
||||
if (frame) handleNotification(frame)
|
||||
})
|
||||
if (wx.offBLECharacteristicValueChange) {
|
||||
wx.offBLECharacteristicValueChange(handleValueChange)
|
||||
}
|
||||
wx.onBLECharacteristicValueChange(handleValueChange)
|
||||
resolve()
|
||||
},
|
||||
fail: function () { resolve() }
|
||||
@@ -129,6 +140,20 @@ function subscribeToNotifications(deviceId, serviceId) {
|
||||
})
|
||||
}
|
||||
|
||||
function requestMtu(deviceId) {
|
||||
return new Promise(function (resolve) {
|
||||
if (!wx.setBLEMTU) {
|
||||
resolve()
|
||||
return
|
||||
}
|
||||
wx.setBLEMTU({
|
||||
deviceId: deviceId,
|
||||
mtu: 64,
|
||||
complete: function () { resolve() }
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function discoverServices(deviceId, callbacks) {
|
||||
wx.getBLEDeviceServices({
|
||||
deviceId: deviceId,
|
||||
@@ -216,7 +241,9 @@ function connect(deviceId, callbacks) {
|
||||
timeout: 10000,
|
||||
success: function () {
|
||||
_connected = true
|
||||
discoverServices(deviceId, callbacks)
|
||||
requestMtu(deviceId).then(function () {
|
||||
discoverServices(deviceId, callbacks)
|
||||
})
|
||||
},
|
||||
fail: function () {
|
||||
_connected = false
|
||||
@@ -251,46 +278,48 @@ function attemptReconnect(deviceId, retriesLeft) {
|
||||
success: function () {
|
||||
_connected = true
|
||||
_chars = {}
|
||||
wx.getBLEDeviceServices({
|
||||
deviceId: deviceId,
|
||||
success: function (res) {
|
||||
var services = res.services
|
||||
var serviceMap = {}
|
||||
for (var i = 0; i < services.length; i++) {
|
||||
var uuid = services[i].uuid.toUpperCase()
|
||||
if (uuid.indexOf(SERVICE.DEVICE_INFO) !== -1) {
|
||||
serviceMap.deviceInfo = services[i].uuid
|
||||
} else if (uuid.indexOf(SERVICE.DATA_COMM) !== -1) {
|
||||
serviceMap.dataComm = services[i].uuid
|
||||
requestMtu(deviceId).then(function () {
|
||||
wx.getBLEDeviceServices({
|
||||
deviceId: deviceId,
|
||||
success: function (res) {
|
||||
var services = res.services
|
||||
var serviceMap = {}
|
||||
for (var i = 0; i < services.length; i++) {
|
||||
var uuid = services[i].uuid.toUpperCase()
|
||||
if (uuid.indexOf(SERVICE.DEVICE_INFO) !== -1) {
|
||||
serviceMap.deviceInfo = services[i].uuid
|
||||
} else if (uuid.indexOf(SERVICE.DATA_COMM) !== -1) {
|
||||
serviceMap.dataComm = services[i].uuid
|
||||
}
|
||||
}
|
||||
|
||||
var tasks = []
|
||||
if (serviceMap.deviceInfo) {
|
||||
tasks.push(discoverChars(deviceId, serviceMap.deviceInfo, 'deviceInfo'))
|
||||
}
|
||||
if (serviceMap.dataComm) {
|
||||
tasks.push(discoverChars(deviceId, serviceMap.dataComm, 'dataComm'))
|
||||
}
|
||||
|
||||
Promise.all(tasks).then(function () {
|
||||
var serviceId = serviceMap.dataComm || null
|
||||
return subscribeToNotifications(deviceId, serviceId)
|
||||
}).then(function () {
|
||||
_reconnecting = false
|
||||
emit('reconnected', { deviceId: deviceId })
|
||||
})
|
||||
},
|
||||
fail: function () {
|
||||
if (retriesLeft > 1) {
|
||||
setTimeout(function () {
|
||||
attemptReconnect(deviceId, retriesLeft - 1)
|
||||
}, 2000)
|
||||
} else {
|
||||
_reconnecting = false
|
||||
emit('reconnect_failed', { deviceId: deviceId })
|
||||
}
|
||||
}
|
||||
|
||||
var tasks = []
|
||||
if (serviceMap.deviceInfo) {
|
||||
tasks.push(discoverChars(deviceId, serviceMap.deviceInfo, 'deviceInfo'))
|
||||
}
|
||||
if (serviceMap.dataComm) {
|
||||
tasks.push(discoverChars(deviceId, serviceMap.dataComm, 'dataComm'))
|
||||
}
|
||||
|
||||
Promise.all(tasks).then(function () {
|
||||
var serviceId = serviceMap.dataComm || null
|
||||
return subscribeToNotifications(deviceId, serviceId)
|
||||
}).then(function () {
|
||||
_reconnecting = false
|
||||
emit('reconnected', { deviceId: deviceId })
|
||||
})
|
||||
},
|
||||
fail: function () {
|
||||
if (retriesLeft > 1) {
|
||||
setTimeout(function () {
|
||||
attemptReconnect(deviceId, retriesLeft - 1)
|
||||
}, 2000)
|
||||
} else {
|
||||
_reconnecting = false
|
||||
emit('reconnect_failed', { deviceId: deviceId })
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
fail: function () {
|
||||
|
||||
在新工单中引用
屏蔽一个用户