Esp32 Bluetooth Modülü

erenuz40

Üye
Katılım
1 Ocak 2021
Mesajlar
6
Puanları
1
Yaş
21
Esp32 bluetooth modülünü kullanarak bir erişim noktası oluşturdum. Android telefonların bluetooth listesinde gözüküyor fakat ios'larda gözükmüyor.Bunun için yardım edebilecek var mı?


#include <BLEDevice.h>

#include <BLEUtils.h>

#include <BLEServer.h>



#define SERVICE_UUID "0000FFE0-0000-1000-8000-00805F9B34FB"

#define CHARACTERISTIC_UUID "0000FFE1-0000-1000-8000-00805F9B34FB"

//const int led = 2;

#define ONBOARD_LED 22

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;

// 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"



class MySecurity : public BLESecurityCallbacks { // BLE 클래스 pin code 함수

uint32_t onPassKeyRequest(){

ESP_LOGI(LOG_TAG, "PassKeyRequest");

return 123456;

}

void onPassKeyNotify(uint32_t pass_key){

ESP_LOGI(LOG_TAG, "The passkey Notify number:%d", pass_key);

}

bool onConfirmPIN(uint32_t pass_key){

ESP_LOGI(LOG_TAG, "The passkey YES/NO number:%d", pass_key);

vTaskDelay(5000);

return true;

}

bool onSecurityRequest(){

ESP_LOGI(LOG_TAG, "SecurityRequest");

return true;

}

void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl){

ESP_LOGI(LOG_TAG, "Starting BLE work!");

delay(1000);
digitalWrite(ONBOARD_LED,HIGH);
delay(100);
digitalWrite(ONBOARD_LED,LOW);


}

void onConnect(BLEServer* pServer) {
deviceConnected = true;
BLEDevice::startAdvertising();
};

void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}


};



class MyCallbacks: public BLECharacteristicCallbacks {

void onWrite(BLECharacteristic *pCharacteristic) {

std::string value = pCharacteristic->getValue();

if (value.length() > 0) {

for (int i = 0; i < value.length(); i++)

Serial.print(value);

}

}


};



void setup() {

Serial.begin(115200);

// pinMode(led,OUTPUT);
pinMode(ONBOARD_LED,OUTPUT);
BLEDevice::init("ESP32");

BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);

BLEDevice::setSecurityCallbacks(new MySecurity());

pServer = BLEDevice::createServer();

BLEService *pService = pServer->createService(SERVICE_UUID);

pCharacteristic = pService->createCharacteristic(

CHARACTERISTIC_UUID,

BLECharacteristic::pROPERTY_READ |

BLECharacteristic::pROPERTY_WRITE

);

pCharacteristic->setValue("Hello World");

pService->start();

BLEAdvertising *pAdvertising = pServer->getAdvertising();

pAdvertising->start();

BLESecurity *pSecurity = new BLESecurity(); // pin code 설정

uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;

uint32_t passkey = 123456; // PASS

uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE;

esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));

pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_MITM_BOND);

pSecurity->setCapability(ESP_IO_CAP_OUT);

pSecurity->setKeySize(16);

esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t));

pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);

esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));

Serial.println("Characteristic defined! Now you can read it in your phone!");

}



void loop() {

// put your main code here, to run repeatedly:

// notify changed value
if (deviceConnected) {
pCharacteristic->setValue((uint8_t*)&value, 4);
pCharacteristic->notify();
value++;
delay(10); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
}
// 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;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;

// / Blink led when connected
delay(1000);
digitalWrite(ONBOARD_LED,HIGH);
delay(100);
digitalWrite(ONBOARD_LED,LOW);

}




}
 

Yeni mesajlar

Forum istatistikleri

Konular
128,153
Mesajlar
915,488
Kullanıcılar
449,895
Son üye
HalilKonec

Yeni konular

Geri
Üst