רובוטרוניקס מפתחת אלקטורניקה תוכנה ומערכת קליטה ושידור מבוססי BLE
מה זה BLE ?
שכבות של BLE
(הטלפון ) A Central (Master)
Peripheral (Slave) BLE Device which sends the packets
Any BLE Device can be configured as a Server or a Client. ESP32 is no exception to this
קונטרולר :
הוסט :
אפליקציה :
The controller contains the following:
- PHY – The Physical Layer
- LL – The Link Layer
- HCI – Controller side Host-Controller Interface
The host contains the following:
- HCI – Host side Host-Controller Interface
- L2CAP – Logical Link Control and Application Protocol
- SM – Security Manager
- ATT – Attribute Protocol
- GAP – Generic Access Profile
- GATT – Generic Attribute Profile
חיבור בין שתי ESP32 דרך BLE אחד CLINET , SERVER
BLE AS SERVER
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <BLEDevice.h> #include <BLEUtils.h> #include <BLEServer.h> // See the following for generating UUIDs: // https://www.uuidgenerator.net/ #define SERVICE_UUID “4fafc201-1fb5-459e-8fcc-c5c9c331914b“ #define CHARACTERISTIC_UUID “beb5483e-36e1-4688-b7f5-ea07361b26a8“ /* BLEServer *pServer = BLEDevice::createServer(); BLEService *pService = pServer->createService(SERVICE_UUID); BLECharacteristic *pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE ); */ BLEServer *pServer; BLEService *pService; BLECharacteristic *pCharacteristic; void setup() { Serial.begin(115200); Serial.println(“Starting BLE Server!“); BLEDevice::init(“ESP32-BLE-Server“); pServer = BLEDevice::createServer(); pService = pServer->createService(SERVICE_UUID); pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE ); /* BLEServer *pServer = BLEDevice::createServer(); BLEService *pService = pServer->createService(SERVICE_UUID); BLECharacteristic *pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE );*/ pCharacteristic->setValue(“Hello, World!“); pService->start(); //BLEAdvertising *pAdvertising = pServer->getAdvertising(); 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(); //pAdvertising->start(); Serial.println(“Characteristic defined! Now you can read it in the Client!“); } void loop() { std::string value = pCharacteristic->getValue(); Serial.print(“The new characteristic value is: “); Serial.println(value.c_str()); delay(2000); } |
BLE AS CLIENT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
Skip to content Search… All gists Back to GitHub Sign in Sign up Instantly share code, notes, and snippets. @elktros elktros/ESP32-BLE-Client.ino Created 2 years ago 0 0 Code Revisions 1 <script src="https://gist.github.com/elktros/6236b54d0ebe74c9a347b3d5d55993df.js"></script> ESP32 BLE Client ESP32-BLE-Client.ino #include "BLEDevice.h" /* Specify the Service UUID of Server */ static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b"); /* Specify the Characteristic UUID of Server */ static BLEUUID charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8"); static boolean doConnect = false; static boolean connected = false; static boolean doScan = false; static BLERemoteCharacteristic* pRemoteCharacteristic; static BLEAdvertisedDevice* myDevice; static void notifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) { Serial.print("Notify callback for characteristic "); Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str()); Serial.print(" of data length "); Serial.println(length); Serial.print("data: "); Serial.println((char*)pData); } class MyClientCallback : public BLEClientCallbacks { void onConnect(BLEClient* pclient) { } void onDisconnect(BLEClient* pclient) { connected = false; Serial.println("onDisconnect"); } }; /* Start connection to the BLE Server */ bool connectToServer() { Serial.print("Forming a connection to "); Serial.println(myDevice->getAddress().toString().c_str()); BLEClient* pClient = BLEDevice::createClient(); Serial.println(" - Created client"); pClient->setClientCallbacks(new MyClientCallback()); /* Connect to the remote BLE Server */ pClient->connect(myDevice); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private) Serial.println(" - Connected to server"); /* Obtain a reference to the service we are after in the remote BLE server */ BLERemoteService* pRemoteService = pClient->getService(serviceUUID); if (pRemoteService == nullptr) { Serial.print("Failed to find our service UUID: "); Serial.println(serviceUUID.toString().c_str()); pClient->disconnect(); return false; } Serial.println(" - Found our service"); /* Obtain a reference to the characteristic in the service of the remote BLE server */ pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID); if (pRemoteCharacteristic == nullptr) { Serial.print("Failed to find our characteristic UUID: "); Serial.println(charUUID.toString().c_str()); pClient->disconnect(); return false; } Serial.println(" - Found our characteristic"); /* Read the value of the characteristic */ /* Initial value is 'Hello, World!' */ if(pRemoteCharacteristic->canRead()) { std::string value = pRemoteCharacteristic->readValue(); Serial.print("The characteristic value was: "); Serial.println(value.c_str()); } if(pRemoteCharacteristic->canNotify()) { pRemoteCharacteristic->registerForNotify(notifyCallback); } connected = true; return true; } /* Scan for BLE servers and find the first one that advertises the service we are looking for. */ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { /* Called for each advertising BLE server. */ void onResult(BLEAdvertisedDevice advertisedDevice) { Serial.print("BLE Advertised Device found: "); Serial.println(advertisedDevice.toString().c_str()); /* We have found a device, let us now see if it contains the service we are looking for. */ if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) { BLEDevice::getScan()->stop(); myDevice = new BLEAdvertisedDevice(advertisedDevice); doConnect = true; doScan = true; } } }; void setup() { Serial.begin(115200); Serial.println("Starting Arduino BLE Client application..."); BLEDevice::init("ESP32-BLE-Client"); /* Retrieve a Scanner and set the callback we want to use to be informed when we have detected a new device. Specify that we want active scanning and start the scan to run for 5 seconds. */ BLEScan* pBLEScan = BLEDevice::getScan(); pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan->setInterval(1349); pBLEScan->setWindow(449); pBLEScan->setActiveScan(true); pBLEScan->start(5, false); } void loop() { /* If the flag "doConnect" is true, then we have scanned for and found the desired BLE Server with which we wish to connect. Now we connect to it. Once we are connected we set the connected flag to be true. */ if (doConnect == true) { if (connectToServer()) { Serial.println("We are now connected to the BLE Server."); } else { Serial.println("We have failed to connect to the server; there is nothin more we will do."); } doConnect = false; } /* If we are connected to a peer BLE Server, update the characteristic each time we are reached with the current time since boot */ if (connected) { String newValue = "Time since boot: " + String(millis()/2000); Serial.println("Setting new characteristic value to \"" + newValue + "\""); /* Set the characteristic's value to be the array of bytes that is actually a string */ pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length()); /* You can see this value updated in the Server's Characteristic */ } else if(doScan) { BLEDevice::getScan()->start(0); // this is just example to start scan after disconnect, most likely there is better way to do it in arduino } delay(2000); /* Delay a second between loops */ } |
קישור לכתבת מקור : LINK ESP32 BLE