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

前言

学习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.cpp
   Ported 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 scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  pBLEScan->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 memory
  delay(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.cpp
   Ported 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 scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  pBLEScan->setInterval(100);
  pBLEScan->setWindow(99);  // less or equal setInterval value
  Serial.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");
}

测试结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值