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 
#include 
#include 
#include 
#include 


#include 
#include 

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;
    }
}

你可能感兴趣的:(ESP32,蓝牙,低功耗蓝牙,BLE)