1 前言
6.1中我们创建了BLE SCAN去检索蓝牙设备,但是未发现手机等蓝牙设备。
这是因为BLE无法检索到经典蓝牙。
2 蓝牙划分
经典蓝牙和低功耗蓝牙
Android蓝牙开发—经典蓝牙和BLE(低功耗)蓝牙的区别
经典蓝牙:BR/EDR/AMP
低功耗蓝牙:BLE
单模(smart):只支持BLE
双模(smart ready):支持经典蓝牙和BLE
蓝牙4.0才开始支持BLE,蓝牙1.0、2.0、3.0都属于经典蓝牙。
PS:蓝牙4.0是一种标准,该标准包括经典蓝牙模块和低功耗蓝牙模块。
经典蓝牙主要用于无线通信例如音频传输,而低功耗蓝牙一般用于穿戴设备、物联网设备等。
3 无法监听到手机蓝牙原因
需要注意的是设备之间的通讯需要看他们支持的蓝牙标准。
仅低功耗蓝牙设备无法和仅支持经典蓝牙设备通信。
所以我们看到我的esp32无法监听到手机设备,不过其中有一个smart watch,看起来该设备应该是双模。
推测应该是设置中的蓝牙只用到了经典蓝牙。
4 查看设备蓝牙地址
电脑
打开cmd窗口,输入“ipconfig /all”
显示如下

其中显示物理地址就是蓝牙地址,是6个2位16进制串。
安卓/手机
直接在设置中搜索蓝牙地址
设置-系统-关于平板-状态消息-蓝牙地址
ipad
设置-通用-关于本机-蓝牙
5 显示监听到的蓝牙设备名字
5.1 代码
/*
func3 scan example + keyB to start scan + display ble device which can be connected
*/
/*
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>
//add display
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include "SH1106Wire.h" // legacy: #include "SSD1306.h"
SH1106Wire display(0x3c, 23, 22);
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;
String str1 = "ble_device:";
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
//Serial.printf("Advertised Device name: %s \n",advertisedDevice.getName().c_str());
str1.concat(advertisedDevice.getName().c_str());
}
};
void led_on(){
pixels.setPixelColor(0, pixels.Color(10, 0, 0));
pixels.show();
display.clear();
display.drawString(0, 0, "light on");
display.display();
Serial.println("led_on");
}
void led_off(){
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
pixels.show();
display.clear();
display.drawString(0, 0, "light off");
display.display();
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...");
//init display
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
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;
//display.clear();
display.drawString(0, 16, str1);
display.display();
delay(500);
}
else
{
led_off();
keyB_status = false;
}
}
delay(1000);
Serial.println("out loop");
}
5.2 结果


本文档介绍了如何使用ESP32通过BLE进行蓝牙设备扫描,并展示了如何在Arduino环境下显示扫描到的蓝牙设备名称。内容涵盖了经典蓝牙与低功耗蓝牙的区别,解释了无法监听到某些设备(如手机)的原因,并提供了代码示例来监听和显示蓝牙设备名字。
648

被折叠的 条评论
为什么被折叠?



