ESP32低功耗蓝牙

/*
    Video: https://www.youtube.com/watch?v=oCMOYS71NIU
    Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
    Ported to Arduino ESP32 by Evandro Copercini
   Create a BLE server that, once we receive a connection, will send periodic notifications.
   创建一个BLE服务器,一旦我们收到连接,将会周期性发送通知
   T使用步骤:
   1. 创建一个 BLE Server
   2. 创建一个 BLE Service
   3. 创建一个 BLE Characteristic
   4. 创建一个 BLE Descriptor
   5. 开始服务
   6. 开始广播
*/
#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>


#include <Wire.h>
#include <U8g2lib.h>

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);  

String str = "";

uint8_t txValue = 0;
BLEServer *pServer = NULL;                   //BLEServer指针 pServer
BLECharacteristic *pTxCharacteristic = NULL; //BLECharacteristic指针 pTxCharacteristic
bool deviceConnected = false;                //本次连接状态
bool oldDeviceConnected = false;             //上次连接状态

// See the following for generating UUIDs: https://www.uuidgenerator.net/
#define SERVICE_UUID "dd1ce41f-5de1-4834-acb1-d1f067ccbf5e" // UART service UUID
#define CHARACTERISTIC_UUID_RX "26c9a3c7-8f1a-4db8-a3f1-bd7bd946436a"
#define CHARACTERISTIC_UUID_TX "d7d10b89-7c76-47b9-a303-bc3d088affb1"

// void ssd1302_show(String *str){
//   u8g2.clearBuffer();					// clear the internal memory
//   u8g2.setFont(u8g2_font_ncenB08_tr);	// choose a suitable font
//   u8g2.drawStr(0,10,str);	// write something to the internal memory
//   u8g2.sendBuffer();					// transfer internal memory to the display
//   //delay(10000);  
// }

class MyServerCallbacks : public BLEServerCallbacks
{
    void onConnect(BLEServer *pServer)
    {
        deviceConnected = true;
        u8g2.clearBuffer();					// clear the internal memory
        u8g2.setFont(u8g2_font_ncenB08_tr);	// choose a suitable font
        u8g2.drawStr(0,10,"ConnectON!");	// write something to the internal memory
        u8g2.sendBuffer();					// transfer internal memory to the display
        //ssd1302_show("ConnectON!");
        //delay(5000);
    };

    void onDisconnect(BLEServer *pServer)
    {
        deviceConnected = false;
        u8g2.clearBuffer();					// clear the internal memory
        u8g2.setFont(u8g2_font_ncenB08_tr);	// choose a suitable font
        u8g2.drawStr(0,10,"ConnectOFF!");	// write something to the internal memory
        u8g2.sendBuffer();					// transfer internal memory to the display
        //ssd1302_show("ConnectOFF!");
        //delay(5000);
    }
};

class MyCallbacks : public BLECharacteristicCallbacks
{
    void onWrite(BLECharacteristic *pCharacteristic)
    {
        std::string rxValue = pCharacteristic->getValue(); //接收信息

        if (rxValue.length() > 0)
        { //向串口输出收到的值
            Serial.print("RX: ");

            str = "";

            for (int i = 0; i < rxValue.length(); i++){
                Serial.print(rxValue[i]);
                str += rxValue[i];
            }
                
            Serial.println();
            Serial.print("STR: ");
            Serial.println(str);
            u8g2.clearBuffer();					// clear the internal memory
            u8g2.setFont(u8g2_font_ncenB08_tr);	// choose a suitable font
            u8g2.drawStr(0,10,str.c_str());	// write something to the internal memory
            u8g2.sendBuffer();					// transfer internal memory to the display
            //ssd1302_show("RX:"+str);
        }
    }
};

void setup()
{
    Serial.begin(115200);

    u8g2.begin();

    // 创建一个 BLE 设备
    BLEDevice::init("UART_BLE");

    // 创建一个 BLE 服务
    pServer = BLEDevice::createServer();
    pServer->setCallbacks(new MyServerCallbacks()); //设置回调
    BLEService *pService = pServer->createService(SERVICE_UUID);

    // 创建一个 BLE 特征
    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(" 等待一个客户端连接,且发送通知... ");
    //ssd1302_show("WaitConnect!");
    u8g2.clearBuffer();					// clear the internal memory
    u8g2.setFont(u8g2_font_ncenB08_tr);	// choose a suitable font
    u8g2.drawStr(0,10,"WaitConnect!");	// write something to the internal memory
    u8g2.sendBuffer();					// transfer internal memory to the display
}

void loop()
{
    // deviceConnected 已连接
    if (deviceConnected)
    {
        pTxCharacteristic->setValue(&txValue, 1); // 设置要发送的值为1
        pTxCharacteristic->notify();              // 广播
        txValue++;                                // 指针地址自加1
        delay(2000);                              // 如果有太多包要发送,蓝牙会堵塞
    }

    // disconnecting  断开连接
    if (!deviceConnected && oldDeviceConnected)
    {
        delay(500);                  // 留时间给蓝牙缓冲
        pServer->startAdvertising(); // 重新广播
        Serial.println(" 开始广播 ");
        //ssd1302_show("GB Again!");
        u8g2.clearBuffer();					// clear the internal memory
        u8g2.setFont(u8g2_font_ncenB08_tr);	// choose a suitable font
        u8g2.drawStr(0,10,"GB Again!");	// write something to the internal memory
        u8g2.sendBuffer();					// transfer internal memory to the display
        oldDeviceConnected = deviceConnected;
    }

    // connecting  正在连接
    if (deviceConnected && !oldDeviceConnected)
    {
        // do stuff here on connecting
        oldDeviceConnected = deviceConnected;
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值