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_Defs.h"
|
||||
|
||||
#define SERVICE_UUID "03b80e5a-ede8-4b33-a751-6ce34ec4c700"
|
||||
#define CHARACTERISTIC_UUID "7772e5db-3868-4112-a1a9-f2669d106bf3"
|
||||
|
||||
#if defined(ESP32)
|
||||
#include "Ble_esp32.h"
|
||||
#endif
|
||||
|
|
|
|||
121
src/Ble_esp32.h
121
src/Ble_esp32.h
|
|
@ -1,38 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
// Headers for ESP32 BLE
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEUtils.h>
|
||||
#include <BLEServer.h>
|
||||
|
||||
#define SERVICE_UUID "03b80e5a-ede8-4b33-a751-6ce34ec4c700"
|
||||
#define CHARACTERISTIC_UUID "7772e5db-3868-4112-a1a9-f2669d106bf3"
|
||||
|
||||
BEGIN_BLEMIDI_NAMESPACE
|
||||
|
||||
class BleMidiInterface;
|
||||
|
||||
class MyServerCallbacks: public BLEServerCallbacks {
|
||||
public:
|
||||
MyServerCallbacks(BleMidiInterface* bleMidiInterface) {
|
||||
_bleMidiInterface = bleMidiInterface;
|
||||
}
|
||||
protected:
|
||||
BleMidiInterface* _bleMidiInterface;
|
||||
|
||||
void onConnect(BLEServer* pServer) {
|
||||
// _bleMidiInterface->mConnectedCallback();
|
||||
};
|
||||
|
||||
void onDisconnect(BLEServer* pServer) {
|
||||
// _bleMidiInterface->mDisconnectedCallback();
|
||||
}
|
||||
};
|
||||
|
||||
class MyCallbacks: public BLECharacteristicCallbacks {
|
||||
void onWrite(BLECharacteristic *pCharacteristic) {
|
||||
}
|
||||
};
|
||||
|
||||
class BleMidiInterface
|
||||
{
|
||||
protected:
|
||||
|
|
@ -41,6 +15,7 @@ protected:
|
|||
BLEAdvertising *pAdvertising;
|
||||
BLECharacteristic *pCharacteristic;
|
||||
|
||||
public:
|
||||
void(*mConnectedCallback)();
|
||||
void(*mDisconnectedCallback)();
|
||||
|
||||
|
|
@ -55,40 +30,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
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 bool begin(const char* deviceName);
|
||||
|
||||
inline void onConnected(void(*fptr)()) {
|
||||
mConnectedCallback = fptr;
|
||||
|
|
@ -98,4 +40,61 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
class MyServerCallbacks: public BLEServerCallbacks {
|
||||
public:
|
||||
MyServerCallbacks(BleMidiInterface* bleMidiInterface) {
|
||||
_bleMidiInterface = bleMidiInterface;
|
||||
}
|
||||
protected:
|
||||
BleMidiInterface* _bleMidiInterface;
|
||||
|
||||
void onConnect(BLEServer* pServer) {
|
||||
_bleMidiInterface->mConnectedCallback();
|
||||
};
|
||||
|
||||
void onDisconnect(BLEServer* pServer) {
|
||||
_bleMidiInterface->mDisconnectedCallback();
|
||||
}
|
||||
};
|
||||
|
||||
class MyCallbacks: public BLECharacteristicCallbacks {
|
||||
void onWrite(BLECharacteristic *pCharacteristic) {
|
||||
}
|
||||
};
|
||||
|
||||
bool BleMidiInterface::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;
|
||||
}
|
||||
|
||||
END_BLEMIDI_NAMESPACE
|
||||
|
|
|
|||
Loading…
Reference in New Issue