austin944

BLE Notification

Dec 24th, 2025
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include <ArduinoBLE.h>
  2.  
  3. // Define a unique UUID for the Service and Characteristic
  4. // You can generate your own at https://www.uuidgenerator.net/
  5. const char* deviceServiceUuid = "19B10000-E8F2-537E-4F6C-D104768A1214";
  6. const char* deviceCharacteristicUuid = "19B10001-E8F2-537E-4F6C-D104768A1214";
  7.  
  8. constexpr size_t ARRAY_SIZE_BYTES = 238;
  9. constexpr uint32_t COUNTER_INTERVAL = 0x100;
  10. constexpr uint32_t COUNTER_INTERVAL_MASK = (COUNTER_INTERVAL-1);
  11. constexpr uint32_t BITS_PER_BYTE = 8;
  12. constexpr uint32_t MILLISECONDS_PER_SECOND = 1000;
  13. constexpr uint32_t BYTES_SENT_PER_INTERVAL = (ARRAY_SIZE_BYTES*COUNTER_INTERVAL);
  14.  
  15. // Create the BLE Service
  16. BLEService sensorService(deviceServiceUuid);
  17.  
  18. BLECharacteristic largeDataChar(deviceCharacteristicUuid, BLERead | BLENotify, 244);
  19.  
  20. int lastValue = 0;
  21. long previousMillis = 0;
  22.  
  23. void setup() {
  24.  
  25. Serial.begin(9600);
  26. while (!Serial);
  27.  
  28. // Initialize BLE
  29. if (!BLE.begin()) {
  30. Serial.println("Starting BLE failed!");
  31. while (1);
  32. }
  33.  
  34. // not supported
  35. // if (BLE.setMTU(242)) {
  36. // Serial.print("MTU set to: ");
  37. // Serial.println(BLE.mtu());
  38. // } else {
  39. // Serial.println("Failed to set MTU");
  40. // }
  41.  
  42. // Set the local name that will appear on your phone
  43. BLE.setLocalName("UnoR4_Sensor");
  44. BLE.setAdvertisedService(sensorService);
  45.  
  46. // Add the characteristic to the service
  47. sensorService.addCharacteristic(largeDataChar);
  48.  
  49. // Add the service
  50. BLE.addService(sensorService);
  51.  
  52. // Start advertising
  53. BLE.advertise();
  54. Serial.println("BLE Peripheral active, waiting for connections...");
  55. }
  56.  
  57. void loop() {
  58. int count = 0;
  59. long currentMillis = millis();
  60. long previousMillis;
  61. uint8_t largePacket[ARRAY_SIZE_BYTES];
  62.  
  63. // Listen for BLE centrals to connect
  64. BLEDevice central = BLE.central();
  65.  
  66. if (central) {
  67. Serial.print("Connected to central: ");
  68. Serial.println(central.address());
  69.  
  70. while (central.connected()) {
  71. // This line triggers the notification to the connected device
  72. //sensorLevelChar.writeValue(newValue);
  73. largeDataChar.writeValue(largePacket, ARRAY_SIZE_BYTES);
  74. if ((count++ & COUNTER_INTERVAL_MASK) == 0) {
  75. previousMillis = currentMillis;
  76. currentMillis = millis();
  77. Serial.println((BYTES_SENT_PER_INTERVAL*MILLISECONDS_PER_SECOND)/(float)(currentMillis - previousMillis));
  78. }
  79. }
  80.  
  81. Serial.print("Disconnected from central: ");
  82. Serial.println(central.address());
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment