Skip to content

Instantly share code, notes, and snippets.

@fanoush
Created June 4, 2023 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fanoush/cf64ab204f9f200ee15cf1c1ec60ed1c to your computer and use it in GitHub Desktop.
Save fanoush/cf64ab204f9f200ee15cf1c1ec60ed1c to your computer and use it in GitHub Desktop.
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLE2902.h>
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("Connected");
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("Disonnected");
}
};
void setup() {
// put your setup code here, to run once:
Serial.begin(921600);
BLEDevice::init("C3");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ
| BLECharacteristic::PROPERTY_WRITE
| BLECharacteristic::PROPERTY_NOTIFY
| BLECharacteristic::PROPERTY_INDICATE
);
// Create a BLE Descriptor
pCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
}
void loop() {
// put your main code here, to run repeatedly:
if (deviceConnected) {
pCharacteristic->setValue((uint8_t*)&value, 4);
pCharacteristic->notify();
value++;
}
Serial.printf("Hello loop %d\n",value);
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment