-
-
Save fanoush/cf64ab204f9f200ee15cf1c1ec60ed1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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