Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <BLEDevice.h>
- #include <BLEUtils.h>
- #include <BLEScan.h>
- #include <BLEAdvertisedDevice.h>
- #include <string>
- #include <BLEServer.h>
- #include <FS.h>
- #include <SD.h>
- #include <SPI.h>
- #include <sstream>
- #define SERVICE_UUID "635f284e-1ef9-11e9-ab14-d663bd873d93"
- #define CHARACTERISTIC_UUID "635f2ace-1ef9-11e9-ab14-d663bd873d93"
- int rssi = -200;
- int toWrite = 1;
- BLEScan* pBLEScan;
- BLECharacteristic *pCharacteristic;
- void setup() {
- Serial.begin(115200);
- SD.begin(5);
- // Assegna un nome ad dispositivo bluetooth
- BLEDevice::init("ESP32-Bluetooth");
- // Settaggi per il tipo di scansione BT da eseguire
- pBLEScan = BLEDevice::getScan();
- pBLEScan->setActiveScan(true);
- pBLEScan->setInterval(500);
- pBLEScan->setWindow(99);
- // Crea il server BT nel quale pubblicare il valore RSSI corrente
- BLEServer *pServer = BLEDevice::createServer();
- BLEService *pService = pServer->createService(SERVICE_UUID);
- pCharacteristic = pService->createCharacteristic(
- CHARACTERISTIC_UUID,
- BLECharacteristic::PROPERTY_READ |
- BLECharacteristic::PROPERTY_WRITE
- );
- pCharacteristic->setValue("rssi is " + rssi);
- //pService->start();
- // Effettua l'advertising del valore impostato nel passaggio precedente
- BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
- pAdvertising->addServiceUUID(SERVICE_UUID);
- pAdvertising->setScanResponse(true);
- pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
- pAdvertising->setMinPreferred(0x12);
- BLEDevice::startAdvertising();
- }
- void appendFile(fs::FS &fs, const char * path, const char * message){
- Serial.printf("Appending to file: %s\n", path);
- File file = fs.open(path, FILE_APPEND);
- if(!file){
- Serial.println("Failed to open file for appending");
- return;
- }
- if(file.print(message)){
- toWrite = 0;
- Serial.println("Message appended");
- } else {
- Serial.println("Append failed");
- }
- file.close();
- }
- void loop() {
- BLEScanResults foundDevices = pBLEScan->start(1, false);
- for (int i = 0; i < foundDevices.getCount(); i++)
- {
- BLEAdvertisedDevice app = foundDevices.getDevice(i);
- String str = app.getAddress().toString().c_str();
- if (str.equals("00:cd:ff:00:34:12") && toWrite == 1) {
- Serial.println(app.getAddress().toString().c_str());
- Serial.println(app.getRSSI());
- pCharacteristic->setValue("rssi is " + rssi);
- char str[4];
- sprintf(str, "%d\n", app.getRSSI());
- appendFile(SD, "/rssi.txt", str);
- SD.end();
- }
- }
- pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement