Library 下載點 https://github.com/nkolban/ESP32_BLE_Arduino
在綠色Code處點選並Download.Zip,然後解壓縮得到 ESP32_BLE_Arduino-master資料夾,將資料夾複製到C:\Program Files (x86)\Arduino\libraries,或者您安裝Arduino的資料夾中\libraries
線上產生 UUID,本範例使用559f49c4-7072-11eb-9439-0242ac130002
網站網址:https://www.uuidgenerator.net/version1
使用Version 1
/*
This example configures LinkIt 7697 to send iBeacon-compatbile advertisement data.
You should be able to search this device with iOS or Android iBeacon tools.
created Mar 2017
*/
#include <LBLE.h>
#include <LBLEPeriphral.h>
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
// Initialize BLE subsystem
Serial.println("BLE begin");
LBLE.begin();
while (!LBLE.ready()) {
delay(100);
}
Serial.println("BLE ready");
// configure our advertisement data as iBeacon.
LBLEAdvertisementData beaconData;
// This is a common AirLocate example UUID.
LBLEUuid uuid("559f49c4-7072-11eb-9439-0242ac130002");
beaconData.configAsIBeacon(uuid, 01, 02, -10);
Serial.print("Start advertising iBeacon with uuid=");
Serial.println(uuid);
// start advertising it
LBLEPeripheral.advertise(beaconData);
}
void loop() {
// The underlying framework will advertise periodically.
// we simply wait here.
//
// You can use iBeacon apps such as
// "Locate Beacon" by Radius Networks on iOS devices
// to locate this beacon.
delay(3000);
}
ESP32程式碼: https://youyouyou.pixnet.net/blog/post/119410657
取自小霸王的網站
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEUUID.h>
#include <BLEAdvertisedDevice.h>
#include <BLEBeacon.h>
BLEScan* pBLEScan;
BLEBeacon id;
int scanTime = 6; //In seconds
String reverse(String str) {
String rev;
for (int i = str.length() - 1; i >= 0; i--) {
rev = rev + str[i];
}
return rev;
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice advertisedDevice)
{
id.setData(advertisedDevice.getManufacturerData());
//Print UUID
Serial.print("UUID :");
String bUUID = id.getProximityUUID().toString().c_str();
bUUID = reverse(bUUID);
Serial.print(bUUID);
//Print RSSI
Serial.print(",RSSI :");
int bRSSI = advertisedDevice.getRSSI();
Serial.print(bRSSI);
//Print Major
Serial.print(",Major :");
int bMajor = id.getMajor() / 256;
Serial.print(bMajor);
//Print Minor
Serial.print(",Minor :");
int bMinor = id.getMinor() / 256;
Serial.print(bMinor);
Serial.println("");
//如果找到的UUID相同,且RSSI大於-50時,亮燈
if ( bUUID == "55f9944c0727-11be-4993-2024-ca310020" && bRSSI >= -110) {
digitalWrite(2, HIGH);
Serial.println("找到設備了,就在這裡");
} else {
digitalWrite(2, LOW);
Serial.println("沒找到或遠了點");
}
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
Serial.println("Scanning...");
pinMode(2, OUTPUT);
}
void loop()
{
BLEScanResults foundDevices = pBLEScan->start(scanTime);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}
當ESP32找到7697發出的位址,所以燈號變亮的
但是問題來了,調整跟ESP32距離,沒什麼變化
所以我只好把數值做改變,讓燈號亮起來
妙的是,我把電源拔掉的時候,ESP32竟然還是停留在找到的數據,沒有把它清除掉
不知道問題在哪裡,要等能發出beacon的設備到再試試看