美文网首页
esp32s 蓝牙探针

esp32s 蓝牙探针

作者: 奋斗_登 | 来源:发表于2019-11-05 11:51 被阅读0次

1、蓝牙探针有效距离为10米,决定了近场场景,且采集的mac地址也会出现假的,iphone xr是这样的,可以采集到小米手环,其它机型测试并不灵敏,没发现采集到
2、开发环境
配置“开发板管理器地址”:https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

image.png
选择开发板设置信息:
image.png
代码如下:


#define SCAN_TIME 60     // seconds


#include <Arduino.h>
#include <sstream>

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

std::stringstream ss;


class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
{
    void onResult(BLEAdvertisedDevice advertisedDevice)
    {
      Serial.println("Advertised Device:  \n");
      Serial.print(advertisedDevice.toString().c_str());
      Serial.println("\n");
    }
};
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  Serial.println("ESP32 BLE Scanner");

  // disable brownout detector to maximize battery life
  Serial.println("disable brownout detector");
  // WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);

  Serial.println("BLEDevice::init()");
  BLEDevice::init("");

  // put your main code here, to run repeatedly:
  BLEScan *pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  pBLEScan->setInterval(0x50);
  pBLEScan->setWindow(0x30);

  Serial.println("Start BLE scan for 30 seconds...\n");

  BLEScanResults foundDevices = pBLEScan->start(SCAN_TIME);
  int count = foundDevices.getCount();
  ss << "[";
  for (int i = 0; i < count; i++)
  {
    if (i > 0)
    {
      ss << ",";
    }
    BLEAdvertisedDevice d = foundDevices.getDevice(i);
    ss << "{\"Address\":\"" << d.getAddress().toString() << "\",\"Rssi\":" << d.getRSSI();

    if (d.haveName())
    {
      ss << ",\"Name\":\"" << d.getName() << "\"";
    }

    if (d.haveAppearance())
    {
      ss << ",\"Appearance\":" << d.getAppearance();
    }

    if (d.haveManufacturerData())
    {
      std::string md = d.getManufacturerData();
      uint8_t *mdp = (uint8_t *)d.getManufacturerData().data();
      char *pHex = BLEUtils::buildHexData(nullptr, mdp, md.length());
      ss << ",\"ManufacturerData\":\"" << pHex << "\"";
      free(pHex);
    }

    if (d.haveServiceUUID())
    {
      ss << ",\"ServiceUUID\":\"" << d.getServiceUUID().toString() << "\"";
    }

    if (d.haveTXPower())
    {
      ss << ",\"TxPower\":" << (int)d.getTXPower();
    }

    ss << "}";
  }
  ss << "]";
  //Serial.println(String(ss));
  Serial.println("Scan done!");

}

void loop() {
  // put your main code here, to run repeatedly:
  //Serial.println("loop");
}

运行效果:


image.png

相关文章

网友评论

      本文标题:esp32s 蓝牙探针

      本文链接:https://www.haomeiwen.com/subject/foysbctx.html