Activate BLE Bonding and security with Button and show special services
Posted: Tue Apr 23, 2019 5:52 pm
Hello,
i am trying to build a device with esp32 and connect this via BLE to Android/iOS. I will build a custom iOS/Android App.
I'd like to send some basic informtion about the device to all users who might be in range of the esp32. These user are allowed to read/write some information. They do not need to bond.
some users are allowed to read/write special services/characteristics. i have no display but one button(for testing i use the hall sensor) at my esp. so my plan is: if a users presses this button, he is allowed to bond within 10 seconds.
i modified public examples. but i did not manage to activate bonding only for 10 seconds. so i build a work around within the "onConfirmPIN". this is not very nice, because on android the user is show a message "is pin XYZ shown on device"
so my questions are:
A) is there a nice way to activate bonding for 10 seconds after button is pressed?
B) how can i define services/characteristics with are only available for bonded clients?
THANKS a lot in advance for your feedback
here my current code:
i am trying to build a device with esp32 and connect this via BLE to Android/iOS. I will build a custom iOS/Android App.
I'd like to send some basic informtion about the device to all users who might be in range of the esp32. These user are allowed to read/write some information. They do not need to bond.
some users are allowed to read/write special services/characteristics. i have no display but one button(for testing i use the hall sensor) at my esp. so my plan is: if a users presses this button, he is allowed to bond within 10 seconds.
i modified public examples. but i did not manage to activate bonding only for 10 seconds. so i build a work around within the "onConfirmPIN". this is not very nice, because on android the user is show a message "is pin XYZ shown on device"
so my questions are:
A) is there a nice way to activate bonding for 10 seconds after button is pressed?
B) how can i define services/characteristics with are only available for bonded clients?
THANKS a lot in advance for your feedback
here my current code:
Code: Select all
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define LOOP_DELAY 100
float blePairTimeOut = 0;
class MySecurity : public BLESecurityCallbacks {
bool onConfirmPIN(uint32_t pin){
Serial.println("onConfirmPIN");
if (blePairTimeOut > 0){
return true;
}
else{
return false;
}
}
uint32_t onPassKeyRequest(){
Serial.println("onPassKeyRequest");
ESP_LOGI(LOG_TAG, "PassKeyRequest");
return 133700;
}
void onPassKeyNotify(uint32_t pass_key){
Serial.println("onPassKeyNotify");
ESP_LOGI(LOG_TAG, "On passkey Notify number:%d", pass_key);
}
bool onSecurityRequest(){
Serial.println("onSecurityRequest");
ESP_LOGI(LOG_TAG, "On Security Request");
return true;
}
void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl){
Serial.println("onAuthenticationComplete");
ESP_LOGI(LOG_TAG, "Starting BLE work!");
if(cmpl.success){
Serial.println("onAuthenticationComplete -> success");
uint16_t length;
esp_ble_gap_get_whitelist_size(&length);
ESP_LOGD(LOG_TAG, "size: %d", length);
}
else{
Serial.println("onAuthenticationComplete -> fail");
}
}
};
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("ESP32 TW");
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
/*
* Required in authentication process to provide displaying and/or input passkey or yes/no butttons confirmation
*/
BLEDevice::setSecurityCallbacks(new MySecurity());
BLEServer *pServer = BLEDevice::createServer();
//public services
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("Hello World says Neil");
pCharacteristic->setCallbacks("
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
BLESecurity *pSecurity = new BLESecurity();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_MITM_BOND); //ESP_LE_AUTH_REQ_SC_ONLY
pSecurity->setCapability(ESP_IO_CAP_KBDISP);
pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
Serial.println("Characteristic defined! Now you can read it in your phone!");
}
void loop() {
// put your main code here, to run repeatedly:
int val = hallRead();
// print the results to the serial monitor:
//Serial.print("sensor = ");
if (val > 100 ) {
// turn LED on:
Serial.println("BLE ACTIVE");
blePairTimeOut = 10;
} else {
// turn LED off:
}
delay(LOOP_DELAY);
if (blePairTimeOut > 0){
blePairTimeOut = blePairTimeOut - LOOP_DELAY/1000;
}
}