Skip to content

Instantly share code, notes, and snippets.

@mr-deamon
Created June 11, 2023 18:25
Show Gist options
  • Save mr-deamon/b1c8b82cfefffac89e0f75b08a7292ee to your computer and use it in GitHub Desktop.
Save mr-deamon/b1c8b82cfefffac89e0f75b08a7292ee to your computer and use it in GitHub Desktop.
#include <NimBLEDevice.h>
BLEServer *pServer = NULL;
BLECharacteristic * pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint8_t txValue = 0;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
/** None of these are required as they will be handled by the library with defaults. **
** Remove as you see fit for your needs */
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
Serial.println("Connected");
deviceConnected = true;
BLEDevice::startAdvertising();//adding this line allows for multiple simultaneous BLE connections
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("Disconnected");
}
/***************** New - Security handled here ********************
****** Note: these are the same return values as defaults ********/
uint32_t onPassKeyRequest() {
Serial.println("Server PassKeyRequest");
return 123456;
}
void onPassKeyNotify(uint32_t passkey) {
Serial.println("Passkey Notify");
}
bool onConfirmPIN(uint32_t pass_key) {
Serial.print("The passkey YES/NO number: "); Serial.println(pass_key);
return true;
}
void onAuthenticationComplete(ble_gap_conn_desc* desc) {
if (desc->sec_state.encrypted) {
if (!desc->sec_state.encrypted) {
Serial.println("Encrypt connection failed - disconnecting");
/** Find the client with the connection handle provided in desc */
NimBLEDevice::getClientByID(desc->conn_handle)->disconnect();
vTaskDelay(5000);//bruteforce protection, 5s
return;
}
Serial.println("Encrypt connection successfull");
}
}
/*******************************************************************/
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.print("Received Value: ");
for (int i = 0; i < rxValue.length(); i++)
Serial.print(rxValue[i]);
if (rxValue.find("F") != -1)
Serial.println("do stuff");
}
}
};
void setup() {
Serial.begin(115200);
pinMode(BUILTIN_LED, OUTPUT); // not working
digitalWrite(BUILTIN_LED, LOW); // not working
// Create the BLE Device
NimBLEDevice::init("NimBLE");
NimBLEDevice::setPower(ESP_PWR_LVL_P9);
NimBLEDevice::setSecurityAuth(true, true, true);
NimBLEDevice::setSecurityAuth(ESP_LE_AUTH_BOND);
NimBLEDevice::setSecurityPasskey(123456);
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY);
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
NIMBLE_PROPERTY::NOTIFY
);
BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
NIMBLE_PROPERTY::WRITE
);
pRxCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->setAppearance(384);
pAdvertising->addServiceUUID((uint16_t)0x1812);
pAdvertising->setScanResponse(true); // must be true or else BLE name gets truncated
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
}
void loop() {
delay(1000);
Serial.println(pServer->getConnectedCount ( ));
Serial.println(NimBLEDevice::getNumBonds ( ));
if (deviceConnected) {
//pTxCharacteristic->setValue('c'); // Sending bluetooth message
//pTxCharacteristic->notify();
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
digitalWrite(BUILTIN_LED, LOW); // not working
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
digitalWrite(BUILTIN_LED, HIGH); // not working
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment