Skip to content

No recieve from Callbacks #225

@podaen

Description

@podaen

Dear,

I want to create a secure server to receive data from an app I created to an esp32. The pin-code was accepted in my android. But something strange happens when the code is accepted. It doesn't confirm the code in the callbacks. I tryed too make a SecurityCallback class but there ware too many arguments...?? Also the CharacteristicCallbacks doesn't appear when I send data. Does anyone have suggestions on that?

#include <NimBLEDevice.h>
#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define PASSKEY 123456
NimBLEServer* pServer = NULL;
NimBLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
String StrRecieved;
/**  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) {
		deviceConnected = true;
		BLEDevice::startAdvertising();
	};
	void onDisconnect(BLEServer* pServer) {
		deviceConnected = false;
	}
	/***************** New - Security handled here ********************
	****** Note: these are the same return values as defaults ********/
	uint32_t onPassKeyRequest() {
		Serial.println("Server PassKeyRequest");
		return PASSKEY;
		//return 000000;
	}
	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) {
		Serial.println("Starting BLE work!");
	}
	/*******************************************************************/
};
//class SecurityCallback : public BLESecurityCallbacks {
//
//	uint32_t onPassKeyRequest() {
//		return 000000;
//	}
//
//	void onPassKeyNotify(uint32_t pass_key) {}
//
//	bool onConfirmPIN(uint32_t pass_key) {
//		vTaskDelay(5000);
//		return true;
//	}
//
//	bool onSecurityRequest() {
//		return true;
//	}
//
//	void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl) {
//		if (cmpl.success) {
//			Serial.println("   - SecurityCallback - Authentication Success");
//		}
//		else {
//			Serial.println("   - SecurityCallback - Authentication Failure*");
//			pServer->removePeerDevice(pServer->getConnId(), true);
//		}
//		BLEDevice::startAdvertising();
//	}
//};

/** Handler class for characteristic actions */
class CharacteristicCallbacks : public NimBLECharacteristicCallbacks {
	void onWrite(NimBLECharacteristic* pCharacteristic) {
		std::string value = pCharacteristic->getValue();
		Serial.println("here");
		if (value.length() > 0) {
			StrRecieved = "";
			for (int i = 0; i < value.length(); i++) {
				// Serial.print(value[i]); 
				StrRecieved = StrRecieved + value[i];
			}
			Serial.println("*********");
			Serial.print("Recieved = ");
			Serial.println(StrRecieved); 
		}
	}
};
void setup() {
	Serial.begin(115200);
	Serial.println("Starting NimBLE Server");
	/** sets device name */
	NimBLEDevice::init("NimBLE-Arduino");
	/** Optional: set the transmit power, default is 3db */
	NimBLEDevice::setPower(ESP_PWR_LVL_P9); /** +9db */
	NimBLEDevice::setSecurityAuth(true, true, true);
	//NimBLEDevice::setSecurityAuth(false, false, true);
	NimBLEDevice::setSecurityPasskey(PASSKEY);
	//NimBLEDevice::setSecurityPasskey(PASSKEY);
	NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY);
	//NimBLEServer* pServer = NimBLEDevice::createServer();
	//BLEDevice::setSecurityCallbacks(new SecurityCallback());
	pServer = NimBLEDevice::createServer();
	pServer->setCallbacks(new MyServerCallbacks());
	NimBLEService* pService = pServer->createService(SERVICE_UUID);
	NimBLECharacteristic* pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::READ_AUTHEN);
	pCharacteristic->setCallbacks(new CharacteristicCallbacks());
        pService->start();
	NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
	pAdvertising->addServiceUUID(SERVICE_UUID);
	//pAdvertising->setScanResponse(true);
	pAdvertising->start();
	Serial.println("Advertising Started");
	////NimBLEDevice::setMTU(128);//128bytes
	//Serial.println("recieving size MTU");
	//Serial.println(NimBLEDevice::getMTU());
}
void loop() {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions