收藏
回答

notifyBLECharacteristicValueChange获取不到回调信息?

notifyBLECharacteristicValueChange成功回调后 调用onBLECharacteristicValueChange,当写入后拿不到回调信息 只有非常偶尔的一两次拿到了 几乎是拿不到

上传了微信日志 微信号tonghao77D 时间1105 17:45左右

// 连接配网设备
			connetWifi(deviceId) {
				console.log('配网deviceId', deviceId)
				var that = this;
				uni.createBLEConnection({
					// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
					deviceId: deviceId, //设备id
					success: function(res2) {
						uni.getBLEDeviceServices({
							deviceId: deviceId,
							success: function(res) {
								console.log('获取连接wifi的service服务', res)
								for (let i = 0; i < res.services.length; i++) {
									if (res.services[i].isPrimary && res.services[i].uuid ==
										'0000FFE0-0000-1000-8000-00805F9B34FB') {
										let serviceId = res.services[i].uuid
										that.wifiServiceId = serviceId
										console.log('service服务Id', serviceId)
										//获取连接设备的所有特征值
										uni.getBLEDeviceCharacteristics({
											deviceId: deviceId,
											serviceId: serviceId,
											success: function(res3) {
												if (res3.errCode == 0) {
													that.notifyID = ''
													that.writeId = ''
													for (var i = 0; i < res3.characteristics.length; i++) {
														if (res3.characteristics[i]['properties']['notify']) {
															that.notifyID = res3.characteristics[i]['uuid']
														}
														// 获取可以写入的设备的特征值uuid
														if (res3.characteristics[i]['properties']['write']) {
															that.writeId = res3.characteristics[i]['uuid']
														}
													}
													// 有可读通道
													if (that.notifyID) {
														that.notifyBLECharacteristicValueChange(deviceId, serviceId,that.notifyID, 2)
													}


												} else {
													console.log('获取特征值失败')
												}
											},
										})
										return
									}


								}
							},
							fail: function(res) {
								console.log('获取蓝牙失败', res)
							}
						})


					}
				})
			},
// notify监听消息
			notifyBLECharacteristicValueChange(deviceId, serviceId, characteristicId, type) {
				var that = this
				console.log(deviceId, serviceId, characteristicId)
				uni.notifyBLECharacteristicValueChange({
					state: true,
					type: 'notification',
					deviceId: deviceId,
					serviceId: serviceId,
					characteristicId: characteristicId,


					success: function(res) {
						console.log('开启监听成功', res)
						//  console.log('启用低功耗蓝牙设备特征值变化时的 notify 功能', that.data.uuId)
						/*用来监听手机蓝牙设备的数据变化*/
						setTimeout(() => {
							that.onBLECharacteristicValueChange()
							if (type === 2) {
								setTimeout(() => {
                                                                     // 发送信息
									that.wifiSend()
								}, 1000)
							}
						}, 1000)




					},
					fail: function(res) {
						console.log('启用低功耗蓝牙设备特征值变化时的 notify 功能失败', res)
					}
				})
				that.onBLECharacteristicValueChange()
			},
回答关注问题邀请回答
收藏

1 个回答

  • 社区技术运营专员-Jahozheng
    社区技术运营专员-Jahozheng
    2022-11-06

    请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    2022-11-06
    有用
    回复
登录 后发表内容