当前位置: 代码迷 >> 综合 >> 【掌控板-arduino】6.1 esp32蓝牙学习步骤
  详细解决方案

【掌控板-arduino】6.1 esp32蓝牙学习步骤

热度:68   发布时间:2023-12-05 20:34:34.0

前言

学习esp32蓝牙功能,用于传输数据

蓝牙基础知识:

蓝牙的划分:经典蓝牙、蓝牙低功耗BLE
对单个蓝牙模块:蓝牙的框架(蓝牙核心协议‘、蓝牙应用层协议这两个协议的组成)
对多个蓝牙模块:模式(client、server)

参考文章:
esp32的蓝牙学习

ESP32 蓝牙开发(重点查看)

博客园–夜行过客

官方api

蓝牙相关库安装

安装arduino蓝牙相关库,检索关键字“ESP32_BLE_Arduino”
在这里插入图片描述
然后使用source_insight加载对应的代码查看。
C:\Users\XXXX\Documents\Arduino\libraries\ESP32_BLE_Arduino
学习示例

蓝牙scan

创建蓝牙并检索当前周边的蓝牙设备
参见示例:C:\Users\XXX\Documents\Arduino\libraries\ESP32_BLE_Arduino\examples\BLE_scan\BLE_scan.ino

/*func1 scan example *//*Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cppPorted to Arduino ESP32 by Evandro Copercini */#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>int scanTime = 5; //In seconds
BLEScan* pBLEScan;class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
    Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());}
};void setup() {
    Serial.begin(115200);Serial.println("Scanning...");BLEDevice::init("");pBLEScan = BLEDevice::getScan(); //create new scanpBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());pBLEScan->setActiveScan(true); //active scan uses more power, but get results fasterpBLEScan->setInterval(100);pBLEScan->setWindow(99);  // less or equal setInterval value
}void loop() {
    // put your main code here, to run repeatedly:BLEScanResults foundDevices = pBLEScan->start(scanTime, false);Serial.print("Devices found: ");Serial.println(foundDevices.getCount());Serial.println("Scan done!");pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memorydelay(2000);
}

运行结果:
在这里插入图片描述

增加按键运行scan

/*func2 scan example + keyB to start scan *//*Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cppPorted to Arduino ESP32 by Evandro Copercini */#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>const int buttonPin = 2;     // the number of the pushbutton pin
bool keyB_status = false;
int buttonState = 0;  
Adafruit_NeoPixel pixels(3,17, NEO_GRB + NEO_KHZ800);int scanTime = 5; //In seconds
BLEScan* pBLEScan;class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
    Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());}
};void led_on(){
    pixels.setPixelColor(0, pixels.Color(10, 0, 0));pixels.show();Serial.println("led_on");
}void led_off(){
    pixels.setPixelColor(0, pixels.Color(0, 0, 0));pixels.show();Serial.println("led_off");
}void start_scan(){
    //开始检索附件的蓝牙设备BLEScanResults foundDevices = pBLEScan->start(scanTime, false);Serial.print("Devices found: ");//找到蓝牙设备数目Serial.println(foundDevices.getCount());Serial.println("Scan done!");//清空存储的蓝牙设备信息pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
}void setup() {
    Serial.begin(115200);Serial.println("Scanning...");pinMode(buttonPin, INPUT);BLEDevice::init("esp32-zhangkongban");pBLEScan = BLEDevice::getScan(); //create new scanpBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());pBLEScan->setActiveScan(true); //active scan uses more power, but get results fasterpBLEScan->setInterval(100);pBLEScan->setWindow(99);  // less or equal setInterval valueSerial.println("end setup");
}void loop() {
    Serial.println("in loop");// put your main code here, to run repeatedly:buttonState = digitalRead(buttonPin);Serial.println(buttonState);if (buttonState == LOW) {
    delay(500);if(keyB_status == false){
    led_on();start_scan();keyB_status = true;}else{
    led_off();keyB_status = false;}}delay(10);Serial.println("out loop");
}

测试结果:

在这里插入图片描述