zoukankan      html  css  js  c++  java
  • arduino ESP32 AndroidStudio BLE低功耗蓝牙 物联网

                      arduino ESP32 AndroidStudio BLE低功耗蓝牙 物联网

    参考路径:  https://blog.csdn.net/qq_35174914/article/details/79328125

    nodered开发:

    1.   node red sample节点前后端数据传送

    2.    node red 代码编辑块传到后台

     

    esp32采用的蓝牙于普通的蓝牙不同,是低功耗蓝牙,手机用一般的蓝牙代码是连不上的。在本文中,不讨论有关低功耗蓝牙的内容,只说明如何实现ESP32与 Android手机间通过低功耗蓝牙进行相互通信。

        进入这个仓库   https://github.com/nkolban/ESP32_BLE_Arduino  打包下载所有文件

    将内容解压后复制Arduino安装目录下的Libraries文件夹下

    注意不要产生多级目录

    然后是Arduino代码

    #include <BLEDevice.h>
    #include <BLEServer.h>
    #include <BLEUtils.h>
    #include <BLE2902.h>
    #include <String.h>

    BLECharacteristic *pCharacteristic;
    bool deviceConnected = false;
    uint8_t txValue = 0;
    long lastMsg = 0;//存放时间的变量
    String rxload="BlackWalnutLabs";

    #define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
    #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
    #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) {
    rxload="";
    for (int i = 0; i < rxValue.length(); i++)
    {
    rxload +=(char)rxValue[i];
    Serial.print(rxValue[i]);
    }
    Serial.println("");
    }
    }
    };

    void setupBLE(String BLEName){
    const char *ble_name=BLEName.c_str();
    BLEDevice::init(ble_name);
    BLEServer *pServer = BLEDevice::createServer();
    pServer->setCallbacks(new MyServerCallbacks());
    BLEService *pService = pServer->createService(SERVICE_UUID);
    pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX,BLECharacteristic::PROPERTY_NOTIFY);
    pCharacteristic->addDescriptor(new BLE2902());
    BLECharacteristic *pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX,BLECharacteristic::PROPERTY_WRITE);
    pCharacteristic->setCallbacks(new MyCallbacks());
    pService->start();
    pServer->getAdvertising()->start();
    Serial.println("Waiting a client connection to notify...");
    }


    void setup() {
    Serial.begin(115200);
    setupBLE("BlackWalnutLabs");//设置蓝牙名称
    }

    void loop() {
    long now = millis();//记录当前时间
    if (now - lastMsg > 1000)
    {//每隔1秒发一次信号
    if (deviceConnected&&rxload.length()>0) {
    String str=rxload;
    const char *newValue=str.c_str();
    pCharacteristic->setValue(newValue);
    pCharacteristic->notify();
    }
    lastMsg = now;//刷新上一次发送数据的时间
    }

    }
    上传到ESP32上以后 进入APP搜索设备,连接设备

    直接使用即可

    附上AndroidStudio的代码 https://pan.baidu.com/s/1ht9OwSW   y51b

    或者用C币的。。。恩 http://download.csdn.net/download/qq_35174914/10252124


    ————————————————
    版权声明:本文为CSDN博主「风度青年」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_35174914/article/details/79328125

  • 相关阅读:
    在插入一条记录后 取得自动增长ID
    hashtable,dictionary 从原理上说说有什么异同,哪个性能高一些
    单例模式
    聚簇索引与非聚簇索引的区别
    基于SQL SERVER2008的SCCM2007部署
    XML架构下的表结构设置主键
    IE6与IE7下一点样式的区别
    Session丢失原因与解决方案小结
    Python_如何去除字符串里的空格
    Python_让人脑阔疼的编码问题(转)+(整理)
  • 原文地址:https://www.cnblogs.com/hbtmwangjin/p/12876878.html
Copyright © 2011-2022 走看看