gjchen 发表于 2022-10-11 15:49:11

ESP32蓝牙扫描无法扫描到附近的手机设备

网上找过很多示例代码,也用过ESP32标配的示例代码,使用的是ESP32WROOM芯片板,用过BLE模式,也用过经典蓝牙模式,也用过双模模式,但仍然无法扫描到设备周围的手机的蓝牙地址,示例代码如下,请高手指点,谢谢!


/*
   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> // 蓝牙Ble设备库
#include <BLEUtils.h>
#include <BLEScan.h> // 蓝牙ble设备的扫描功能库 本篇重点
#include <BLEAdvertisedDevice.h> // 扫描到的蓝牙设备(广播状态)

int scanTime = 120; //In seconds扫描时间
BLEScan* pBLEScan; // 扫描对象

// 扫描广播设备结果回调
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());
      //Serial.printf("Advertised Address: %s \n",advertisedDevice.getAddress().toString().c_str());
    }
};

void setup() {
Serial.begin(115200);
Serial.println("Scanning...");

BLEDevice::init("ESP32TEST"); // 蓝牙设备初始化,设置设备名字
pBLEScan = BLEDevice::getScan(); // 创建一个新扫描入口,获取扫描对象

pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());// 注册扫描结果回调
//BLE_ADDR_TYPE_PUBLIC
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster // 配置主动扫描
pBLEScan->setInterval(0x50); // 配置扫描间隔时间,ms为单位
pBLEScan->setWindow(0x30);// 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);
}

   
页: [1]
查看完整版本: ESP32蓝牙扫描无法扫描到附近的手机设备