static void run() { ESP_LOGD(LOG_TAG, "MLE-15 sample starting"); BLEDevice::init(""); BLEClient* pClient = BLEDevice::createClient(); pClient->connect(BLEAddress("ff:ff:45:19:14:80")); BLERemoteService* pRemoteService = pClient->getService(serviceUUID); if (pRemoteService == nullptr) { ESP_LOGD(LOG_TAG, "Failed to find our service UUID: %s", serviceUUID.toString().c_str()); return; } BLERemoteCharacteristic* pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID); if (pRemoteCharacteristic == nullptr) { ESP_LOGD(LOG_TAG, "Failed to find our characteristic UUID: %s", charUUID.toString().c_str()); return; } pRemoteCharacteristic->writeValue((uint8_t)1); //BLEClient *pClient = BLE::createClient(); //pClient->setClientCallbacks(new MyClientCallbacks()); //pClient->connect(BLEAddress("00:00:00:00:00:00")); }
void run(void *data) { BLEAddress* pAddress = (BLEAddress *)data; BLEClient* pClient = BLEDevice::createClient(); pClient->connect(*pAddress); pClient->getServices(); BLERemoteService* pRemoteService = pClient->getService(serviceUUID); if (pRemoteService == nullptr) { ESP_LOGD(LOG_TAG, "Failed to find our service UUID: %s", serviceUUID.toString().c_str()); return; } pRemoteService->getCharacteristics(); BLERemoteCharacteristic* pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID); if (pRemoteCharacteristic == nullptr) { ESP_LOGD(LOG_TAG, "Failed to find our characteristic UUID: %s", charUUID.toString().c_str()); return; } pRemoteCharacteristic->readValue(); pRemoteCharacteristic->writeValue("123"); pRemoteCharacteristic->registerForNotify(notifyCallback); pClient->disconnect(); ESP_LOGD(LOG_TAG, "%s", pClient->toString().c_str()); ESP_LOGD(LOG_TAG, "-- End of task"); }
void run(void* data) { BLEAddress* pAddress = (BLEAddress*)data; BLEClient* pClient = BLEDevice::createClient(); BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT); BLEDevice::setSecurityCallbacks(new MySecurity()); BLESecurity *pSecurity = new BLESecurity(); pSecurity->setKeySize(); pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_ONLY); pSecurity->setCapability(ESP_IO_CAP_IO); pSecurity->setRespEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK); // Connect to the remove BLE Server. pClient->connect(*pAddress); // Obtain a reference to the service we are after in the remote BLE server. BLERemoteService* pRemoteService = pClient->getService(serviceUUID); if (pRemoteService == nullptr) { ESP_LOGD(LOG_TAG, "Failed to find our service UUID: %s", serviceUUID.toString().c_str()); return; } // Obtain a reference to the characteristic in the service of the remote BLE server. BLERemoteCharacteristic* pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID); if (pRemoteCharacteristic == nullptr) { ESP_LOGD(LOG_TAG, "Failed to find our characteristic UUID: %s", charUUID.toString().c_str()); return; } // Read the value of the characteristic. std::string value = pRemoteCharacteristic->readValue(); ESP_LOGD(LOG_TAG, "The characteristic value was: %s", value.c_str()); while(1) { // Set a new value of the characteristic ESP_LOGD(LOG_TAG, "Setting the new value"); std::ostringstream stringStream; struct timeval tv; gettimeofday(&tv, nullptr); stringStream << "Time since boot: " << tv.tv_sec; pRemoteCharacteristic->writeValue(stringStream.str()); FreeRTOS::sleep(1000); } pClient->disconnect(); ESP_LOGD(LOG_TAG, "%s", pClient->toString().c_str()); ESP_LOGD(LOG_TAG, "-- End of task"); } // run
/** * @brief Retrieve all the characteristics for this service. * This function will not return until we have all the characteristics. * @return N/A */ void BLERemoteService::retrieveCharacteristics() { ESP_LOGD(LOG_TAG, ">> getCharacteristics() for service: %s", getUUID().toString().c_str()); removeCharacteristics(); // Forget any previous characteristics. uint16_t offset = 0; esp_gattc_char_elem_t result; while(1) { uint16_t count = 1; esp_gatt_status_t status = ::esp_ble_gattc_get_all_char( getClient()->getGattcIf(), getClient()->getConnId(), m_startHandle, m_endHandle, &result, &count, offset ); if (status == ESP_GATT_INVALID_OFFSET) { // We have reached the end of the entries. break; } if (status != ESP_GATT_OK) { // If we got an error, end. ESP_LOGE(LOG_TAG, "esp_ble_gattc_get_all_char: %s", BLEUtils::gattStatusToString(status).c_str()); break; } if (count == 0) { // If we failed to get any new records, end. break; } ESP_LOGD(LOG_TAG, "Found a characteristic: Handle: %d, UUID: %s", result.char_handle, BLEUUID(result.uuid).toString().c_str()); // We now have a new characteristic ... let us add that to our set of known characteristics BLERemoteCharacteristic *pNewRemoteCharacteristic = new BLERemoteCharacteristic( result.char_handle, BLEUUID(result.uuid), result.properties, this ); m_characteristicMap.insert(std::pair<std::string, BLERemoteCharacteristic*>(pNewRemoteCharacteristic->getUUID().toString(), pNewRemoteCharacteristic)); offset++; // Increment our count of number of descriptors found. } // Loop forever (until we break inside the loop). m_haveCharacteristics = true; // Remember that we have received the characteristics. ESP_LOGD(LOG_TAG, "<< getCharacteristics()"); } // getCharacteristics
void run(void* data) { BLEAddress* pAddress = (BLEAddress*)data; BLEClient* pClient = BLEDevice::createClient(); // Connect to the remove BLE Server. pClient->connect(*pAddress); // Obtain a reference to the service we are after in the remote BLE server. BLERemoteService* pRemoteService = pClient->getService(serviceUUID); if (pRemoteService == nullptr) { ESP_LOGD(LOG_TAG, "Failed to find our service UUID: %s", serviceUUID.toString().c_str()); return; } // Obtain a reference to the characteristic in the service of the remote BLE server. BLERemoteCharacteristic* pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID); if (pRemoteCharacteristic == nullptr) { ESP_LOGD(LOG_TAG, "Failed to find our characteristic UUID: %s", charUUID.toString().c_str()); return; } // Read the value of the characteristic. std::string value = pRemoteCharacteristic->readValue(); ESP_LOGD(LOG_TAG, "The characteristic value was: %s", value.c_str()); while(1) { // Set a new value of the characteristic ESP_LOGD(LOG_TAG, "Setting the new value"); std::ostringstream stringStream; struct timeval tv; gettimeofday(&tv, nullptr); stringStream << "Time since boot: " << tv.tv_sec; pRemoteCharacteristic->writeValue(stringStream.str()); FreeRTOS::sleep(1000); } pClient->disconnect(); ESP_LOGD(LOG_TAG, "%s", pClient->toString().c_str()); ESP_LOGD(LOG_TAG, "-- End of task"); } // run
void BLEPeripheral::begin() { unsigned char advertisementDataType = 0; unsigned char scanDataType = 0; unsigned char advertisementDataLength = 0; unsigned char scanDataLength = 0; unsigned char advertisementData[BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH]; unsigned char scanData[BLE_SCAN_DATA_MAX_VALUE_LENGTH]; if (this->_serviceSolicitationUuid){ BLEUuid serviceSolicitationUuid = BLEUuid(this->_serviceSolicitationUuid); advertisementDataLength = serviceSolicitationUuid.length(); advertisementDataType = (advertisementDataLength > 2) ? 0x15 : 0x14; memcpy(advertisementData, serviceSolicitationUuid.data(), advertisementDataLength); } else if (this->_advertisedServiceUuid){ BLEUuid advertisedServiceUuid = BLEUuid(this->_advertisedServiceUuid); advertisementDataLength = advertisedServiceUuid.length(); advertisementDataType = (advertisementDataLength > 2) ? 0x06 : 0x02; memcpy(advertisementData, advertisedServiceUuid.data(), advertisementDataLength); } else if (this->_manufacturerData && this->_manufacturerDataLength > 0) { advertisementDataLength = this->_manufacturerDataLength; if (advertisementDataLength > sizeof(advertisementData)) { advertisementDataLength = sizeof(advertisementData); } advertisementDataType = 0xff; memcpy(advertisementData, this->_manufacturerData, advertisementDataLength); } if (this->_localName){ unsigned char localNameLength = strlen(this->_localName); scanDataLength = localNameLength; if (scanDataLength > sizeof(scanData)) { scanDataLength = sizeof(scanData); } scanDataType = (localNameLength > scanDataLength) ? 0x08 : 0x09; memcpy(scanData, this->_localName, scanDataLength); } if (this->_localAttributes == NULL) { this->initLocalAttributes(); } for (int i = 0; i < this->_numLocalAttributes; i++) { BLELocalAttribute* localAttribute = this->_localAttributes[i]; if (localAttribute->type() == BLETypeCharacteristic) { BLECharacteristic* characteristic = (BLECharacteristic*)localAttribute; characteristic->setValueChangeListener(*this); } } for (int i = 0; i < this->_numRemoteAttributes; i++) { BLERemoteAttribute* remoteAttribute = this->_remoteAttributes[i]; if (remoteAttribute->type() == BLETypeCharacteristic) { BLERemoteCharacteristic* remoteCharacteristic = (BLERemoteCharacteristic*)remoteAttribute; remoteCharacteristic->setValueChangeListener(*this); } } if (this->_numRemoteAttributes) { this->addRemoteAttribute(this->_remoteGenericAttributeService); this->addRemoteAttribute(this->_remoteServicesChangedCharacteristic); } this->_device->begin(advertisementDataType, advertisementDataLength, advertisementData, scanDataType, scanDataLength, scanData, this->_localAttributes, this->_numLocalAttributes, this->_remoteAttributes, this->_numRemoteAttributes); this->_device->requestAddress(); }
void BLEPeripheral::BLEDeviceRemoteCharacteristicValueChanged(BLEDevice& device, BLERemoteCharacteristic& remoteCharacteristic, const unsigned char* value, unsigned char valueLength) { remoteCharacteristic.setValue(this->_central, value, valueLength); }
void BLEPeripheral::begin() { unsigned char advertisementDataSize = 0; BLEEirData advertisementData[3]; BLEEirData scanData; scanData.length = 0; unsigned char remainingAdvertisementDataLength = BLE_ADVERTISEMENT_DATA_MAX_VALUE_LENGTH + 2; if (this->_serviceSolicitationUuid){ BLEUuid serviceSolicitationUuid = BLEUuid(this->_serviceSolicitationUuid); unsigned char uuidLength = serviceSolicitationUuid.length(); advertisementData[advertisementDataSize].length = uuidLength; advertisementData[advertisementDataSize].type = (uuidLength > 2) ? 0x15 : 0x14; memcpy(advertisementData[advertisementDataSize].data, serviceSolicitationUuid.data(), uuidLength); advertisementDataSize += 1; remainingAdvertisementDataLength -= uuidLength + 2; } if (this->_advertisedServiceUuid){ BLEUuid advertisedServiceUuid = BLEUuid(this->_advertisedServiceUuid); unsigned char uuidLength = advertisedServiceUuid.length(); if (uuidLength + 2 <= remainingAdvertisementDataLength) { advertisementData[advertisementDataSize].length = uuidLength; advertisementData[advertisementDataSize].type = (uuidLength > 2) ? 0x06 : 0x02; memcpy(advertisementData[advertisementDataSize].data, advertisedServiceUuid.data(), uuidLength); advertisementDataSize += 1; remainingAdvertisementDataLength -= uuidLength + 2; } } if (this->_manufacturerData && this->_manufacturerDataLength > 0) { if (remainingAdvertisementDataLength >= 3) { unsigned char dataLength = this->_manufacturerDataLength; if (dataLength + 2 > remainingAdvertisementDataLength) { dataLength = remainingAdvertisementDataLength - 2; } advertisementData[advertisementDataSize].length = dataLength; advertisementData[advertisementDataSize].type = 0xff; memcpy(advertisementData[advertisementDataSize].data, this->_manufacturerData, dataLength); advertisementDataSize += 1; remainingAdvertisementDataLength -= dataLength + 2; } } if (this->_localName){ unsigned char localNameLength = strlen(this->_localName); scanData.length = localNameLength; if (scanData.length > BLE_SCAN_DATA_MAX_VALUE_LENGTH) { scanData.length = BLE_SCAN_DATA_MAX_VALUE_LENGTH; } scanData.type = (localNameLength > scanData.length) ? 0x08 : 0x09; memcpy(scanData.data, this->_localName, scanData.length); } if (this->_localAttributes == NULL) { this->initLocalAttributes(); } for (int i = 0; i < this->_numLocalAttributes; i++) { BLELocalAttribute* localAttribute = this->_localAttributes[i]; if (localAttribute->type() == BLETypeCharacteristic) { BLECharacteristic* characteristic = (BLECharacteristic*)localAttribute; characteristic->setValueChangeListener(*this); } } for (int i = 0; i < this->_numRemoteAttributes; i++) { BLERemoteAttribute* remoteAttribute = this->_remoteAttributes[i]; if (remoteAttribute->type() == BLETypeCharacteristic) { BLERemoteCharacteristic* remoteCharacteristic = (BLERemoteCharacteristic*)remoteAttribute; remoteCharacteristic->setValueChangeListener(*this); } } if (this->_numRemoteAttributes) { this->addRemoteAttribute(this->_remoteGenericAttributeService); this->addRemoteAttribute(this->_remoteServicesChangedCharacteristic); } this->_device->begin(advertisementDataSize, advertisementData, scanData.length > 0 ? 1 : 0, &scanData, this->_localAttributes, this->_numLocalAttributes, this->_remoteAttributes, this->_numRemoteAttributes); this->_device->requestAddress(); }