47 lines
1.4 KiB
C++
47 lines
1.4 KiB
C++
#include <Arduino.h>
|
|
#include <BLEDevice.h>
|
|
#include <BLEServer.h>
|
|
|
|
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
|
|
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
|
|
|
|
class BLEAdvertisingRestartCallback: public BLEServerCallbacks {
|
|
void onConnect(BLEServer* server) {
|
|
BLEDevice::getAdvertising()->start();
|
|
}
|
|
};
|
|
|
|
class BluetoothService {
|
|
const char *_serviceName;
|
|
BLECharacteristicCallbacks *_callbacks;
|
|
BLEServer *_server;
|
|
BLEService *_service;
|
|
BLECharacteristic *_characteristic;
|
|
|
|
public:
|
|
BluetoothService(const char* serviceName, BLECharacteristicCallbacks* callbacks) {
|
|
_serviceName = serviceName;
|
|
_callbacks = callbacks;
|
|
}
|
|
|
|
void init() {
|
|
BLEDevice::init(_serviceName);
|
|
_server = BLEDevice::createServer();
|
|
_service = _server->createService(SERVICE_UUID);
|
|
_server->setCallbacks(new BLEAdvertisingRestartCallback());
|
|
|
|
_characteristic = _service->createCharacteristic(CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
|
|
_characteristic->setCallbacks(_callbacks);
|
|
}
|
|
|
|
void start() {
|
|
_service->start();
|
|
|
|
BLEAdvertising *advertising = BLEDevice::getAdvertising();
|
|
advertising->addServiceUUID(SERVICE_UUID);
|
|
advertising->setScanResponse(true);
|
|
advertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
|
|
advertising->setMinPreferred(0x12);
|
|
BLEDevice::startAdvertising();
|
|
}
|
|
};
|