admin管理员组

文章数量:1794759

APP Invertor 制作BLE蓝牙APP   控制esp32蓝牙小车

APP Invertor 制作BLE蓝牙APP 控制esp32蓝牙小车

APP Invertor蓝牙小车制作
  • 1、插件下载
    • 1.1、导入插件
  • 2、APP界面展示
    • 2.1 、可视化编程
  • 3、esp32 蓝牙代码
  • 4、实测效果
    • 4.1 APP控制端
    • 4.2 蓝牙接收端
  经过几天的学习,终于成功制作了一个属于自己的蓝牙APP,APP可以控制蓝牙小车。   使用 App Inventor 2 WxBit 汉化增强版 编写APP,建议使用这个,不同的APP Invertor 版本里面会有所差异。我的目的是为esp32 连接蓝牙,所以使用传统的蓝牙客户端是无法连接成功的,如果使用传统的蓝牙客户端会在连接时报 507错误。因为传统的蓝牙模块不是BLE (低功耗模块),在蓝牙4.0之前的都是传统蓝牙模块。因此我们需要下载 BLE插件。需要APP工程文件和代码的留言。

1、插件下载

BLE插件下载:BluetoothLE 截至目前插件最新是version 20200828

1.1、导入插件

2、APP界面展示

界面由文本框、按键、标签结合相应的布局构成。

2.1 、可视化编程

初始化,连接蓝牙: 需要说明一下,收发服务的UUID 是不同的2个值,定义在esp32 代码里面。 接收数据以及按键: 里面的特征UUID在发送端和接收端是不同的,不要混淆使用。 代码里面UUID: 定义于ESP32代码里面。

3、esp32 蓝牙代码

  这个代码里面仅仅有蓝牙接收和发送数据部分,没有实例小车运行代码。小车的控制信号都已经收到了,实例小车的运行只需要根据收到的信号,做出不同的反应就可以。

// 包含所必需的库 #include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h> BLEServer *pServer = NULL; BLECharacteristic *pTxCharacteristic; bool deviceConnected = false; bool oldDeviceConnected = false; char BLEbuf[32] = {0}; String data = ""; // 定义收发服务的UUID(唯一标识) #define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // RX串口标识 #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" // TX串口标识 #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; }; void onDisconnect(BLEServer* pServer) { deviceConnected = false; } }; class MyCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string rxValue = pCharacteristic->getValue(); if (rxValue.length() > 0) { Serial.println("*********"); //接收数据 Serial.print("Received Value: "); for (int i = 0; i < rxValue.length(); i++){ Serial.print(rxValue[i]); } Serial.println(); data =rxValue.c_str(); //Serial.println(data); Serial.println("*********"); Serial.println(); } } }; // setup()在复位或上电后运行一次: void setup() { Serial.begin(115200); Serial.println("1- Download and install an BLE scanner app in your phone"); Serial.println("2- Scan for BLE devices in the app"); Serial.println("3- Connect to MyESP32"); Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something"); Serial.println("5- See the magic =)"); // 初始化蓝牙设备 BLEDevice::init("MyESP32"); // 为蓝牙设备创建服务器 pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); // 基于SERVICE_UUID来创建一个服务 BLEService *pService = pServer->createService(SERVICE_UUID); pTxCharacteristic = 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()); // 开启服务 pService->start(); // 开启通知 pServer->getAdvertising()->start(); Serial.println("Waiting a client connection to notify..."); Serial.println(); } // loop()一直循环执行: void loop() { if (deviceConnected==1&data.length()>0) { memset(BLEbuf, 0, 32); memcpy(BLEbuf, data.c_str(), 32);//数据赋值 Serial.println(BLEbuf); pTxCharacteristic->setValue(BLEbuf); //收到数据后返回数据 pTxCharacteristic->notify(); data = ""; //返回数据后进行清空,否则一直发送data } // 没有新连接时 if (!deviceConnected && oldDeviceConnected) { // 给蓝牙堆栈准备数据的时间 delay(500); pServer->startAdvertising(); // 重新开始广播 Serial.println("start advertising"); oldDeviceConnected = deviceConnected; } // 正在连接时 if (deviceConnected && !oldDeviceConnected) { // 正在连接时进行的操作 oldDeviceConnected = deviceConnected; } } 4、实测效果 4.1 APP控制端

APP控制端在连接蓝牙之前需要手机自带的蓝牙提前配对。

4.2 蓝牙接收端

自此一个简单的蓝牙APP控制小车的基本代码和框架就出来了。

本文标签: 蓝牙小车InvertorappBLE