【Esp32连接微信小程序蓝牙】附Arduino源码《 返回10007 相同特征id冲突问题》

server/2024/10/18 8:23:36/

前言

最近接了一个外包,发现了esp32连接小程序会有很多bug,所以接下来会慢慢更新解决方案,还是需要多接触项目才能进步呀兄弟们!

附上uuid的生成链接:

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

问题

这里借用一下,别人博客遇到的问题。

后面重新开看流程时发现使用 wx.getBLEDeviceCharacteristics的时候有出现了三个特征值,以至于报错,博主问题原链接

我们可以看到小程序>微信小程序维护的也....现在2024年了这个bug还没修好.....

链接

解决办法

问题发现

解决办法只能从esp32代码来入手,首先来看看原本的蓝牙连接代码,我们可以看到,首先在开头就写了四个uuid特征码来进行蓝牙初始化,创建、发送、接收,这就是导致问题出现的关键


#define SERVICE_UUID        "1596c77c-cf40-4137-9957-d24916f8e50b"   //你可以通过上面的网址去生成UUID
#define CHARACTERISTIC_UUID "1596c77c-cf40-4137-9957-d24916f8e50b"
#define CHARACTERISTIC_UUID_RX "1596c77c-cf40-4137-9957-d24916f8e50b"
#define CHARACTERISTIC_UUID_TX "1596c77c-cf40-4137-9957-d24916f8e50b"void setup() {chipId = String((uint32_t)ESP.getEfuseMac(), HEX);chipId.toUpperCase();
//  chipid =ESP.getEfuseMac();
//  Serial.printf("Chip id: %s\n", chipid.c_str());Serial.println("chipId:"+chipId);Serial.println();Serial.printf("Chip id: %s\n", chipId.c_str());// Create the BLE DeviceBLEDevice::init("xhn_Service");// Create the BLE ServerpServer = BLEDevice::createServer();pServer->setCallbacks(new MyServerCallbacks());//随机生成的uuid放入BLEService *pService = pServer->createService(SERVICE_UUID);// Create a BLE CharacteristicpTxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX,  BLECharacteristic::PROPERTY_NOTIFY);pTxCharacteristic->addDescriptor(new BLE2902());BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX,                                  uuid_RX,                                                   BLECharacteristic::PROPERTY_WRITE);pRxCharacteristic->setCallbacks(new MyCallbacks());// Start the servicepService->start();// Start advertisingpServer->getAdvertising()->start();Serial.println("Waiting a client connection to notify...");
}String readString;void loop() {if (deviceConnected) {//        pTxCharacteristic->setValue(&txValue, 1);//        pTxCharacteristic->notify();//        txValue++;//    delay(10); // bluetooth stack will go into congestion, if too many packets are sent}while (Serial.available() > 0) {if (deviceConnected) {delay(3);readString += Serial.read();pTxCharacteristic->setValue(chipId.c_str());
//      pTxCharacteristic->setValue((uint32_t)ESP.getEfuseMac());pTxCharacteristic->notify();Serial.println(chipId);}}// disconnectingif (!deviceConnected && oldDeviceConnected) {delay(500); // give the bluetooth stack the chance to get things readypServer->startAdvertising(); // restart advertisingSerial.println("start advertising");oldDeviceConnected = deviceConnected;}// connectingif (deviceConnected && !oldDeviceConnected) {// do stuff here on connectingoldDeviceConnected = deviceConnected;}
}

问题解决

因为我们初始化,接收、发送时,传递的都是同一个uuid,所以导致特征码重复,而报错,所以我们就可以在初始化的时候使用一个uuid,在发送或接收使用uuid时,切换另一个,因为获取uuid的目的是为了让小程序绑定设备码,所以在初始化的时候我们就可以绑定成功,从而uuid的作用就不重要了。

这边以修改接收的uuid为例:(其实修改一行就解决问题了),或者你将发送的UUID的修改成别的uuid也可以,只要你在小程序绑定号设备号就行,因为设备号是不会改变的。


#define SERVICE_UUID        "1596c77c-cf40-4137-9957-d24916f8e50b"   //你可以通过上面的网址去生成UUID
#define CHARACTERISTIC_UUID "1596c77c-cf40-4137-9957-d24916f8e50b"
#define CHARACTERISTIC_UUID_RX "2abe697b-cad9-409b-802e-624646c3e69c"
#define CHARACTERISTIC_UUID_TX "1596c77c-cf40-4137-9957-d24916f8e50b"void setup() {chipId = String((uint32_t)ESP.getEfuseMac(), HEX);chipId.toUpperCase();
//  chipid =ESP.getEfuseMac();
//  Serial.printf("Chip id: %s\n", chipid.c_str());Serial.println("chipId:"+chipId);Serial.println();Serial.printf("Chip id: %s\n", chipId.c_str());// Create the BLE DeviceBLEDevice::init("xhn_Service");// Create the BLE ServerpServer = BLEDevice::createServer();pServer->setCallbacks(new MyServerCallbacks());//随机生成的uuid放入BLEService *pService = pServer->createService(SERVICE_UUID);// Create a BLE CharacteristicpTxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX,  BLECharacteristic::PROPERTY_NOTIFY);pTxCharacteristic->addDescriptor(new BLE2902());BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX,                                                                                   BLECharacteristic::PROPERTY_WRITE);pRxCharacteristic->setCallbacks(new MyCallbacks());// Start the servicepService->start();// Start advertisingpServer->getAdvertising()->start();Serial.println("Waiting a client connection to notify...");
}String readString;void loop() {if (deviceConnected) {//        pTxCharacteristic->setValue(&txValue, 1);//        pTxCharacteristic->notify();//        txValue++;//    delay(10); // bluetooth stack will go into congestion, if too many packets are sent}while (Serial.available() > 0) {if (deviceConnected) {delay(3);readString += Serial.read();pTxCharacteristic->setValue(chipId.c_str());
//      pTxCharacteristic->setValue((uint32_t)ESP.getEfuseMac());pTxCharacteristic->notify();Serial.println(chipId);}}// disconnectingif (!deviceConnected && oldDeviceConnected) {delay(500); // give the bluetooth stack the chance to get things readypServer->startAdvertising(); // restart advertisingSerial.println("start advertising");oldDeviceConnected = deviceConnected;}// connectingif (deviceConnected && !oldDeviceConnected) {// do stuff here on connectingoldDeviceConnected = deviceConnected;}
}


http://www.ppmy.cn/server/49508.html

相关文章

在Visual Studio Code中使用pytest进行AWS Lambda函数测试的最佳实践

背景/引言 在现代软件开发中,自动化测试已经成为保证代码质量的重要一环。对于AWS Lambda函数开发者来说,使用pytest进行单元测试和集成测试是一个高效且可靠的方法。本文将介绍在Visual Studio Code中使用pytest测试AWS Lambda函数的最佳实践&#xff…

R可视化:ggpubr包学习

欢迎大家关注全网生信学习者系列: WX公zhong号:生信学习者 Xiao hong书:生信学习者 知hu:生信学习者 CDSN:生信学习者2 介绍 ggpubr是我经常会用到的R包,它傻瓜式的画图方式对很多初次接触R绘图的人来…

CentOS7的#!bash #!/bin/bash #!/bin/env bash #!/usr/bin/bash #!/usr/bin/env bash

bash脚本开头可写成 #!/bin/bash , #!/bin/env bash , #!/usr/bin/bash , #!/usr/bin/env bash #!/bin/bash , #!/usr/bin/bash#!/bin/env bash , #!/usr/bin/env bash CentOS7的 /bin 是 /usr/bin 的软链接, /sbin 是 /usr/sbin 的软链接, [root3050 ~]# ll /bin lrwxrwxrw…

从零开始学数据结构系列之第三章《中序线索二叉树线索化及总代码》

文章目录 中序线索化总代码往期回顾 中序线索化 void inThreadTree(TreeNode* T, TreeNode** pre) {if(T){inThreadTree(T->lchild,pre);if(T->lchild NULL){T->ltag 1;T->lchild *pre;}if(*pre ! NULL && (*pre)->rchild NULL){(*pre)->rtag …

分数限制下,选好专业还是选好学校?

选择学校还是选择专业,这是一个很多高考生和家长在填报志愿时都会面临的难题。这个问题没有标准答案,因为每个人的情况和优先考虑的因素都不同。以下是一些需要考虑的方面以及我个人的见解,希望能为高考生和家长们提供一些参考。 考虑专业优…

数据网格和视图入门

WinForms数据网格(GridControl类)是一个数据感知控件,可以以各种格式(视图)显示数据。本主题包含以下部分,这些部分将指导您如何使用网格控件及其视图和列(字段)。 Grid Control’s…

考研计算机网络(第一章 概述)

1.计算机网络的定义 计算机网路是一个将分散的,具有独立功能的计算机系统,通过通信设备和线路连接起来,由功能完善的软件实现资源共享和信息传递的系统。 2.计算机网络的分类 按交换技术分类:电路交换网络,报文交换…

小程序简单版音乐播放器

小程序简单版音乐播放器 结构 先来看看页面结构 <!-- wxml --><!-- 标签页标题 --> <view class"tab"><view class"tab-item {{tab0?active:}}" bindtap"changeItem" data-item"0">音乐推荐</view><…