added connected/disconnected events
This commit is contained in:
parent
ebc5cce602
commit
c6b246f9dd
|
|
@ -7,6 +7,9 @@
|
||||||
#include "utility/BleMidi_Settings.h"
|
#include "utility/BleMidi_Settings.h"
|
||||||
#include "utility/BleMidi_Defs.h"
|
#include "utility/BleMidi_Defs.h"
|
||||||
|
|
||||||
|
#define SERVICE_UUID "03b80e5a-ede8-4b33-a751-6ce34ec4c700"
|
||||||
|
#define CHARACTERISTIC_UUID "7772e5db-3868-4112-a1a9-f2669d106bf3"
|
||||||
|
|
||||||
#if defined(ESP32)
|
#if defined(ESP32)
|
||||||
#include "Ble_esp32.h"
|
#include "Ble_esp32.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
135
src/Ble_esp32.h
135
src/Ble_esp32.h
|
|
@ -1,15 +1,44 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
// Headers for ESP32 BLE
|
||||||
#include <BLEDevice.h>
|
#include <BLEDevice.h>
|
||||||
#include <BLEUtils.h>
|
#include <BLEUtils.h>
|
||||||
#include <BLEServer.h>
|
#include <BLEServer.h>
|
||||||
|
|
||||||
#define SERVICE_UUID "03b80e5a-ede8-4b33-a751-6ce34ec4c700"
|
|
||||||
#define CHARACTERISTIC_UUID "7772e5db-3868-4112-a1a9-f2669d106bf3"
|
|
||||||
|
|
||||||
BEGIN_BLEMIDI_NAMESPACE
|
BEGIN_BLEMIDI_NAMESPACE
|
||||||
|
|
||||||
class BleMidiInterface;
|
class BleMidiInterface
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
// ESP32
|
||||||
|
BLEServer *pServer;
|
||||||
|
BLEAdvertising *pAdvertising;
|
||||||
|
BLECharacteristic *pCharacteristic;
|
||||||
|
|
||||||
|
public:
|
||||||
|
void(*mConnectedCallback)();
|
||||||
|
void(*mDisconnectedCallback)();
|
||||||
|
|
||||||
|
public:
|
||||||
|
inline BleMidiInterface()
|
||||||
|
{
|
||||||
|
mConnectedCallback = NULL;
|
||||||
|
mDisconnectedCallback = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline ~BleMidiInterface()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool begin(const char* deviceName);
|
||||||
|
|
||||||
|
inline void onConnected(void(*fptr)()) {
|
||||||
|
mConnectedCallback = fptr;
|
||||||
|
}
|
||||||
|
inline void onDisconnected(void(*fptr)()) {
|
||||||
|
mDisconnectedCallback = fptr;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class MyServerCallbacks: public BLEServerCallbacks {
|
class MyServerCallbacks: public BLEServerCallbacks {
|
||||||
public:
|
public:
|
||||||
|
|
@ -20,11 +49,11 @@ protected:
|
||||||
BleMidiInterface* _bleMidiInterface;
|
BleMidiInterface* _bleMidiInterface;
|
||||||
|
|
||||||
void onConnect(BLEServer* pServer) {
|
void onConnect(BLEServer* pServer) {
|
||||||
// _bleMidiInterface->mConnectedCallback();
|
_bleMidiInterface->mConnectedCallback();
|
||||||
};
|
};
|
||||||
|
|
||||||
void onDisconnect(BLEServer* pServer) {
|
void onDisconnect(BLEServer* pServer) {
|
||||||
// _bleMidiInterface->mDisconnectedCallback();
|
_bleMidiInterface->mDisconnectedCallback();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -33,69 +62,39 @@ class MyCallbacks: public BLECharacteristicCallbacks {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class BleMidiInterface
|
bool BleMidiInterface::begin(const char* deviceName)
|
||||||
{
|
{
|
||||||
protected:
|
BLEDevice::init(deviceName);
|
||||||
// ESP32
|
|
||||||
BLEServer *pServer;
|
|
||||||
BLEAdvertising *pAdvertising;
|
|
||||||
BLECharacteristic *pCharacteristic;
|
|
||||||
|
|
||||||
void(*mConnectedCallback)();
|
|
||||||
void(*mDisconnectedCallback)();
|
|
||||||
|
|
||||||
public:
|
|
||||||
inline BleMidiInterface()
|
|
||||||
{
|
|
||||||
mConnectedCallback = NULL;
|
|
||||||
mDisconnectedCallback = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline ~BleMidiInterface()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool begin(const char* deviceName)
|
|
||||||
{
|
|
||||||
BLEDevice::init(deviceName);
|
|
||||||
|
|
||||||
pServer = BLEDevice::createServer();
|
|
||||||
pServer->setCallbacks(new MyServerCallbacks(this));
|
|
||||||
|
|
||||||
// Create the BLE Service
|
|
||||||
BLEService* pService = pServer->createService(BLEUUID(SERVICE_UUID));
|
|
||||||
|
|
||||||
// Create a BLE Characteristic
|
|
||||||
pCharacteristic = pService->createCharacteristic(
|
|
||||||
BLEUUID(CHARACTERISTIC_UUID),
|
|
||||||
BLECharacteristic::PROPERTY_READ |
|
|
||||||
BLECharacteristic::PROPERTY_WRITE |
|
|
||||||
BLECharacteristic::PROPERTY_NOTIFY |
|
|
||||||
BLECharacteristic::PROPERTY_WRITE_NR
|
|
||||||
);
|
|
||||||
pCharacteristic->setCallbacks(new MyCallbacks());
|
|
||||||
// Start the service
|
|
||||||
pService->start();
|
|
||||||
|
|
||||||
BLEAdvertisementData advertisementData = BLEAdvertisementData();
|
|
||||||
advertisementData.setFlags(0x04);
|
|
||||||
advertisementData.setCompleteServices(BLEUUID(SERVICE_UUID));
|
|
||||||
advertisementData.setName(deviceName);
|
|
||||||
|
|
||||||
// Start advertising
|
|
||||||
pAdvertising = pServer->getAdvertising();
|
|
||||||
pAdvertising->setAdvertisementData(advertisementData);
|
|
||||||
pAdvertising->start();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void onConnected(void(*fptr)()) {
|
pServer = BLEDevice::createServer();
|
||||||
mConnectedCallback = fptr;
|
pServer->setCallbacks(new MyServerCallbacks(this));
|
||||||
}
|
|
||||||
inline void onDisconnected(void(*fptr)()) {
|
// Create the BLE Service
|
||||||
mDisconnectedCallback = fptr;
|
BLEService* pService = pServer->createService(BLEUUID(SERVICE_UUID));
|
||||||
}
|
|
||||||
};
|
// Create a BLE Characteristic
|
||||||
|
pCharacteristic = pService->createCharacteristic(
|
||||||
|
BLEUUID(CHARACTERISTIC_UUID),
|
||||||
|
BLECharacteristic::PROPERTY_READ |
|
||||||
|
BLECharacteristic::PROPERTY_WRITE |
|
||||||
|
BLECharacteristic::PROPERTY_NOTIFY |
|
||||||
|
BLECharacteristic::PROPERTY_WRITE_NR
|
||||||
|
);
|
||||||
|
pCharacteristic->setCallbacks(new MyCallbacks());
|
||||||
|
// Start the service
|
||||||
|
pService->start();
|
||||||
|
|
||||||
|
BLEAdvertisementData advertisementData = BLEAdvertisementData();
|
||||||
|
advertisementData.setFlags(0x04);
|
||||||
|
advertisementData.setCompleteServices(BLEUUID(SERVICE_UUID));
|
||||||
|
advertisementData.setName(deviceName);
|
||||||
|
|
||||||
|
// Start advertising
|
||||||
|
pAdvertising = pServer->getAdvertising();
|
||||||
|
pAdvertising->setAdvertisementData(advertisementData);
|
||||||
|
pAdvertising->start();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
END_BLEMIDI_NAMESPACE
|
END_BLEMIDI_NAMESPACE
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue